2017-10-15 2 views
0

왜 결과가 "구성 요소 앞의 지시문"인지 이해할 수 없습니다.데코레이터 실행 순서는 무엇입니까?

function Component(component) { 
    console.log('selector: ' + component.selector); 
    console.log('template: ' + component.template); 
    console.log('component init'); 
    return (target: any) => { 
     console.log('component call'); 
     return target; 
    } 
} 

function Directive() { 
    console.log('directive init'); 
    return (target: any) => { 
     console.log('directive call'); 
     return target; 
    } 

} 

@Component({selector: 'person',template: 'person.html'}) 
@Directive() 
class Person {} 

let p = new Person(); 

출력 :

selector: person 
template: person.html 
component init 
directive init 
directive call 
component call 

component calldirective call 전에해야하지?

+2

이 왜 그렇게 생각합니까? 데코레이터는 그 아래에있는 것에 적용되므로 "안쪽에"적용됩니다. – jonrsharpe

+0

어딘가에서 꾸미는 데코레이터는 왼쪽에서 오른쪽으로, 위에서 아래로 – Hao

+0

이것은 직접 https://www.typescriptlang.org/docs/handbook/decorators.html#decorator-composition에서 다루고 있습니다. –

답변

0

데코레이터 : 기본적으로

@C 
@D 
class Person {} 

는 유사 뭔가된다.
데코레이터 자체 가기 반대 방향으로 실행, 하단 : 당신의 예에서

@a @b x 
// bit like 
{ 
    const decA = a 
    const decB = b 
    decA(decB(x)) 
} 

{ 
    const decComp = Component({selector: 'person', template: 'person.html'}) 
    // selector: person 
    // template: person.html 
    // component init 
    const decDirective = Directive() 
    // directive init 
    decComp(decDirective(Person)) 
    // directive call 
    // component call 
} 

Reference

0

지시문 호출 전에 구성 요소 호출을 사용해야합니까?

아니요. 내부는 외부로 호출됩니다. 표현은 위에서 아래로 불러 장식을 생산받을

C(D(class Person())) 
관련 문제