Skip to content

Getters

Pomy edited this page Aug 24, 2017 · 5 revisions

Sometimes the component needs to get some data from the state in module for component rendering or update, using the mergeProps helper function to map module state to local computed properties.

有时组件需要从 module 中的 state 获取一些数据用于组件渲染或者更新, 利用 mergeProps 辅助函数可以将 module 中的 state 映射到局部计算属性.

First, we should create a module:

首先创建一个 module:

const moduleA = {
    namespace: 'modulea',
    state: {
        title: 'the title of moduleA',
        desc: 'some description for moduleA'
    },
    actions: {
        // ...
    }
}

Then we can use the mergeProps helper function to map module state to local computed properties:

然后利用 mergeProps 辅助函数进行属性映射:

import { mergeProps } from 'revuejs'

export default {
  // ...
  computed: {
    // mix the props into computed with object spread operator
    // 使用对象展开运算符将 props 混入 computed 对象中
    ...mergeProps([
      'modulea.title',
      'modulea.desc',
      // ...
    ])
  }
}

If you want to map a getter to a different name, use an object:

默认会使用 state 中的属性名作为计算属性名, 如果想另取一个名字, 使用对象形式:

...mergeProps({
    comTitle: 'modulea.title',
    comDesc: 'modulea.desc'
})
Clone this wiki locally