如何让指令被消费

How to get a directive consumed

使用 Browserify 从 TypeScript 编译...我正在编写一个 UDM,将新的 DOM 元素动态注入页面...我想对它们应用 NG2 @Directive(),但不是来自代码中其他任何地方的 "called",编译过程会绕过指令的 import

如有任何建议,我们将不胜感激。

如果要将指令应用于组件中的元素,则需要在其 directives 属性中指定它们:

import { SomeDirective } from './some.directive';

@Component({
  (...)
  directives: [ SomeDirective ]
})
export class SomeComponent {
  (...)
}

在这种情况下,编译期间不会绕过导入。如果不在属性中指定指令,就会这样...