来自脚本的Angular 2外部指令



你好,我有一个工作良好的angular 2项目。现在我想包括一个外部指令从js我通过一个脚本标签加载即https://test.com/testDirectiveLoad.js这个js包含一个指令。当我把脚本和指令在index.html文件的指令工作。但我想包括指令在我的组件之一,例如headerComponent.ts。如果我把指令放在那个组件中,浏览器会显示一个错误:

Template parse errors:
'test' is not a known element:
1. If 'test' is an Angular component, then verify that it is part of this module.
2. If 'test' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. 

谁能帮帮我

如果没有误解你的问题,我认为你需要导入你的外部javascript到你的组件,如下面的例子:

declare let createTable: any

然后在导出类中:

ngOnInit() {
    this.service.getAll().then(() => createTable('#elementName', this.tConfig(), this))
}
tConfig() { ... }
在javascript:

function createTable(elementID, options, delegate) {...}

我通过在我的app.module.ts中添加模式:[CUSTOM_ELEMENTS_SCHEMA]来解决这个问题,详细信息请查看https://stackoverflow.com/a/39517874/6039002

最新更新