javascript - 直接导出反应属性与 toRef 包装
问题描述
我正在使用新的组合 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')
如果您将所有的导入state
和导出name
都导入另一个模块,然后更改state.name
,则更改将反映在toRef
导出中,而不是文字中。
推荐阅读
- ios - Swift:调用中的参数缺少参数
- python - 有没有办法可以运行由 python 中的函数打印的代码?
- php - PHP 7.4+ 类属性类型
- html - 如何在单击时隐藏按钮中的蓝色阴影?
- ios - Swift 可解码请求对象
- nginx - Nginx 使用 SSL 提供 404
- bash - 如何从内部浏览器页面(例如“brave://rewards”)抓取数据
- python - 有没有比 stripe.Customer.retrieve 函数更快的替代方法?
- javascript - 如何使父容器的高度设置为固定 px 高度和子内容高度之间的最大值?
- python-3.x - 使用aiohttp,将响应体解析为动态类型