jquery - 如何使用带有 vuejs 指令和 browserify 的 bootstrap select 插件

标签 jquery vue.js

我想在 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>

这是我的指令和 View

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 个,否则它们将无法正确地相互扩展

关于jquery - 如何使用带有 vuejs 指令和 browserify 的 bootstrap select 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418299/

相关文章:

javascript - js,mysql,php - 通过 while 循环获取 ajax 请求中的特定结果

javascript - jQuery/CSS : selected amount of stars to light up

php - 从数据库获取值并将它们设置在数组中 1tomany

vue.js - Vue + Laravel 5.4 + 混合 - "Cannot Get/"

css - 如何根据同胞子属性更改样式

javascript - 函数闭包中的另一个 jQuery 版本

javascript - iframe (firefox) 的 jQuery 滚动失败

vue.js - "unsupported type error"带有用于 .HDR 文件类型的 RGBE 加载器

javascript - 如何使用多个观察者过滤 VueJS 中的列表

javascript - 如何搜索嵌套级别的 JSON 数组