如何在 angular 6 中使用 jquery-ui [英] how to use jquery-ui in angular 6

查看:27
本文介绍了如何在 angular 6 中使用 jquery-ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了在 StackOverflow 上发布的许多方法来在 angular 6 组件中使用 jquery-ui,但没有一个有效.例如,

  1. 我运行 npm install jquery jquery-ui 来安装 jquery 和 jquery-ui.

  2. 在 angular.json 中包含以下内容

    脚本":["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 不是函数在 AppComponent.push../src/app/app.component.ts.AppComponent.ngAfterContentInit (http:||localhost:4200/main.js:154:56)在 callProviderLifecycles (http:||localhost:4200/vendor.js:42663:18)在 callElementProvidersLifecycles (http:||localhost:4200/vendor.js:42644:13)在 callLifecycleHooksChildrenFirst (http:||localhost:4200/vendor.js:42634:29)在 checkAndUpdateView (http:||localhost:4200/vendor.js:43565:5)在 callWithDebugContext (http:||localhost:4200/vendor.js:44454:25)在 Object.debugCheckAndUpdateView [作为 checkAndUpdateView] (http:||localhost:4200/vendor.js:44132:12)在 ViewRef_.push../node_modules/@angular/core/fesm5/core.js.ViewRef_.detectChanges (http:||localhost:4200/vendor.js:41948:22)在 http:||本地主机:4200/vendor.js:37684:63在 Array.forEach(本机)

index.html

<html lang="zh-cn"><头><title>汽车经销商</title><身体><app-root></app-root></html>

app.component.html

app.component.ts

import { Component, AfterContentInit } from '@angular/core';import * as $ from 'jquery';@成分({选择器:'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']})导出类 AppComponent 实现 AfterContentInit {标题 = 'MDK';ngAfterContentInit() {$( "#slider" ).slider({范围:真实,值:[ 17, 67 ]});}}

另一篇文章建议我不应该使用 angular 6的angular.json,而是使用index.html来包含脚本,但它也没有用.

我在 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>

解决方案

如果你写

import * as $ from 'jquery';

那么只有 jquery 的代码(而不是额外的插件,比如 jquery-ui)才会被 typescript 编译器导入到 $变量.

如果你使用

 声明 let $: any;

然后你只是通知打字稿这个变量存在.在这种情况下,$ 将包含您在 angular.json 中导入的脚本中分配给它的任何内容,即 jquery AND 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,

  1. I ran npm install jquery jquery-ui to install jquery and jquery-ui.

  2. Included following in angular.json

    "scripts": [ "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

<!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 ]
    });
  }
}

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.

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>

解决方案

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.

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 jquery AND jquery-ui plugins

这篇关于如何在 angular 6 中使用 jquery-ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆