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. spring 获取ApplicationContext

    第一种:获取根目录下的文件名 ApplicationContext ac = new ClassPathXmlApplicationContext("../mvc-dispatcher-se ...

  2. 3D旋转矩阵的推导过程

    3D旋转矩阵的推导过程 包含平移的线性变换称作仿射变换,3D中的仿射变换不能用 3 x 3 矩阵表达,必须使用4 x 4矩阵. 一般来说,变换物体相当于以相反的量变换描述这个物体的坐标系.当有多个变换 ...

  3. Nexus环境搭建

    安装 1.解压nexus-2.11.01-bundle.zip到F:\Java\nexus(可自定义) 2.进入F:\Java\nexus\nexus-2.11.1-01\bin\jsw进入相应的系统 ...

  4. (转)淘淘商城系列——使用maven构建工程

    http://blog.csdn.net/yerenyuan_pku/article/details/72669269 开发工具和环境 这里,我统一规范一下淘淘商城的开发工具和环境,如下: Eclip ...

  5. SparkRPC源码分析之RPC管道与消息类型

    SparkRPC源码分析之RPC管道与消息类型我们前面看过了netty基础知识扫盲,那我们应该明白,ChannelHandler这个组件内为channel的各种事件提供了处理逻辑,也就是主要业务逻辑写 ...

  6. 编写高质量Python代码的59个有效方法

    Python学习资料或者需要代码.视频加Python学习群:960410445 1. 用Pythonic方式思考 第一条:确认自己使用的Python版本 (1)有两个版本的python处于活跃状态,p ...

  7. jQuery 返回顶部效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 比较 String,StringBuffer,StringBuilder

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

  9. selenium click radio

    radio = dr.find_element_by_xpath('//*[@id="contentTable"]/tbody/tr[1]/td[1]/input') webdri ...

  10. js 随机生成颜色

    方法一  function randomColor (){ var str='#'; for(var i=0;i<6;i++){ str+=Math.floor(Math.random()*16 ...