【问题标题】:"If you don't need to access the actual object instance, you can wrap it in a `reactive`"“如果你不需要访问实际的对象实例,你可以将它包装在一个 `reactive` 中”
【发布时间】:2021-07-14 04:58:36
【问题描述】:

根据doc:

如果您不需要访问实际的对象实例,可以将其包装在reactive 中:

nested: reactive({
  count,
})

看不懂这个技巧,谁能提供一个真实世界的例子?

【问题讨论】:

    标签: vue.js vuejs3 vue-reactivity


    【解决方案1】:

    我同意小费似乎没什么意义。

    基于引入提示的commit 中的更改,我认为它的意思是:

    如果您不想解开模板中的值,可以将值包装在reactive 中的setup() 中。

    即如下代码:

    export default {
      setup() {
        const count = ref(0)
        return {
          nested: {
            count
          }
        }
      }
    }
    

    ...需要在模板中显式展开:

    <template>               ?
      <div>{{ nested.count.value }}</div>
    </template>
    

    但是在这里的嵌套对象周围使用reactive():

    export default {
      setup() {
        const count = ref(0)
        return {      ?
          nested: reactive({
            count
          })
        }
      }
    }
    

    ...允许模板不那么冗长:

    <template>
      <div>{{ nested.count }}</div>
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-09
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2021-10-16
      相关资源
      最近更新 更多