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的更多相关文章

  1. [Vue @Component] Extend Vue Components in TypeScript

    This lesson shows how you can extend and reuse logic in Vue components using TypeScript inheritance. ...

  2. [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 ...

  3. Vue 中使用 TypeScript 详细总结

    VUE 项目中使用 Typescript 第一节:项目起步 Vue 中使用 TypeScript 项目中主要使用到的第三方依赖 vue2 vue-class-component vue-propert ...

  4. typescript整合到vue中的详细介绍,ts+vue一梭子

    通过vue-cli命令行安装vue项目,注意不要eslint 安装依赖 cnpm install typescript --save-dev cnpm install ts-loader --save ...

  5. [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 ...

  6. 原有vue项目接入typescript

    原有vue项目接入typescript 为什么要接入typescript javascript由于自身的弱类型,使用起来非常灵活. 这也就为大型项目.多人协作开发埋下了很多隐患.如果是自己的私有业务倒 ...

  7. vue.js使用typescript踩坑记

    最近在把https://github.com/renrenio/renren-fast-vue这个项目转为typescript,在此记录一下遇到的小坑 name坑:属性该怎么给? 声明文件坑:如何解决 ...

  8. 初次在Vue项目使用TypeScript,需要做什么

    前言 总所周知,Vue新版本3.0 使用 TypeScript 开发,让本来就很火的 TypeScript 受到更多人的关注.虽然 TypeScript 在近几年才火,但其实它诞生于2012年10月, ...

  9. 在Vue 中使用Typescript

    Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...

随机推荐

  1. 【Linux】CentOS tar压缩与解压命令大全

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  2. 比较 String,StringBuffer,StringBuilder

    1)三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2)String <(StringBuffer,StringBuild ...

  3. 查看DNS、IP、Mac等

    A.Win98:winipcfg  B.Win2000以上:Ipconfig/all  C.NSLOOKUP:如查看河北的DNS  C:\\>nslookup  Default Server: ...

  4. MyEclipse2017修改Web Context Root

    1,复制一个已经存在的项目,并修改项目名 2,选中项目右键选择properities,打开. 但是这里的web context root无法修改 3,删除web显示properties的所有属性,输入 ...

  5. 数据结构之线性顺序表ArrayList(Java实现)

    一.ListMe接口: import java.util.ArrayList; //实现线性表(顺序表和链表)的接口://提供add get isEmpty size 功能public interfa ...

  6. android实战简易教程-链接

    http://blog.csdn.net/yayun0516/article/category/2799943

  7. Javascript创建对象几种方法解析

    Javascript创建对象几种方法解析 Javascript面向对象编程一直是面试中的重点,将自己的理解整理如下,主要参考<Javascript高级程序设计 第三版>,欢迎批评指正. 通 ...

  8. 专题训练——[kuangbin带你飞]最短路练习

    最短路练习 0. Til the Cows Come Home  POJ - 2387 完美的模板题 //#include<Windows.h> #include<iostream& ...

  9. [Luogu] P4838 P哥破解密码

    题目背景 P哥是一个经常丢密码条的男孩子. 在ION 8102赛场上,P哥又弄丢了密码条,笔试满分的他当然知道这可是要扣5分作为惩罚的,于是他开始破解ION Xunil系统的密码. 题目描述 定义一个 ...

  10. [BZOJ] 1037 [ZJOI2008]生日聚会

    Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3007 Solved: 1795 [Submit][Status][Discuss] Descript ...