如何从 Vuetify 列表中的 Vuex 商店获取状态,VueJs
How to get states from a Vuex store from within a Vuetify list, VueJs
我有一个看起来像这样的 Vue 文件:
import store from '@/store'
export default{
name: 'myList',
data: () => ({
show: true,
listContent: [{
name: '1',
icon: 'person',
value: function () {
return store.state.myStore.settings.one
}
}, {
name: '2',
icon: 'person',
value: function () {
return store.state.myStore.settings.two
}
}, {
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
]
})
}
不起作用的部分是从 'listContent'.
获取 'value'
{
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
在我的代码中,我导入了视图,就好像我要放置 :
this.$store.state.myStore.settings.one
在价值函数中,'this' 将引用对象
{
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
而且我无法获得商店。但是,我的代码仍然不起作用。我需要访问 listContent 中的商店。
列表呈现如下:
<v-data-table :items="listContent" hide-actions hide-headers>
<template slot="items" slot-scope="props">
<td>{{ props.item.name }}</td>
<td class="text-xs-right" v-text="props.item.value()"> </td>
</template>
</v-data-table>
要么我引用的商店不正确,要么模板不正确。有什么想法吗?
为什么要 value
成为 returns 状态值的函数。您可以使用 this.$store.state.myStore.settings.one
将其分配给 state
值
为了使 data
选项成为一个普通函数而不是箭头函数,这样 this
仍然代表 vue 实例
export default {
name: "myList",
data() {
return {
show: true,
listContent: [
{
name: "1",
icon: "person",
value: this.$store.state.myStore.settings.one
},
{
name: "2",
icon: "person",
value: this.$store.state.myStore.settings.two
},
{
name: "3",
icon: "person",
value: this.$store.state.myStore.settings.three
}
]
};
}
};
也许这会有所帮助。很长,但它有效。
const myModule = {
state: {
test: "modulle",
settings: {
one: "This is one",
two: "This is two",
three: "This is three"
}
}
};
const store = new Vuex.Store({
modules: { myModule }
});
new Vue({
el: "#app",
store,
data() {
return {
listContent: [
{
name: "1",
icon: "person",
value: null
},
{
name: "2",
icon: "person",
value: null
},
{
name: "3",
icon: "person",
value: null
}
]
};
},
watch:{
'$store.state.myModule.settings.one':{
immediate:true,
handler:function(value){
this.listContent[0].value = value;
}
},
'$store.state.myModule.settings.two':{
immediate:true,
handler:function(value){
this.listContent[1].value = value;
}
},
'$store.state.myModule.settings.three':{
immediate:true,
handler:function(value){
this.listContent[2].value = value;
}
},
}
});
我有一个看起来像这样的 Vue 文件:
import store from '@/store'
export default{
name: 'myList',
data: () => ({
show: true,
listContent: [{
name: '1',
icon: 'person',
value: function () {
return store.state.myStore.settings.one
}
}, {
name: '2',
icon: 'person',
value: function () {
return store.state.myStore.settings.two
}
}, {
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
]
})
}
不起作用的部分是从 'listContent'.
获取 'value' {
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
在我的代码中,我导入了视图,就好像我要放置 :
this.$store.state.myStore.settings.one
在价值函数中,'this' 将引用对象
{
name: '3',
icon: 'person',
value: function () {
return store.state.myStore.settings.three
}
}
而且我无法获得商店。但是,我的代码仍然不起作用。我需要访问 listContent 中的商店。
列表呈现如下:
<v-data-table :items="listContent" hide-actions hide-headers>
<template slot="items" slot-scope="props">
<td>{{ props.item.name }}</td>
<td class="text-xs-right" v-text="props.item.value()"> </td>
</template>
</v-data-table>
要么我引用的商店不正确,要么模板不正确。有什么想法吗?
为什么要 value
成为 returns 状态值的函数。您可以使用 this.$store.state.myStore.settings.one
state
值
为了使 data
选项成为一个普通函数而不是箭头函数,这样 this
仍然代表 vue 实例
export default {
name: "myList",
data() {
return {
show: true,
listContent: [
{
name: "1",
icon: "person",
value: this.$store.state.myStore.settings.one
},
{
name: "2",
icon: "person",
value: this.$store.state.myStore.settings.two
},
{
name: "3",
icon: "person",
value: this.$store.state.myStore.settings.three
}
]
};
}
};
也许这会有所帮助。很长,但它有效。
const myModule = {
state: {
test: "modulle",
settings: {
one: "This is one",
two: "This is two",
three: "This is three"
}
}
};
const store = new Vuex.Store({
modules: { myModule }
});
new Vue({
el: "#app",
store,
data() {
return {
listContent: [
{
name: "1",
icon: "person",
value: null
},
{
name: "2",
icon: "person",
value: null
},
{
name: "3",
icon: "person",
value: null
}
]
};
},
watch:{
'$store.state.myModule.settings.one':{
immediate:true,
handler:function(value){
this.listContent[0].value = value;
}
},
'$store.state.myModule.settings.two':{
immediate:true,
handler:function(value){
this.listContent[1].value = value;
}
},
'$store.state.myModule.settings.three':{
immediate:true,
handler:function(value){
this.listContent[2].value = value;
}
},
}
});