2017-08-03 80 views
2

我已經嘗試運行以下npm安裝,但它不能解決任何問題。 NPM安裝的jquery --save NPM安裝@類型/ jquery的--save-dev的jQuery不能在角4中工作

我已經加入 「腳本」:[ 「../node_modules/jquery/dist/jquery.min.js」],在.angular-cli.json文件。

app.component.ts

import { Component } from '@angular/core'; 
declare var $:any; 


@Component({ 
selector: 'app-root', 
templateUrl: './app.component.html', 
styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 

title = 'jQuery works!'; 


     onToggle(){ 

$('.test').slideToggle(); 

     } 

} 

錯誤:$未定義

+1

難道你不需要從'jquery'導入$; https://stackoverflow.com/questions/39511788/how-to-import-jquery-to-angular2-typescrypt-projects – JGFMK

+0

你使用引導? – Sreemat

+0

我前幾天幫過別人。請檢查鏈接。 https://stackoverflow.com/questions/45440350/angular-how-to-load-external-library-from-bower-not-available-in-npm/45441017#45441017 – vistajess

回答

1

你嘗試進口jquery的這樣嗎?

import * as $ from 'jquery';