如何在Angular 2指令中使用templateUrl

max*_*max 4 angular2-directives

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

从'@ angular/core'导入{Directive};

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

导出类MasterSideBar {

constructor(){

console.log('Hello MasterSideBar Directive');   
Run Code Online (Sandbox Code Playgroud)

}

}

我收到以下错误:

类型'{selector:string; templateUrl:string; }'不能赋值给'Directive'类型的参数.对象文字只能指定已知属性,'templateUrl'在类型'Directive'中不存在.

Jay*_*der 6

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

Component是唯一接受模板为HTML的指令.

请检查指令类型

希望能帮助到你!!