如何在angular 7上添加第三方插件jquery?

时间:2019-04-03 12:26:27

标签: jquery angular angular7

嗨,我一直在尝试在我的App Angular 7上添加第三方jquery插件。到目前为止,我已经完成了:在angular.json中添加了如下文件的位置:

"scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "src/assets/js/ticker/jquery.easy-ticker.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
 ]

在我的AppComponent中:

declare var $: any; interface JQuery { easyTicker(options?: any):
JQuery; }

尝试执行:

 ngAfterViewInit() {

    $('.ultimasExecucoes').easyTicker({
      visible: 1,
      interval: 4000
    });
}

在我的模板中

<div class="ultimasExecucoes">
            <ul>
            <li *ngFor="let workFlow of execucoesEncerradas"> 
                {{workFlow.nomeWorkFlow}} - Data Início: {{workFlow.dataInicioWf | date : 'dd/MM/yyyy HH:mm:ss'}} 
                - Data Encerramento: {{workFlow.dataEncerramentoWf | date : 'dd/MM/yyyy HH:mm:ss'}} 
                - Resultado: {{workFlow.resultadoExecucao}}
            </li>
            </ul>
        </div>

但是我的页面没有任何反应。如何添加插件jquery?

1 个答案:

答案 0 :(得分:0)

您好,我读了许多有关jquery类型的文章后,就这样解决了这个问题:

  1. 首先,您需要安装jquery / types:npm install --save @ types / jquery
  2. 在目录nodes_modules / @ types内部,我创建了一个文件夹“ easyticker”(插件名称)
  3. 在目录“ easyticker”中创建了文件:index.d.ts,放在其中:
  

声明模块“ easyTicker”

     

接口JQuery {easyTicker(options :: any):JQuery; }

  1. 在我的组件上,我导入了jquery:
  

从'jquery'导入*作为jQuery

  1. 然后我给插件打电话:
  

ngAfterViewInit(){

  /* JqueryEasyTicker */
  (<any>$)( document ).ready(function() {
      (<any>$)('#ultimasExecucoes').easyTicker({
        visible: 1,
        interval: 8000
      });
  });   
 }
相关问题