我是 Angular 2 的新手,但我在 Angular 1.x 方面有很好的经验。
我收到错误:
找不到模块“aspect.js/dist/lib/aspect”
下面是我的代码:
日志记录.aspect.ts
import {Injectable} from '@angular/core';
import {beforeMethod, Metadata} from 'aspect.js/dist/lib/aspect';
@Injectable()
export class LogAspect {
@beforeMethod({
classNamePattern: /(Matter|Customer)Service/,
methodNamePattern: /^(get)/
})
invokeBeforeMethod(meta: Metadata) {
console.log(`Inside of the logger.
Called ${meta.className}.${meta.method.name}
with args: ${meta.method.args.join(', ')}.`
);
}
}
方面定义了一个建议,该建议应用于名称中包含正则表达式模式 (Matter|Customer)Service 的类中以 get 开头的所有方法调用。该建议可用的元数据可能包含实际的方法和类名称以及方法调用参数
发票.service.ts
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import {Wove} from 'aspect.js/dist/lib/aspect';
import {Matter} from './Matter.model';
@Injectable()
@Wove()
export class MatterService{
private url: string;
constructor(private http: Http) {
this.url = '/data/matters/data.json';
}
get(): Observable<Matter[]> {
return this.http.get(this.url)
.map(
(response) => <Matter[]>response.json()
);
}
}
请建议在 Angular2 中实现 AOP 的任何其他方法
你有没有尝试过kaop-ts https://github.com/k1r0s/kaop-ts?我发现它更直观,并且它对我的公司项目很有用
// LogAspect.ts
export class LogAspect {
static log(meta) {
console.log('Called: ', meta.target)
console.log('Args: ', meta.args)
}
}
// YourService.ts
import { Injectable } from '@angular/core'
import { beforeMethod } from 'kaop-ts'
import { LogAspect } from './LogAspect'
@Injectable()
export class YourService {
@beforeMethod(LogAspect.log)
get() {
// ....
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)