angular2-directives - 如何在 Angular 2 指令中使用 templateUrl

标签 angular2-directives

如何将 html 模板添加到指令中?这是一个不起作用的示例代码:

import { Directive } from '@angular/core';

@Directive({
selector: '[master-side-bar]',
templateUrl : 'master-side-bar.html' })

export class MasterSideBar {

constructor() {

console.log('Hello MasterSideBar Directive');   

}

}



我收到以下错误:

Argument of type '{ selector: string; templateUrl: string; }' is not assignable to parameter of type 'Directive'. Object literal may only specify known properties, and 'templateUrl' does not exist in type 'Directive'.

最佳答案

从您的代码中,您需要使用 组件 不是指令。

组件是唯一接受模板为 HTML 的指令。

请查收 Types of directives

希望能帮助到你!!

关于angular2-directives - 如何在 Angular 2 指令中使用 templateUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44219553/

相关文章:

html - [null] 中未定义的错误 _renderer.setElementStyle "Cannot set property ' 背景颜色'

javascript - 如何从组件循环向主要父级发出事件 angular 5

angular - 错误 : No providers for Component

javascript - Angular 2 表达式解析器和 ng-init 指令

javascript - 多次出现相同的自定义指令Angular2

javascript - Angular 下拉导航

Angular *ngIf 内存使用

forms - Angular2 - 访问验证指令中的 AbstractControl 实例

javascript - Angular 4 ngFor

angular - 组件渲染后如何从指令中调用函数?