Jquery未使用Angular typescript加载
我想在angular项目中添加jquery,但它不起作用。 我尝试了多种解决方案,但不起作用 错误Jquery未使用Angular typescript加载,angular,Angular,我想在angular项目中添加jquery,但它不起作用。 我尝试了多种解决方案,但不起作用 错误 ERROR ReferenceError: $ is not defined at SignInComponent.push../src/app/users/sign-in/sign-in.component.ts.SignInComponent.ngOnInit (sign-in.component.ts:28) at checkAndUpdateDirectiveInline
ERROR ReferenceError: $ is not defined
at SignInComponent.push../src/app/users/sign-in/sign-in.component.ts.SignInComponent.ngOnInit (sign-in.component.ts:28)
at checkAndUpdateDirectiveInline (core.js:22099)
at checkAndUpdateNodeInline (core.js:23363)
at checkAndUpdateNode (core.js:23325)
at debugCheckAndUpdateNode (core.js:23959)
at debugCheckDirectivesFn (core.js:23919)
at Object.eval [as updateDirectives] (SignInComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
at checkAndUpdateView (core.js:23307)
at callViewAction (core.js:23548)
我尝试的
- npm install jquery — save
然后
"scripts": [ "../node_modules/jquery/dist/jquery.min.js" ]
我的组件
import { Component, OnInit,Renderer2, AfterViewInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
declare var $: any;
@Component({
selector: 'app-master',
templateUrl: './master.component.html',
styleUrls: ['./master.component.css']
})
export class MasterComponent implements OnInit {
constructor() { }
ngOnInit() {
console.log('call');
console.log($(document));
}
您可以使用@types/jquery
它可能是angular.json文件中的脚本路径,请通过以下方式进行更改(删除一个点):
感谢这不是一个答案,但我认为如果可能的话,最好避免使用jQuery和Angular。我试过一次,结果弄得一团糟。你有什么特别的需要吗?如果是这样的话,我真的建议尝试找到一个角度的解决方案。这个解决方案应该很好=)@Muhammad u did
ng serve
在将它添加到scripts:[]
?这不是问题的确切答案,但解决了我面临的问题,我安装了jquery,但没有安装类型,安装类型后,可以调用jquery插件函数
npm i @types/jquery
"scripts": [ "./node_modules/jquery/dist/jquery.min.js" ]