如何在角4中使用jquery插件

Sur*_*nal 0 javascript jquery angular

我试图使用这个https://github.com/ShinDarth/Nestable jquery插件来嵌套拖放功能.我已经安装了jquery npm install jquery --save 和来自的npm install @types/jquery --save-dev

import * as $ from 'jquery';在我的组件中声明并在index.html页面中包含插件文件.

这是我的组件代码:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

毕竟它抛出错误: 在此输入图像描述

我该如何解决这个问题?

[我已经尝试将jquery链接包含在cdn的索引页面中,但显示相同的错误]

的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>
Run Code Online (Sandbox Code Playgroud)

....

Ced*_*Ced 7

最终你想安装jquery和其他库angular-cli.json

scripts: [
"../node_modules/jquery/dist/jquery.min.js",
"../assets/nestable/jquery.nestable.js",
"../assets/nestable/jquery.nestable++.js"
]
Run Code Online (Sandbox Code Playgroud)

更改仅在您重新启动后才适用ng serve.

编辑:有关进一步阅读和解释,这篇文章可能会有所帮助https://github.com/angular/angular-cli/wiki/stories-global-scripts