我是Angular 2的新手,但我在Angular 1.x中有很好的经验。
我收到错误: 找不到模块&aspect; js / dist / lib / aspect'
以下是我的代码:
logging.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(', ')}.`
);
}
}
aspect定义了一个建议,该建议应用于以名称中包含regex-pattern(Matter | Customer)服务的类中的get开头的所有方法调用。建议可用的元数据可能包含实际的方法和类名以及方法调用参数
invoice.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的任何其他方法答案 0 :(得分:1)
你试过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() {
// ....
}
}