Jquery不在angular2中工作
我是安圭拉的新手。我曾经这样做过,但当我试图在我的家用电脑上设置相同的环境时,我无法做到这一点 在我的add page.component.ts中,我声明Jquery不在angular2中工作,jquery,angular,Jquery,Angular,我是安圭拉的新手。我曾经这样做过,但当我试图在我的家用电脑上设置相同的环境时,我无法做到这一点 在我的add page.component.ts中,我声明$和jquery如下:- declare var $ : any; declare var jquery : any; 然后在ngAfterViewInit函数中,我运行这个jquery ngAfterViewInit(){ $(function(){ if($('#desc').length>0)
$
和jquery
如下:-
declare var $ : any;
declare var jquery : any;
然后在ngAfterViewInit
函数中,我运行这个jquery
ngAfterViewInit(){
$(function(){
if($('#desc').length>0)
{
$("#desc").Editor();
$(".Editor-editor").html($('#desc').text());
$("#desc").val($('.Editor-editor').html());
}
});
}
但是,jquery不起作用。当我在alert('Hi')
内部ngAfterViewInit()
但在$(document.ready(function(){})
外部编写警报时,我收到了警报。但是$(document.ready(function(){})
中的警报不起作用。因此,这意味着ngAfterViewInit
正在工作,但$(document).ready(function(){})
没有工作
顺便说一下,jquery库添加到index.html文件中:-
<script src="assets/admin-assets/bower_components/jquery/jquery.min.js"></script>
我做错了什么?您不需要这行代码:
declare var jquery : any;
import { ngAfterViewInit } from '@angular/core';
另外,请确保使用以下代码行从angular导入ngAfterViewInit:
declare var jquery : any;
import { ngAfterViewInit } from '@angular/core';
还要确保您的jquery文件存在于components文件夹中,如果您不知道如何使用,请尝试通过将引用更改为
”来使用CDNhttps://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js“
而不是在脚本标记中。我认为您应该使用它来显示控制台中的错误吗?您是否尝试过运行任何其他代码,如alert('ready')代码>,在.ready()
中,如果它能工作,则使用c?是的,我从“@angular/core”导入了{ngAfterViewInit}代码>在我的组件文件中。您是否尝试了jquery的CDN?我也尝试了CDN。问题在于附加外部文件中的jquery代码。解决了这个问题。