簡體   English   中英

如何在角度6中使用jquery-ui

[英]how to use jquery-ui in angular 6

我已經嘗試在StackOverflow上發布了多個方法,在角度6組件中使用jquery-ui ,但它們都沒有工作。 例如,

  1. 我運行了npm install jquery jquery-ui來安裝jquery和jquery-ui。

  2. 包含在angular.json中

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

另一篇文章建議我不應該使用 angular6的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編譯器導入到$ variable中。

如果你使用

 declare let $: any;

然后你只是通知打字稿這個變量存在。 在這種情況下, $將包含您在angular.json導入的腳本中分配給它的任何內容,即jqueryjquery-ui插件

請更新angular.json文件中的腳本部分

"scripts": [
           "../node_modules/jquery/dist/jquery.min.js",
           "../node_modules/jquery-ui-dist/jquery-ui.js"
        ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM