【发布时间】:2017-11-14 01:45:24
【问题描述】:
我得到了一个指令,它在构造函数中注入了它的父指令(相同类型)。它正在使用“SkipSelf”,因此它不会获得在它所在的组件上定义的指令(但在父级的树上)。
@Directive({
selector: '[myDirective]'
})
export class MyDirective {
constructor(@SkipSelf() @Optional() public parent?: MyDirective) {
console.log('Directive: This is my parent: ' + this.parent);
}
}
此外,我得到了一个注入相同指令但正在查看自身的组件(因此这里没有使用 SkipSelf)。
import {Component, Optional} from '@angular/core';
import {MyDirective} from '../directives/my-directive.directive';
@Component({
selector: 'my-component',
template: './my-component.html'
})
export class MyComponent {
constructor(@Optional() public parent?: MyDirective) {
console.log('Component: This is my parent: ' + this.parent);
}
}
example-html 可能如下所示:
<div myDirective> <!-- (Directive A) does not have a parent (of course) -->
<div>
<div myDirective> <!-- (Directive B) gets the parent (Directive A) -->
<my-component myDirective>
<!-- (Directive C) myDirective gets the parent (Directive B) -->
<!-- myComponent gets the parent directive (Directive C) -->
</my-component>
</div>
</div>
</div>
当我使用 angular-cli (ng serve)“正常”编译时,一切都按预期工作。每个指令都获得对其父级的引用(如果有的话),每个组件实例都获得对其上定义的指令的引用。
但现在我在使用 aot 选项 (ng serve --aot) 进行编译时遇到了问题。指令仍然得到它们的父级,但 component 没有。 (而不是this.parent 是null)
有人知道可能是什么问题吗?会不会是 angular-cli 的错误?
我正在使用以下版本:
@angular/cli: 1.0.0
node: 7.6.0
os: win32 x64
@angular/...: 4.0.1
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.1
【问题讨论】:
标签: angular angular-cli