【问题标题】:Property 'ripples' does not exist on type 'JQuery<HTMLElement>'. in angular component类型“JQuery<HTMLElement>”上不存在属性“涟漪”。在角度分量中
【发布时间】:2020-02-11 13:49:51
【问题描述】:

我正在尝试在 Angular 上实现 jquery 函数,但终端返回以下错误:

类型“JQuery”上不存在属性“涟漪”

在我的控制台上出现以下错误:ERROR ReferenceError: $ is not defined

这是我的代码 sn-p

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import 'jqueryui';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent implements OnInit {
  constructor() { }

  ngOnInit() {
    $(document).ready(function() {
      $('.image').ripples('show');
    });
  }
}

【问题讨论】:

  • ripples() 不是标准的 jQuery 方法。您需要将包含它的库包含到页面中
  • 我安装了 npm i jquery.ripples
  • 您还需要将它包含在页面的脚本中。
  • @phuzi 是的,我做到了“样式”:[“node_modules/jquery.ripples/dist/jquery.ripples-min.js”]
  • 不确定 styles 对于通常包含为 &lt;script&gt; 的内容是否正确

标签: jquery angular


【解决方案1】:

你必须声明 $ 如下,

declare var $; 

而不是

import * as $ from 'jquery';

更新
更新您的 angular.json 文件

"scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              //Here ripple module path
            ],

这将解决您的错误 -> $ 未声明 以及 ripple

【讨论】:

  • 你导入jquery的CDN了吗?
  • 是的,在我的 angular.json 文件中:“styles”:[“node_modules/jquery/dist/jquery.min.js”,“node_modules/bootstrap/dist/css/bootstrap.min.css” , "node_modules/jquery.ripples/dist/jquery.ripples-min.js" ]
  • 你需要在scripts数组中导入.js文件...而不是样式..纠正它,它会工作。
猜你喜欢
  • 2018-06-08
  • 1970-01-01
  • 2018-03-08
  • 2020-01-17
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2020-02-16
  • 2020-04-08
相关资源
最近更新 更多