【问题标题】:Angular 2 external directive from script来自脚本的 Angular 2 外部指令
【发布时间】:2017-02-14 23:30:51
【问题描述】:

您好,我有一个 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. 

谁能帮帮我

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    如果没有误解您的问题,我认为您需要将外部 javascript 导入到您的组件中,如下例所示:

    declare let createTable: any
    

    然后在您的导出类中:

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

    在javascript中:

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

    【讨论】:

    • 您正在使用 ts 代码创建组件,但我的问题是我只想在组件 html 文件中使用指令。即
    【解决方案2】:

    我通过向我的 app.module.ts 添加架构来解决它:[ CUSTOM_ELEMENTS_SCHEMA ] 查看https://stackoverflow.com/a/39517874/6039002 了解详情

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 2018-07-06
      • 2018-12-15
      • 2012-12-06
      • 2016-12-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多