【发布时间】:2017-05-31 10:19:30
【问题描述】:
美好的一天,请查看this bin。它是用 Vue 0.12 版本和chosen js 编写的。我怎样才能使它与 vue2 版本一起工作。我真的需要它作为指令而不是组件。
`<div id='search`-results'>
Vue model value <br>
{{city | json}}
<hr>
Select value:
<!-- note the `v-model` and argument for `v-chosen` -->
<select class="cs-select" v-model="city" options="cities" v-chosen="city"></select>
<select v-model="city" options="cities"></select>
Vue.directive('chosen', {
twoWay: true, // note the two-way binding
bind: function () {
$(this.el)
.chosen({
inherit_select_classes: true,
width: '30%',
disable_search_threshold: 999
})
.change(function(ev) {
this.set(this.el.value);
}.bind(this));
},
update: function(nv, ov) {
// note that we have to notify chosen about update
$(this.el).trigger("chosen:updated");
}
});
var vm = new Vue({
data: {
city: 'Toronto',
cities: [{text: 'Toronto', value: 'Toronto'},
{text: 'Orleans', value: 'Orleans'}]
}
}).$mount("#search-results");
【问题讨论】:
-
我认为“我需要这个作为 Vue2 中的指令”等同于“我需要有人帮助我以错误的方式做这件事”。 Vue2 中的指令没有双向绑定。
-
我想将它保留在指令中的原因是我在选择元素上有很多属性,我不想传递给组件。
-
那么您可能问错了问题。可能“我怎样才能合理地将所有这些属性传递给包装器组件”是您正在寻找的。span>
-
感谢您的帖子。 Vue.js 官方文档也有一个类似的 jQuery 插件示例组件包装器:select2 vuejs.org/v2/examples/select2.html