【问题标题】:Select2 and Vue DirectiveSelect2 和 Vue 指令
【发布时间】:2016-08-16 23:40:18
【问题描述】:

我通过将 jQuery select2 插件包装在自定义指令中来集成它。

export default {

    data() {
        return {
            selected: null,
            options: []
        }
    },

    ready() {
    this.fetchUsers()
}

    methods: {
        fetchUsers: function () {
            this.$http({
                url: '/api/users/get-all',
                method: 'GET'
            }).then(function (response) {
                this.$set('options', response.data)
            }, function (response) {
                // error callback
            });
        }
    },

    directives: {
        select: {
            twoWay: true,
            priority: 1000,

            params: ['options'],

            bind: function () {
                var self = this;

                $(this.el)
                        .select2({
                            theme: "bootstrap",
                            closeOnSelect: true,
                            minimumInputLength: 3,
                            data: this.params.options
                        })
                        .on('change', function () {
                            self.set(this.value)
                        })
            },
            update: function (value) {
                $(this.el).val(value).trigger('change')
            },
            unbind: function () {
                $(this.el).off().select2('destroy')
            }
        }
    }

}

和 HTML:

<select v-select="selected" :options="options"></select>

我尝试获取所有用户并在 fetchUsers 函数中设置选项,但似乎 fetchUsers 函数是在 select2 指令之后执行的,因此选项变量始终为空。

那么我怎样才能通过 ajax (vue-resource) 获取所有用户,然后填充到 select2?

【问题讨论】:

    标签: javascript jquery vue.js select2 vue-resource


    【解决方案1】:

    我在 select2 上遇到了类似的问题,不得不使用 paramwatchers 来检查选项何时更新:

    select: {
        twoWay: true,
        priority: 1000,
    
        params: ['options'],
    
        paramswatchers: {
             "options": function (val, oldVal) {
                 $(this.el).select2({
                     data: val
                 })
             }
        }
    
        bind: function () {
            var self = this;
    
            $(this.el).select2({
                theme: "bootstrap",
                closeOnSelect: true,
                minimumInputLength: 3,
                data: this.params.options
            })
            .on('change', function () {
                self.set(this.value)
            })
        },
        update: function (value) {
            $(this.el).val(value).trigger('change')
        },
        unbind: function () {
            $(this.el).off().select2('destroy')
        }
    }
    

    来源:https://vuejs.org/guide/custom-directive.html#params

    【讨论】:

    • 非常感谢 :D :D。但是 paramsWatchers 更准确地说是 :D 并且我需要像文档一样将 :options 更改为 v-bind:options 吗?
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2019-04-02
    • 2015-06-21
    • 2022-10-13
    • 2019-05-29
    • 2017-08-22
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多