[Typescript Kaop-ts] Use AOP in Vue Components with TypeScript and Kaop-ts
Aspect Oriented Programming, AOP, allows to reuse logic across an entire app in a very neat way, decoupling it from the business logic. Kaop-ts bring us decorators in order to apply AOP. This lesson will show you how you can move cache and exception handling out of your business logic using TypeScript and Kaop-ts
install:
npm i --save kaop-ts
HTML:
<button @click="cacheIt">Cache</button>
Import:
import { beforeMethod, afterMethod, onException, Metadata } from "kaop-ts";
Method:
@beforeMethod(Advice.getCached)
@afterMethod(Advice.setCached)
@afterMethod((meta) => Advice.notify(meta, true))
@onException(Advice.report)
cacheIt() {
console.log("Method executed");
return fetch("https://jsonplaceholder.typicode.com/users/1")
.then(res => res.json())
.then(user => (this.userName = user.name));
}
Advice:
class Advice {
static getCached(meta: Metadata<any>) {
// Access one prop value
console.log(
`Cache: ${meta.scope.checkbox.value} -- ${meta.scope.checkbox.checked}`
);
// Component name -- method name
const key = `${meta.scope.$options["_componentTag"]}--${meta.key}`;
const cached = localStorage.getItem(key);
if (cached) {
meta.scope.userName = cached;
// if have cache then stop here, won't go though the method
meta.prevent();
console.log(meta.scope);
}
}
static setCached(meta: Metadata<any>) {
const key = `${meta.scope.$options["_componentTag"]}--${meta.key}`;
// From value return by original method are stored in meta.result
if (meta.result) {
meta.result.then(() => {
localStorage.setItem(key, meta.scope.userName);
console.log(meta.scope);
});
}
}
static report(meta: Metadata<any>) {
console.error('Exception ocurred', meta.exception)
}
static notify (meta, toServer) {
// Adding extra params to the function
console.log('Notifying', toServer)
}
}
[Typescript Kaop-ts] Use AOP in Vue Components with TypeScript and Kaop-ts的更多相关文章
- [Vue @Component] Extend Vue Components in TypeScript
This lesson shows how you can extend and reuse logic in Vue components using TypeScript inheritance. ...
- [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript
With properties we can follow a one-way parent→child flow communication between components. This les ...
- Vue 中使用 TypeScript 详细总结
VUE 项目中使用 Typescript 第一节:项目起步 Vue 中使用 TypeScript 项目中主要使用到的第三方依赖 vue2 vue-class-component vue-propert ...
- typescript整合到vue中的详细介绍,ts+vue一梭子
通过vue-cli命令行安装vue项目,注意不要eslint 安装依赖 cnpm install typescript --save-dev cnpm install ts-loader --save ...
- [Vue + TS] Create Type-Safe Vue Directives in TypeScript
Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...
- 原有vue项目接入typescript
原有vue项目接入typescript 为什么要接入typescript javascript由于自身的弱类型,使用起来非常灵活. 这也就为大型项目.多人协作开发埋下了很多隐患.如果是自己的私有业务倒 ...
- vue.js使用typescript踩坑记
最近在把https://github.com/renrenio/renren-fast-vue这个项目转为typescript,在此记录一下遇到的小坑 name坑:属性该怎么给? 声明文件坑:如何解决 ...
- 初次在Vue项目使用TypeScript,需要做什么
前言 总所周知,Vue新版本3.0 使用 TypeScript 开发,让本来就很火的 TypeScript 受到更多人的关注.虽然 TypeScript 在近几年才火,但其实它诞生于2012年10月, ...
- 在Vue 中使用Typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...
随机推荐
- Zookeeper系列(一)
一.ZooKeeper的背景 1.1 认识ZooKeeper ZooKeeper---译名为“动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而 ...
- Java Web数据库篇之MySQL特性
MySQL ExplainEXPLAIN 命令的输出内容大致如下: mysql> explain select * from user_info where id = 2\G********** ...
- 共享win7ip,虚拟机nat模式连接,电脑重启之后,无法连接
问题原因:VMware NAT Service没有设置为开机启动 解决办法: 1.按win+r,输入services.msc,点击确定: 2.服务窗口中找到VMware NAT Service,双击: ...
- CE工具里自带的学习工具--第六关
这一步原理: 相当于有一个变量 int a=100; int *p=&a; 点击修改值, 在ce工具里可以找到a的值. a的地址. 但是在实际代码里,并不是这么处理的, 是 通过指针改变这 ...
- 解决android的键盘弹出时,html页面的高度被压缩
如果元素的高度是用100%表示,那么,安卓的键盘弹出时,高度会发生变化,导致布局混乱,所以最好给高度设置像素高度 $("html,body").height(window.inne ...
- windows开发错误
2018/07/16: 1.问题: 代码: list <int> listN; error C2065:'list' : undeclared identifier 我已经#include ...
- 18第一章 ASP.Net内建对象
第一章 ASP.Net内建对象 第一章 ASP.Net内建对象 ASP.Net为保持用户的数据和信息,内建了许多对象,包括Application.Response.Requ ...
- myeclipse出现Failed to load JavaHL Library.
eclipse启动出现如图的状况: 解决方法: Window-Preferences-Team-SVN,在SVN接口的下拉框可以看到,默认选择的是JavaHL(JNI) Not Available,手 ...
- React开发实时聊天招聘工具 -第四章 Redux
复杂以后 setState 就不太方便了 所以使用Redux来管理 React只负责View. Store.State.Dispatch.Reducer reducer(state,action) { ...
- ubuntu(linux)占领小米平板2(mipad2)
昨天 2014年,媳妇坐月子,给媳妇买了mi6和一个小米平板2(16G).是我们人生拥有的第一个平板,激动不已. 买之前看了小米平板1的口碑不错,arm构架,NVIDIA的主板好像,图形处理做得当然没 ...