如何在角度6中使用jquery-ui [英] how to use jquery-ui in angular 6
问题描述
我已经尝试在StackOverflow上发布了一些方法,以便在 angular 6 组件中使用 jquery-ui ,但这些方法都不起作用。例如,
I have tried number of methods posted on StackOverflow to use jquery-ui in angular 6 component but none of them worked. For example,
-
我运行 npm install jquery jquery-ui 来安装jquery和jquery-ui 。
I ran npm install jquery jquery-ui to install jquery and jquery-ui.
在angular.json中包含以下内容
Included following in angular.json
脚本:[
node_modules / jquery / dist / jquery.js,
node_modules / jquery-ui-dist / jquery-ui.js,
"scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/jquery-ui-dist/jquery-ui.js",
错误如下:
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Car Dealer</title>
</head>
<body>
<app-root></app-root>
</body>
</html>
app.component.html
<div id="slider">
</div>
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 ]
});
}
}
另一篇帖子暗示我不应该使用 angular.json的角度为6,但是使用index.html来包含脚本,但它也没有用。
Another post suggested that I should not use angular.json of angular 6 at all but use index.html to include scripts but it also did not worked.
我在index.html中包含了以下但是即使这样出现同样的错误
I included following in index.html but even then same error appeared
<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>
推荐答案
如果你写的话
import * as $ from 'jquery';
然后只有 jquery
的代码(和没有额外的插件,如 jquery-ui
)将由typescript编译器导入 $
变量。
then only the code for jquery
(and not extra plugins, like jquery-ui
) will be imported by typescript compiler into the $
variable.
如果您使用
declare let $: any;
然后你只是通知打字稿这个变量存在。在这种情况下, $
将包含您在 angular.json
中导入的脚本中分配给它的内容,这是 jquery
AND jquery-ui
插件
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 jquery
AND jquery-ui
plugins
这篇关于如何在角度6中使用jquery-ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!