【问题标题】:How to use bootstrap select plugin with vuejs directive and browserify如何使用带有 vuejs 指令和 browserify 的引导选择插件
【发布时间】:2016-05-26 21:22:13
【问题描述】:

我想在 vuejs 项目上使用引导选择插件,但我对如何将它作为指令合并到我的项目中感到困惑。在查看 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 vue.js


    【解决方案1】:

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

    require("bootstrap-select"); 运行时,jquery 必须作为$ 可用。这是我在我的应用中使用它的方式:

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

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

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 2014-11-21
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多