错误 TS2304:在 angular 5 组件中找不到 $ [英] error TS2304: cannot find $ in angular 5 component
问题描述
我在角度组件中有一个侧导航栏,它在点击时展开类似于 this.用于创建导航栏的 HTML 片段是:
HTML:
顾名思义(openNav()
),以下HTML代码将扩展导航栏:
<img (click)="openNav()" src="/imagepath" id="image">
以下 HTML 代码将关闭导航栏:
打字稿:
使用的打字稿是:
openNav(){$("#mySidenav").css("width", "50%");//A线}关闭导航(){$("#mySidenav").css("width", "0%");//B行}
ngOnInit()
函数中不包含上述打字稿代码.
问题说明:
只要我在命令提示符下执行 ng serve
,我就会收到以下错误:
错误 TS2304:找不到名称$".
$ 代表打字稿中openNav() 和closeNav() 函数中 内的行(上面打字稿中的A 行和B 行).
我想知道该错误背后的主要原因是什么因为有时我会在执行 ng-serve 时收到错误,有时我不会.
我假设 $
是 jQuery
.您必须将 jQuery
添加到您的依赖项并将其导入到您使用它的文件中.我还建议为 jQuery 添加类型定义:npm install @types/jquery --save-dev
如果您以任何其他方式导入 jQuery,则只需添加类型即可.
I have a side-navbar in angular component which expands on click similar to this. The HTML snippet used to create the navbar is :
HTML:
As the name(openNav()
) suggests, the following HTML code will expand the navbar:
<div>
<img (click)="openNav()" src="/imagepath" id="image">
</div>
And the following HTML code will close the navbar:
<div id="mySidenav" class="sidenav">
<ul class="navbar-nav mr-auto" id="sidenavContent">
<li class="nav-item" id="close-btn">
<img class="closebtn" (click)="closeNav()" id="white-cross" src="/assets/imagepath">
</li>
<li class="nav-item" id="close-btn">
<p> Item 1 </p>
</li>
<li class="nav-item">
<p> Item 2 </p>
</li>
</ul>
</div>
Typescript:
The typescript used is:
openNav()
{
$("#mySidenav").css("width", "50%"); // Line A
}
closeNav()
{
$("#mySidenav").css("width", "0%"); // Line B
}
The above typescript code is not included in the ngOnInit()
function.
Problem Statement :
As soon as I do ng serve
on my command prompt, I get the following error:
error TS2304: Cannot find name '$'.
The $ represents the lines (Line A and Line B in the typescript above) inside openNav() and closeNav() functions above in the typescript.
I am wondering what are the primary reasons behind that error as sometimes I get the error and sometimes I don't while doing ng-serve.
I assume that $
is jQuery
. You must add jQuery
to your dependencies and imported it in the file where you're using it. I'd suggest also adding the type definitions for jQuery: npm install @types/jquery --save-dev
If you're importing jQuery in any other way, then just add the types.
这篇关于错误 TS2304:在 angular 5 组件中找不到 $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!