【问题标题】:How to load jQuery scripts in every component?如何在每个组件中加载 jQuery 脚本?
【发布时间】:2017-06-19 06:22:11
【问题描述】:

我正在使用 Angular2 和 Limitless 布局构建单页应用程序。 但是,主题中有一个 app.js 文件,其中包含所有 jQuery 功能。 我已经在 index.html 中加载了它,但是 jQuery 仅在我刷新页面时才起作用。

如何修改app.js,让里面的jQuery函数在每个组件中运行?

【问题讨论】:

  • 你在使用 angular-cli 吗?
  • @YoavSchniederman 是的,我是
  • 所以,首先使用 cmd 在根目录运行 npm install --save jquery。比您需要在 angular-cli.json 添加脚本数组的 jquery 路径:“scripts”:[“../node_modules/jquery/dist/jquery.js ] 并且您可以使用“declare var $:any;”在你的 ts 文件中

标签: jquery angular


【解决方案1】:

在组件装饰器之前,使用

// You can use $ to refer to jQuery inside the code
declare var $ : any;

@Component({
  selector: 'my-selector',
  providers: []
})

【讨论】:

    【解决方案2】:

    app.js 中创建函数,即。

    function executeEveryCompo(){
          console.log('Something');
    }
    

    并在每个组件中添加AfterviewInit

    ngAfterviewInit(){
         executeEveryCompo();
    }
    

    不要忘记在组件顶部添加declare var executeEveryCompo:any;

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2016-04-21
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2016-03-14
      • 2016-06-14
      相关资源
      最近更新 更多