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. 【译】x86程序员手册34-9.7错误代码

    9.7 Error Code 错误代码 With exceptions that relate to a specific segment, the processor pushes an error ...

  2. 浅谈CSS中的定位知识

    1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...

  3. spring中路径的注入

    @RequestMapping("${mgt}/file") //请求的路径的统一添加,需要在mvc层配置<context:property-placeholder loca ...

  4. redis 其他特性

    1.消息订阅与发布 subscribe my1 订阅频道 psubscribe my1* 批量订阅频道,订阅以my1开头的所有频道 publish my1 hello 在指定频道中发布消息,返回值为接 ...

  5. list.extend的结果是None

    执行list.exend()方法后,会直接修改list本身,而不会产生返回值 In [97]: d=(43,) In [98]: type(d) Out[98]: tuple In [99]: c O ...

  6. vs2019装了WDK后,编译其他vc工程,提示无法打开文件"msvcprtd.lib"

    今天安装了vs2019,而后又安装了wdk,随便写了一个控制台测试程序,居然报错.网上也查了一圈,也没有得到解决.报错内容如下: MSB8038:已启用Spectre缓解,但找不到Spectre缓解库 ...

  7. CAD交互绘制圆弧(com接口)

    在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...

  8. Maven的pom报错的解决方法

    如果在MyEclipse里面导入项目,导入不了,如下图 接下来可以点击Import Maven Projects里的Action那一行Resolve Later. 点击Do Not Execute(a ...

  9. 【C语言】控制台窗口图形界面编程(八):键盘事件

    目录 00. 目录 01. INPUT_RECORD结构 02. KEY_EVENT_RECORD结构 03. ReadConsoleInput函数 04. 示例程序 00. 目录 01. INPUT ...

  10. linux中查看文件指定行的数据

    http://jingyan.baidu.com/article/15622f24125872fdfdbea560.html