直接导出 reactive 属性 vs toRef wrap

Export reactive property directly vs toRef wrap

我正在使用新组合 API (https://github.com/vuejs/composition-api) 创建我的 Vue 2“商店”。我想公开我的状态的一个子集。

参考来源保持反应性的正确方法是什么属性?

const state = reactive({
  somethingInternal: '',
  name: ''
})

// is this enough?
export const name = state.name
// or do I need to do this?
export const name = toRef(state, 'name')

您必须使用 toRef 来保持反应性。

这只导出字符串文字:

export const name = state.name

但这保留了与反应式的连接 属性:

export const name = toRef(state, 'name')

如果将 statename 的两个导出都导入另一个模块,然后更改 state.name,更改将反映在 toRef 导出中,但不会文字。