装饰者在角度2环境中被执行两次

时间:2017-06-19 10:07:40

标签: angular typescript angular-cli angular-decorator

在使用装饰器时,在ng2(基于ang-cli的)环境中,我写了一个.spec.ts文件。 我期待着这个: -

  • 表达式评估将自上而下
  • 结果将是函数,并将自下而上执行。

两者都在发生(正如预期的那样),这很好,但是,一切都在执行两次。

见下文:=

testchild1.spec.ts

//fdescribe("Ang2: just execute this suite only", function(){

//(function) expression evaluation is top-down
//results (of functions) are then called as functions from down-top

function f(){
  console.log('f() expression evaluated ');
  return function(tar, x:string, descriptor: PropertyDescriptor){
    console.log('f() called');
  }
}

function g(){
  console.log('g() expression evaluated ');
  return function(tar, x:string, descriptor: PropertyDescriptor){
    console.log('g() called');
  }
}

class C{
  @f()
  @g()
  somemethod(){
    console.log('inside somemethod');
  }
} //class C ends.

//});

O / P: -

D:\js\Ang2\proj2\ngrxproj03>ng test src/app/testchild1/testchild1.spec.ts
Your global Angular CLI version (1.1.2) is greater than your local
version (1.1.1). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
 10% building modules 1/1 modules 0 active19 06 2017 15:30:16.929:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
19 06 2017 15:30:16.932:INFO [launcher]: Launching browser Chrome with unlimited concurrency
19 06 2017 15:30:16.940:INFO [launcher]: Starting browser Chrome
19 06 2017 15:30:30.788:INFO [Chrome 58.0.3029 (Windows 8 0.0.0)]: Connected on socket qmmNpsc8yjStv6b-AAAA with id 45423503
Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'f() expression evaluated '

Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'f() expression evaluated '
Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'g() expression evaluated '

Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'g() expression evaluated '
Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'g() called'

Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'g() called'
Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'f() called'

Chrome 58.0.3029 (Windows 8 0.0.0) LOG: 'f() called'
Chrome 58.0.3029 (Windows 8 0.0.0): Executed 2 of 6 (skipped 4) SUCCESS (0.092 secs / 0.187 secs)

了解如何调用所有内容两次。 我做的事情(很明显)是错的吗? 任何指针都会很棒。 (浪费了半天时间)

0 个答案:

没有答案