如何使用 bootstrap select 插件与 vuejs 指令和 browserify

How to use bootstrap select plugin with vuejs directive and browserify

我想在 vuejs 项目上使用 bootstrap select 插件,我对如何将它作为指令合并到我的项目中感到困惑。在查看 vuejs 网站中的 select2 示例后,我想到了以下内容。

<span id="el">
  <select class="selectpicker" data-style="btn-primary" v-selectpicker="selected" :options="workflow">
    <option value="0">default</option>
  </select>                                                    
</span>

这是我的指令和视图

require("bootstrap-select");

Vue.directive('selectpicker',{  

    twoWay: true,

    deep: true,

    bind: function() {          
        $(this.el).selectpicker().on("change", function(e) {
            this.set($(this.el).val());
        }.bind(this));
    },
    update: function (value) {
        $(this.el).selectpicker('refresh').trigger("change");
    },
    unbind: function () {
        $(this.el).off().selectpicker('destroy');
    }
});


new Vue({

    el: '#el',

    data: {

        tasksLoaded: false,
        tasks: [],
        workflow: [
            {
                id:'todo',
                value: 'To Do'
            }, 
            {
                id: 'backlog', 
                value: 'Backlog'
            },
            {
                id: 'doing', 
                value: 'Doing'
            },
            {
                id: 'restest', 
                value:'Retest'
            },
            {
                id: 'completed', 
                value: 'Completed'
            },
            {
                id: 'deployed',
                value: 'Deployed'
            }
        ],
        sections:[],
        tests:[],
        sectionId: '',
        testId: ''
    },

    watch: {            
        // watch these particular models for changes and then fire on trigger       
        sectionId: function(value) {                        
            this.getTests(value);                   
        },
        testId: function(value) {
            this.getResults(value);
        }
    }
});

我最终收到错误

Uncaught TypeError: $(...).selectpicker is not a function

并且我已经检查过它是否已经被调用并且它已经存在。

此外,我正在使用 laravel spark,因此它已经通过

在 app.js 文件中定义了 jQuery
require('laravel-spark/core/bootstrap');

你能告诉我们你在哪里包括 jQuery 和 Bootstrap 吗?

require("bootstrap-select"); 为 运行 时,

jquery 必须作为 $ 可用。这是我在我的应用程序中使用它的方式:

window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('bootstrap-select');

需要以正确的顺序调用所有 3 个,否则它们将无法正确地相互扩展