Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement aop in angular 2

I am new in Angular 2 but I have good experience in Angular 1.x.

I am getting error: Cannot find module 'aspect.js/dist/lib/aspect'

Below are my code:

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 defines an advice which is applied to all method calls starting with get within classes containing the regex-pattern (Matter|Customer)Service in their name. The Metadata available to the advice may contain the actual method- and class names along with the method-call parameters

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()
      );
  }
}

And please suggest any other way to implement AOP in angular2

like image 943
Ramu Agrawal Avatar asked Sep 09 '16 08:09

Ramu Agrawal


1 Answers

Have you tried kaop-ts? I find it more intuitive and it is working for me on a company project

// 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() {
    // ....
  }
}
like image 88
Alex JM Avatar answered Oct 24 '22 12:10

Alex JM