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. HTTP/2 带来的新颖

    Binary Frames HTTP2 在TCP层之上 HTTP下增加了Binary Frames协议, 基于该协议HTTP Request/Response 可以被分拆成由Frame组成的Strea ...

  2. centos7 安装后,意外出现Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]

    安装完成centos7后出现如下提示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License informat ...

  3. CAS机制总结

    一.简介 CAS机制:(Compare and set)比较和替换 简单来说–>使用一个期望值来和当前变量的值进行比较,如果当前的变量值与我们期望的值相等,就用一个新的值来更新当前变量的值CAS ...

  4. 【VScode】使用VScode 来写markdown时序图

    准备工作 在VScode中下载插件Markdown Preview Enhanced插件 创建一个.md文件 在VScode中打开文件,界面内点击右键可以看到Open preview to the s ...

  5. extjs传递参数

    以前我是这么传的: 先获取表Form,再通过表找组件,然后用gradeCode=0&groupCode=1传 现在只需要先获得表,然后通过form.getValues()取得所有的值,再使用E ...

  6. win10 专业版 安装tornado 的步骤

    win10 专业版 安装tornado 的步骤: 1.下载tornado源码压缩包 下载网址:https://github.com/tornadoweb/tornado 若是没有github 账号可以 ...

  7. LNMP构架搭建论坛

    1 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel fontconfig fo ...

  8. 树莓派 - 驱动hello

    树莓派上Linux驱动,从hello world 开始 ... hello.c #include <linux/init.h> #include <linux/module.h> ...

  9. PHP:Mysql判断KEY是否存在 如果存在走修改 如果不存在走添加

    文章来源:http://www.cnblogs.com/hello-tl/p/7738113.html 0.PHP代码 <?php /** * POST 传参 * * 例子 添加修改 使用同一个 ...

  10. noip模拟赛 区间

    分析:要遍历所有的区间,肯定是枚举左端点,然后再枚举右端点.关键是怎么高效地求区间&,|,一般而言是用前缀和的,但是&,|不满足区间可减性,所以可以考虑线段树?这道题不带修改操作,用线 ...