Vue 3 setup 函数

码农天地 -
Vue 3 setup 函数
感兴趣者可以关注我的公众号 《人生代码》

人生代码

也可以订阅我的 CSDN 专栏

Vue 3 入门基础到实战系列教程

setup

setup 函数可以说是 Vue 3 一个入口函数。

参数

使用 setup 函数时,它将接受两个参数:

propscontext

让我们更深入地研究如何使用每个参数。

Props

setup 函数中的第一个参数是 props。正如在一个标准组件中所期望的那样,setup 函数中的 props 是响应式的,当传入新的 prop 时,它将被更新。我们还是在 src/TemplateM.vue

`<template>
  <div >
    counter ---> {{ counter }}
  </div>
</template>
<script>
import { defineComponent, ref } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  props: {
    test: {
      type: Object,
      default: () => {
        return {
          name: 'haha',
        }
      },
    },
  },
  setup(props) {
    const counter = ref(0)
    console.log('props===>', props)
    return {
      counter,
    }
  },
})
</script>
`

但是,因为 props 是响应式的,你不能使用 ES6 解构,因为它会消除 prop 的响应性。

我们可以尝试一下将 props 进行 ES6 解构,看看会发生什么情况:

`<template>
  <div >
    counter ---> {{ counter }}
    <br/>
    test.name ---> {{ test.name }}
    <br/>
    name ---> {{ name }}
  </div>
</template>
<script>
import { defineComponent, ref } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  props: {
    test: {
      type: Object,
      default: () => {
        return {
          name: 'haha',
        }
      },
    },
  },
  setup(props) {
    const counter = ref(0)
    console.log('props===>', props)
    const {name} = props.test
    return {
      counter,
      name
    }
  },
})
</script>
`

我们看到控制台开始提示报警了:

如果需要解构 prop,可以通过使用 setup 函数中的 toRefs 来安全地完成此操作。

`<template>
  <div >
    counter ---> {{ counter }}
    <br/>
    test.name ---> {{ test.name }}
    <br/>
    name ---> {{ name }}
  </div>
</template>
<script>
import { defineComponent, ref, toRefs } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  props: {
    test: {
      type: Object,
      default: () => {
        return {
          name: 'haha',
        }
      },
    },
  },
  setup(props) {
    const counter = ref(0)
    console.log('props===>', props)
    const {name} = toRefs(props.test)
    return {
      counter,
      name
    }
  },
})
</script>
`

这次我们可以看到效果如下:

上下文

传递给 setup 函数的第二个参数是 contextcontext 是一个普通的 JavaScript 对象,它暴露三个组件的 property:

`<template>
  <div >
  </div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  props: {
    test: {
      type: Object,
      default: () => {
        return {
          name: 'haha',
        }
      },
    },
  },
  setup(props, context) {
    console.log('props===>', props, context)
    return {
    }
  },
})
</script>
`

我们可以看到效果如下:

context 是一个普通的 JavaScript 对象,也就是说,它不是响应式的,这意味着你可以安全地对 context 使用 ES6 解构。

`setup(props, { attrs, slots, emit }) {
    ...
}
`

注意

attrsslots 是有状态的对象,它们总是会随组件本身的更新而更新。这意味着你应该避免对它们进行解构,并始终以 attrs.xslots.x 的方式引用 property。请注意,与 props 不同,attrsslots响应式的。如果你打算根据 attrsslots 更改应用副作用,那么应该在 onUpdated 生命周期钩子中执行此操作。

访问组件的 property

执行 setup 时,组件实例尚未被创建。因此,你只能访问以下 property:

propsattrsslotsemit

换句话说,你将无法访问以下组件选项:

datacomputedmethods结合模板使用

如果 setup 返回一个对象,则可以在组件的模板中像传递给 setupprops property 一样访问该对象的 property:

`<template>
  <div  title="hhhhh">
    <div>{{ readersNumber }} {{ book.title }}</div>
  </div>
</template>
<script>
import { defineComponent, ref, reactive } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  setup() {
    const readersNumber = ref(0)
    const book = reactive({ title: 'Vue 3 Guide' })
    // expose to template
    return {
      readersNumber,
      book,
    }
  },
})
</script>
`

我们可以看到效果如下:

注意,从 setup 返回的 refs 在模板中访问时是被自动解开的,因此不应在模板中使用 .value

使用渲染函数

setup 还可以返回一个渲染函数,该函数可以直接使用在同一作用域中声明的响应式状态:

`<template>
  <div  title="hhhhh">
  </div>
</template>
<script>
import { defineComponent, ref, reactive, h } from 'vue'
export default defineComponent({
  name: 'TemplateM',
  setup() {
    const readersNumber = ref(0)
    const book = reactive({ title: 'Vue 3 Guide' })
    // expose to template
    return () => h('div', [readersNumber.value, book.title])
  },
})
</script>
`

效果如下:

使用 this

setup() 内部,this 不会是该活跃实例的引用,因为 setup() 是在解析其它组件选项之前被调用的,所以 setup() 内部的 this 的行为与其它选项中的 this 完全不同。这在和其它选项式 API 一起使用 setup() 时可能会导致混淆。

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

加个好友,技术交流

1628738909466805.jpg