【问题标题】:Loading bootstrap selectpicker in angular2在angular2中加载引导选择器
【发布时间】:2018-01-07 18:49:06
【问题描述】:

我正在尝试在我的 angular2 应用程序中使用 bootstrap selectpicker。我已经在 angular-cli.json 中加载了 jquery 和 bootstrap-select 脚本。

这是我的 app-component.ts

 import $ from 'jquery';
  ngOnInit() {
   $('.selectpicker').selectpicker('refresh');
  }

但是,我收到了这个错误。

__WEBPACK_IMPORTED_MODULE_2_jquery___default(...)(...).selectpicker 不是函数。

我是 angular2 的新手。请帮忙!

【问题讨论】:

  • 您仍然需要 bootstrap.js 用于引导的选择器
  • "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", "../node_modules/font-awesome/css/font-awesome.min. css”、“../node_modules/typeface-montserrat/index.css”、“../node_modules/bootstrap-select/dist/css/bootstrap-select.min.css”、“styles.css”]、“脚本": ["../node_modules/jquery/dist/jquery.min.js", "../node_modules/bootstrap/dist/js/bootstrap.min.js", "../node_modules/bootstrap-select/dist /js/bootstrap-select.min.js" ]
  • 我已将所有这些文件包含在 angular-cli.json 中

标签: angular bootstrap-select bootstrap-selectpicker


【解决方案1】:

这是我的解决方案,

在 angular-cli 中打开 typings.d.ts 文件并添加以下行

interface JQuery {
   addClass(className: string): JQuery;
   attr(attributeName: string, value: string|number): JQuery;
}
interface JQuery {
     selectpicker(options?: any, callback?: Function):any;
}

不要导入 jquery:

declare var $:any;

【讨论】:

  • 谢谢。这行得通。但是你能解释一下这里到底发生了什么吗?
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2017-08-21
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多