【发布时间】:2019-10-27 04:22:46
【问题描述】:
想象一个带有 Vuex 的 Vuejs 应用程序。我想要一个多选下拉菜单。 Vue 组件中的基本示例:
<template>
<div>
<multiselect
v-model="values"
:options="options">
</multiselect>
</div>
</template>
<script>
import Multiselect from 'vue-multiselect'
export default {
components: { Multiselect },
data () {
return {
values: null,
options: ['bar', 'foo', 'hello','test']
}
}
}
</script>
<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>
现在,如果我在这样的数据中初始化“值”:
values: ["test"]
多选下拉菜单已选择“测试”选项。我也可以在单击时取消选择它并再次选择它,因为它在选项中。
如果我尝试像这样初始化我的值:
values:[this.$store.state.variableIwantPreselected]
它不能完全工作。在选择区域/下拉搜索字段中,通常会显示带有所选项目名称的绿色框。对我来说,它不会在绿色框中显示 字符串,在 this.$store.state.variableIwantPreselected 中,而只是一个小绿色框什么都没有写在里面。
我想我错过了一些关于 Vuex 和生命周期钩子的东西,但我不知道是什么。
【问题讨论】:
-
你确定 this.$store.state.variableIwantPreselected 包含一串值数组吗?您是否尝试使用 console.log(this.$store.state.variableIwantPreselected) 进行测试?
-
我刚做了。该值是一个字符串,它在商店中。但是,如果我将 variableIwantPreselected 作为包含一个字符串的数组,它会在绿色的 vue-multiselect 框中显示为 ["variableIwantPreselected"]。我需要一个对象而不是一个字符串吗?
标签: javascript vue.js vuex vue-multiselect