javascript 如何在 angular 6 中使用 jquery-ui
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52421917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use jquery-ui in angular 6
提问by Syed Rafey Husain
I have tried number of methods posted on StackOverflow to use jquery-uiin angular 6component but none of them worked. For example,
我已经尝试了在 StackOverflow 上发布的许多方法来在angular 6组件中使用jquery-ui,但它们都不起作用。例如,
I ran npm install jquery jquery-uito install jquery and jquery-ui.
Included following in angular.json
"scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/jquery-ui-dist/jquery-ui.js",
我运行npm install jquery jquery-ui来安装 jquery 和 jquery-ui。
在 angular.json 中包含以下内容
"脚本": [ "node_modules/jquery/dist/jquery.js", "node_modules/jquery-ui-dist/jquery-ui.js",
Error is as follows:
错误如下:
AppComponent_Host.ngfactory.js [sm]:1ERROR TypeError: jquery__WEBPACK_IMPORTED_MODULE_1__(...).slider is not a function
at AppComponent.push../src/app/app.component.ts.AppComponent.ngAfterContentInit (http:||localhost:4200/main.js:154:56)
at callProviderLifecycles (http:||localhost:4200/vendor.js:42663:18)
at callElementProvidersLifecycles (http:||localhost:4200/vendor.js:42644:13)
at callLifecycleHooksChildrenFirst (http:||localhost:4200/vendor.js:42634:29)
at checkAndUpdateView (http:||localhost:4200/vendor.js:43565:5)
at callWithDebugContext (http:||localhost:4200/vendor.js:44454:25)
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http:||localhost:4200/vendor.js:44132:12)
at ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (http:||localhost:4200/vendor.js:41948:22)
at http:||localhost:4200/vendor.js:37684:63
at Array.forEach (native)
index.html
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Car Dealer</title>
</head>
<body>
<app-root></app-root>
</body>
</html>
app.component.html
应用程序组件.html
<div id="slider">
</div>
app.component.ts
app.component.ts
import { Component, AfterContentInit } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterContentInit {
title = 'MDK';
ngAfterContentInit() {
$( "#slider" ).slider({
range: true,
values: [ 17, 67 ]
});
}
}
Another post suggested that I should not useangular.json of angular 6 at all but use index.html to include scripts but it also did not worked.
另一篇文章建议我根本不应该使用angular 6 的 angular.json 而是使用 index.html 来包含脚本,但它也没有奏效。
I included following in index.html but even then same error appeared
我在 index.html 中包含了以下内容,但即使如此也出现了同样的错误
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
回答by David
If you write
如果你写
import * as $ from 'jquery';
then only the code for jquery(and not extra plugins, like jquery-ui) will be imported by typescript compiler into the $variable.
那么只有jquery(而不是额外的插件,如jquery-ui)的代码才会被 typescript 编译器导入到$变量中。
If you use
如果你使用
declare let $: any;
Then you are just notifying typescript that this variable exist. In that case, $will contain whatever what assigned to it in the scripts you imported in angular.json, which is jqueryAND jquery-uiplugins
然后你只是通知打字稿这个变量存在。在这种情况下,$将包含您导入的脚本中分配给它的任何内容angular.json,即jqueryANDjquery-ui插件
回答by Omprakash Amarwal
Please update your scriptspart in angular.jsonfile
请更新angular.json文件中的脚本部分
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/jquery-ui-dist/jquery-ui.js"
]

