从 getter 中获取选定的 属性
Getting a selected property from a getter
我的状态下有一组容器,我正在尝试设置一个 getter 将其拆分为活动容器和非活动容器。
containers: [{
id: '1',
name: 'test container',
image: 'some image',
state: 'running',
status: 'Running'
}, {
id: '2',
name: 'another test container',
image: 'some image',
state: 'stopped',
status: 'Running'
}]
我正在使用它来获取下面的数组。
export const x = state => _.partition(state.containers, c => c.state === 'running');
问题是我希望它拆分并分配给 activeContainers 和 stoppedContainers,然后导出。
[
[
{
"id": "1",
"name": "test container",
"image": "some image",
"state": "running",
"status": "Running"
}
],
[
{
"id": "2",
"name": "another test container",
"image": "some image",
"state": "stopped",
"status": "Running"
}
]
]
我尝试过使用 ES6 的解构,但我认为我遗漏了一些东西或者将解构放在了错误的位置以使其与 Vuex 一起工作。
export const [activeContainers = [], stoppedContainers = []] = state => _.partition(state.containers, c => c.state === 'running');
从 getter
返回 functions
根据问题中的评论,确实不能让 getter
映射两个属性。阅读更多您不想要 activeContainers
和 stoppedContainers
的另一个 getter
在浏览了您分享的 link 之后,我找到了一种方法,您仍然可以拥有非常接近参数化的东西 getter
。
Here你可以看到它的完整效果。
getters = {
getContainer: (state) => {
const [activeContainer = [], inactiveContainer = []] = _.partition(state.containers, c => c.state === 'running')
return (container) => {
return (container === 'activeContainer') // returned function
? activeContainer
: inactiveContainer
}
}
}
这里getter,我返回一个函数,它可以接受参数并得到非常接近你想要的东西。
我的状态下有一组容器,我正在尝试设置一个 getter 将其拆分为活动容器和非活动容器。
containers: [{
id: '1',
name: 'test container',
image: 'some image',
state: 'running',
status: 'Running'
}, {
id: '2',
name: 'another test container',
image: 'some image',
state: 'stopped',
status: 'Running'
}]
我正在使用它来获取下面的数组。
export const x = state => _.partition(state.containers, c => c.state === 'running');
问题是我希望它拆分并分配给 activeContainers 和 stoppedContainers,然后导出。
[
[
{
"id": "1",
"name": "test container",
"image": "some image",
"state": "running",
"status": "Running"
}
],
[
{
"id": "2",
"name": "another test container",
"image": "some image",
"state": "stopped",
"status": "Running"
}
]
]
我尝试过使用 ES6 的解构,但我认为我遗漏了一些东西或者将解构放在了错误的位置以使其与 Vuex 一起工作。
export const [activeContainers = [], stoppedContainers = []] = state => _.partition(state.containers, c => c.state === 'running');
从 getter
返回 functions
根据问题中的评论,确实不能让 getter
映射两个属性。阅读更多您不想要 activeContainers
和 stoppedContainers
在浏览了您分享的 link 之后,我找到了一种方法,您仍然可以拥有非常接近参数化的东西 getter
。
Here你可以看到它的完整效果。
getters = {
getContainer: (state) => {
const [activeContainer = [], inactiveContainer = []] = _.partition(state.containers, c => c.state === 'running')
return (container) => {
return (container === 'activeContainer') // returned function
? activeContainer
: inactiveContainer
}
}
}
这里getter,我返回一个函数,它可以接受参数并得到非常接近你想要的东西。