简体   繁体   中英

How to use jquery plugin in angular 4

I am trying to use this https://github.com/ShinDarth/Nestable jquery plugin for nested drag and drop feature. I have already installed the jquery from npm install jquery --save and typings from npm install @types/jquery --save-dev

i have declared import * as $ from 'jquery'; in my component and included the plugin file in index.html page.

Here is my component code:

import { Component, OnInit,AfterViewInit,ElementRef } from '@angular/core';
import { MdDialog, MdDialogRef } from "@angular/material";
import { MediaUploadComponent } from '../helper-component/media-upload/media-upload.component';
import * as $ from 'jquery';
// import 'jquery';
// declare const $: JQueryStatic;
// declare var $:any;
// import $ from "jquery";

@Component({
  selector: 'app-appearance',
  templateUrl: './appearance.component.html',
  styleUrls: ['./appearance.component.css']
})
export class AppearanceComponent implements OnInit ,AfterViewInit{
  private selectedOption:string ;
  site_icon:any;
  constructor() { }

  ngOnInit() {
  }
  ngAfterViewInit(){
  // jquery plugin nestable
    $('.nestable').nestable();
  //console.log(jQuery.fn.jquery);
  }
}

After all it throws error : 在此处输入图片说明

How can i fix this problem?

[i have already tried including the jquery link to index page from the cdn but shows the same error]

Index.html

!doctype html>
<html>
<head>
  <meta charset="utf-8">

  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link rel="stylesheet" href="../assets/bootstrap.min.css">
  <!--<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> -->

  <script src="../assets/nestable/jquery.nestable.js"></script>
  <script src="../assets/nestable/jquery.nestable++.js"></script>


</head>

....

Ultimately you'd want to install jquery and other libraries in the angular-cli.json

scripts: [
"../node_modules/jquery/dist/jquery.min.js",
"../assets/nestable/jquery.nestable.js",
"../assets/nestable/jquery.nestable++.js"
]

The changes will only apply once you restart ng serve .

EDIT: For further reading and explanation this article might be of help https://github.com/angular/angular-cli/wiki/stories-global-scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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