简体   繁体   English

如何在angular4中使用jQuery

[英]how to use jquery inside angular4

i am new to angular, i am trying to use Jquery with angular 4, i found this question on stack over flow and in side the question i found this example 我是新手,我试图将Jquery与angular 4一起使用,我在堆栈上发现了这个问题 ,而在侧面我发现了这个例子

but when i try to impliment it i get the following error : 但是当我尝试隐含它时,出现以下错误:

vendor.bundle.js:51867 ERROR Error: Uncaught (in promise): TypeError: $(...).chosen is not a function
TypeError: $(...).chosen is not a function
    at PocTestsPage.webpackJsonp.../../../../../src/app/structure/poc-tests/poc-tests.page.ts.PocTestsPage.ngAfterViewInit (main.bundle.js:898)
    at callProviderLifecycles (vendor.bundle.js:61944)
    at callElementProvidersLifecycles (vendor.bundle.js:61919)
    at callLifecycleHooksChildrenFirst (vendor.bundle.js:61903)
    at checkAndUpdateView (vendor.bundle.js:62935)
    at callWithDebugContext (vendor.bundle.js:63989)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (vendor.bundle.js:63529)
    at ViewRef_.webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges (vendor.bundle.js:61001)
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (vendor.bundle.js:83416)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.placeComponentIntoOutlet (vendor.bundle.js:82597)
    at PocTestsPage.webpackJsonp.../../../../../src/app/structure/poc-tests/poc-tests.page.ts.PocTestsPage.ngAfterViewInit (main.bundle.js:898)
    at callProviderLifecycles (vendor.bundle.js:61944)
    at callElementProvidersLifecycles (vendor.bundle.js:61919)
    at callLifecycleHooksChildrenFirst (vendor.bundle.js:61903)
    at checkAndUpdateView (vendor.bundle.js:62935)
    at callWithDebugContext (vendor.bundle.js:63989)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (vendor.bundle.js:63529)
    at ViewRef_.webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges (vendor.bundle.js:61001)
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (vendor.bundle.js:83416)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.placeComponentIntoOutlet (vendor.bundle.js:82597)
    at resolvePromise (polyfills.bundle.js:3179)
    at resolvePromise (polyfills.bundle.js:3150)
    at polyfills.bundle.js:3227
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.bundle.js:2833)
    at Object.onInvokeTask (vendor.bundle.js:54923)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.bundle.js:2832)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (polyfills.bundle.js:2600)
    at drainMicroTaskQueue (polyfills.bundle.js:2993)
    at ZoneTask.invoke (polyfills.bundle.js:2899)
    at timer (polyfills.bundle.js:3949)

i am using an angular 4 library called cleanUI: 我正在使用一个名为cleanUI的Angular 4库:

i created a component called poc-tests as shown in the image : 我创建了一个称为poc-tests的组件,如图所示: 在此处输入图片说明

poc-tests.html: POC-tests.html:

<!DOCTYPE html>
<html>
<head>
  <title>Angular 2 QuickStart</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.min.css">

  <!-- 1. Load libraries -->
  <!-- Polyfill(s) for older browsers -->
  <script src="https://npmcdn.com/core-js/client/shim.min.js"></script>

  <script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
  <script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
  <script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>

  <!-- jQuery and chosen -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.jquery.min.js"></script>


  <!-- 2. Configure SystemJS -->
  <script src="systemjs.config.js"></script>
  <script>
    System.import('app').catch(function(err){ console.error(err); });
  </script>
</head>

<!-- 3. Display the application -->
<body>
<ng-chosen>Loading...</ng-chosen>
</body>
</html>

poc-tests.page.ts: POC-tests.page.ts:

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

declare var $: any;
declare var jQuery: any;
declare var autosize: any;
declare var Ladda: any;
declare var Chartist: any;

@Component({
  selector: 'ng-chosen',
  template: `<select #selectElem>
    <option *ngFor="let item of items" [value]="item" [selected]="item === selectedValue">{{item}} option</option>
  </select>
  <h4> {{selectedValue}}</h4>`
})
export class PocTestsPage implements AfterViewInit {
  @ViewChild('selectElem') el:ElementRef;
  items = ['First', 'Second', 'Third'];
  selectedValue = 'Second';

  ngAfterViewInit() {
    $(this.el.nativeElement)
      .chosen()
      .on('change', (e, args) => {
        this.selectedValue = args.selected;
      });
  }
}

You need to install @types/jquery and remove all the declare var related to jQuery: 您需要安装@ types / jquery并删除所有与jQuery相关的declare var变量:

npm install @types/jquery --save

If you are also using webpack to build static files, you'll need this plugin: 如果您还使用webpack构建静态文件,则需要此插件:

new webpack.ProvidePlugin({
  $: "jquery",
  jQuery: "jquery"
}),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM