Computed 在 Vue 3 脚本设置中如何工作?

Art*_*nov 35 javascript vue.js vuejs3 vue-composition-api vue-script-setup

我正在努力去computed工作<script setup>

<template>
  <div>
    <p>{{ whatever.get() }}</p>
  </div>
</template>


<script setup>
import {computed} from "vue";

const whatever = computed({
    get() {
        console.log('check')
        return 'check?';
    }
})
</script>
Run Code Online (Sandbox Code Playgroud)

该语句console.log()通过了,但return似乎抛出了一个错误:

check
Vue warn]: Unhandled error during execution of render function 
  at <Cms>
Uncaught TypeError: $setup.whatever.get is not a function
at Proxy.render (app.js?id=d9e007128724c77a8d69ec76c6c081a0:39550:266)
at renderComponentRoot (app.js?id=d9e007128724c77a8d69ec76c6c081a0:25902:44)
at ReactiveEffect.componentUpdateFn [as fn] (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30019:57)
at ReactiveEffect.run (app.js?id=d9e007128724c77a8d69ec76c6c081a0:23830:29)
at setupRenderEffect (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30145:9)
at mountComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29928:9)
at processComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29886:17)
at patch (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29487:21)
at render (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30630:13)
at mount (app.js?id=d9e007128724c77a8d69ec76c6c081a0:28882:25)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

And*_*hiu 51

<template>

仅吸气剂:

  • {{ myVal }}
  • <p v-text="myVal" />

吸气剂和吸气剂:

  • <input v-model="myVal">

重要提示不要使用myVal.get()或!myVal.set(value)使用:

  • 得到:myVal
  • 设置(分配):myVal = value

<script setup>

仅吸气剂:

const someReactiveRef = ref(null)
const myVal = computed(() => someReactiveRef.value)
Run Code Online (Sandbox Code Playgroud)

吸气剂和吸气剂:

const someReactiveRef = ref(null)

const myVal = computed({
  get() {
    return someReactiveRef.value
  },
  set(val) {
    someReactiveRef.value = val
  }
})
// myVal can now be used in `v-model`
Run Code Online (Sandbox Code Playgroud)

当反应式引用来自reactive()对象的属性时,您不需要.value在 setter 或 getter 中使用 。例子:

const store = reactive({
  someProp: null
})

const myVal = computed({
  get() {
    return store.someProp
  },
  set(val) {
    store.someProp = val
  }
})
Run Code Online (Sandbox Code Playgroud)

computed请记住,您始终可以在内部使用reactive。例子:

const { createApp, reactive, computed, toRefs } = Vue
createApp({
  setup() {
    const state = reactive({
      firstName: 'John W.',
      lastName: 'Doe',

      // getter + setter
      fullName: computed({
        get() {
          return [state.firstName, state.lastName].join(' ')
        },
        set(val) {
          const [last, ...rest] = val.split(' ').reverse()
          state.firstName = rest.reverse().join(' ')
          state.lastName = last
        }
      }),

      // getter only:
      name: computed(() => state.fullName)
    })
    return toRefs(state)
  }
}).mount('#app')
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue@3.2.40/dist/vue.global.prod.js"></script>
<div id="app">
  <input v-model="firstName" />
  <input v-model="lastName" />
  <input v-model="fullName" />
  <pre v-text="{ firstName, lastName, fullName, name }"></pre>
</div>
Run Code Online (Sandbox Code Playgroud)

重要笔记:

  • 当向 a 传递非反应式引用时computed,Vue 会警告您(计算包装器是不必要的)。
  • 当向它传递包含循环依赖项的引用(例如:组件实例)时,Vue 将再次警告您并建议使用shallowRefmarkRaw

  • 如何在脚本部分而不是模板中访问 myVal? (3认同)