我有一个通过CLI(Angular 4)生成的Angular应用程序组件。
我这样引入了jQuery + Bootstrap:
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
"styles.css"
],
"scripts": ["../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
我的应用组件长这样:
import { Component, AfterViewInit } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [
'./app.component.css']
})
export class AppComponent implements AfterViewInit {
title = 'app';
constructor() { }
ngAfterViewInit() {
$('.nav a').click(function () {
$('.navbar-collapse').collapse('hide');
});
}
}
收到以下错误:
ERROR TypeError: __WEBPACK_IMPORTED_MODULE_1_jquery__(...).collapse is not a function
Bootstrap 没有被正确导入吗? 我尝试在我的组件顶部添加以下内容:
import 'bootstrap/dist/js/bootstrap.js';
编辑:
我注意到一件事情。如果我通过Chrome控制台执行以下代码,则可以在浏览器中运行而无需问题:
$('.navbar-collapse').collapse('hide');
这里会有什么不同呢?我一定漏掉了什么。