angular 5 bootstrap-select:selectpicker 不是一个函数

ael*_*ael 5 bootstrap-select bootstrap-4 angular5

我想为 angular 5 应用程序安装npm bootstrap-select所以我使用 npm 安装了它

npm install bootstrap-select
Run Code Online (Sandbox Code Playgroud)

然后我安装了所需的依赖项:

npm install jquery
npm install popper.js --save
Run Code Online (Sandbox Code Playgroud)

角度cli.json:

 "apps": [
    {
      "styles": [
        "styles.css"
      ],
      "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ],
    }
  ],
Run Code Online (Sandbox Code Playgroud)

我的 package.json 是(我从列表中删除了一些包):

"dependencies": { 
    (...)
    "@ng-bootstrap/ng-bootstrap": "^1.1.0",
    "bootstrap": "^4.1.1",
    "bootstrap-select": "^1.13.2",
    "jquery": "^3.3.1",
    "ng-multiselect-dropdown": "^0.2.1",
    "popper.js": "^1.14.4",
}
Run Code Online (Sandbox Code Playgroud)

在 html 文件中,我添加了代码:

<ng-template #modalWindow let-c="close" let-d="dismiss">
  <div class="modal-header">
    <h4 class="modal-title">Options</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>

  <div class="modal-body">     
    <select id="myspID" #selectPickerRef class="selectpicker form-control" multiple
        title="Select a number">
      <option>Opt1</option>
      <option>Opt2</option>
      <option>Opt3</option>
    </select>
  </div>

  <div class="modal-footer">
    <button type="button" class="btn btn-light" (click)="c('Close click')">Close</button>
  </div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

添加的 select 元素没有样式,它看起来像一个带有列出选项的简单边框矩形。它甚至不是下拉菜单。

这是一个角度组件元素:

import { Component, OnInit, ViewChild } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import * as $ from 'jquery';

@Component({
  selector: 'app-modal-model-generator',
  templateUrl: './modal-model-generator.component.html',
  styleUrls: ['./modal-model-generator.component.css']
})
export class ModalModelGeneratorComponent implements OnInit {

  closeResult: string;

  @ViewChild('modalWindow')
  modalWindowRef: HTMLElement;

  @ViewChild('selectPickerRef')
  selectPickerEL: any;

  constructor(
    private modalService: NgbModal
  ) {}

  runBootstrapSelects(): void {
    //this.selectPickerEL.selectpicker();
    //$('.selectpicker').selectpicker();
    //$('select').selectpicker();
    $('#myspID').selectpicker();  //<--error is thrown here
  }

  openWindowCustomClass(params) {

    this.modalService.open(this.modalWindowRef, {
      windowClass: 'dark-modal',
      centered: true,
      size: 'lg'
    });
    this.runBootstrapSelects();
  }
}
Run Code Online (Sandbox Code Playgroud)

有一个错误:

$(...).selectpicker is not a function
Run Code Online (Sandbox Code Playgroud)

内部runBootstrapSelects()组件方法。

编辑:
已添加 angular-cli.json 和 angular 组件文件内容。

如果我添加:@import "~bootstrap-select/dist/css/bootstrap-select.css"; 到 style.css 文件:

@import "~bootstrap/dist/css/bootstrap.css";
@import "~bootstrap-select/dist/css/bootstrap-select.css";
Run Code Online (Sandbox Code Playgroud)

然后<select>项目未显示在页面上

但是文件:/node_modules/bootstrap-select/tests/bootstrap4.html包含在 bootstrap-select node_modules 文件夹中。Bootstrap4.html 文件包含显示样式的 bootstrap-select 下拉列表。

小智 1

是的,因为它仅在您使用 jquery 条件调用该插件类“ selectpicker ”时才有效。确保您在 jquery 条件中调用该元素类名称或元素名称。

// 使用 selectpicker 类仅设置选择样式

$('.selectpicker').selectpicker();

          (or)
Run Code Online (Sandbox Code Playgroud)

// 设置所有选择的样式

$('select').selectpicker();