“$ is not defined at external.js”翻译为中文为:”$ 在 external.js 中未定义”。

huangapple go评论67阅读模式
英文:

$ is not defined at external.js

问题

我已将外部 JS 文件添加到我的 Angular 14 解决方案中。JS 文件中有 $('.class').click() 事件。每当我在本地主机上运行应用程序时,浏览器会显示 $ 未定义。我对 Angular 不熟悉,我的问题是如何正确定义 $

我尝试在 JS 文件的第一行添加 var $ = require('jquery'),但它产生新的错误,说 require 未定义。

英文:

I have added an external js file to my angular14 solution. The JS file has $('.class').click() event. Whenever I run the application in localhost, the browser says $ is not defined. I am new to angular and my question is how can I properly define $?

I tried adding var $ = require('jquery') in the first line of js file but it produces new error saying require is not defined.

答案1

得分: 0

看起来你正在尝试在你的Angular应用中使用jQuery。

$jQuery 的缩写。

当你开发Angular应用时,通常不需要同时使用jQuery库。但如果在你的应用中仍然需要与Angular一起使用jQuery的情况,可以查看这个帖子:
https://stackoverflow.com/questions/30623825/how-to-use-jquery-with-angular

英文:

It seems you are trying to use jQuery in your Angular application.

$ is a shortcut for jQuery.

When you are developing an Angular application, there usually is no need to use also jQuery library. But if there still is a case for using jQuery in your app alongside Angular, have a look at this post:
https://stackoverflow.com/questions/30623825/how-to-use-jquery-with-angular

答案2

得分: 0

谢谢您的回复,所以我找出了我犯的错误。我成功地将我的外部JS包含到angular.json中的脚本中。这应该足够了,然而,我还在index.html中放置了<script src=""></script>。我刚刚将它删除了,错误消失了。我正在做一种表单滑块,我需要JS来处理动画。

英文:

Thanks for your replies, so I found out the mistake I did. I was able to include my external JS to scripts in angular.json. That should be enough however, I also placed <script src=""></script> in the index.html. I just removed it and the error is gone. I was doing a some sort of Form Slider and I need the js to handle the animations for me.

huangapple
  • 本文由 发表于 2023年5月18日 01:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定