vue3组合式Composition API之setup函数的具体用法

vue3拉开序幕的setup

  1. 理解:Vue3.0中一个新的配置项,值为一个函数。

  2. setup是所有Composition API(组合API)“ 表演的舞台 ”

  3. 组件中所用到的:数据、方法等等,均要配置在setup中。

  4. setup函数的两种返回值:

    1. 若返回一个对象,则对象中的属性、方法, 在模板中均可以直接使用。(重点关注!)

    2. 若返回一个渲染函数:则可以自定义渲染内容。(了解)

  5. 注意点:

    1. 尽量不要与Vue2.x配置混用

      • Vue2.x配置(data、methos、computed...)中可以访问到setup中的属性、方法。

      • 但在setup中不能访问到Vue2.x配置(data、methos、computed...)。

      • 如果有重名, setup优先。

    2. setup不能是一个async函数,因为返回值不再是return的对象, 而是promise, 模板看不到return对象中的属性。(后期也可以返回一个Promise实例,但需要Suspense和异步组件的配合)

 vue3中的setup函数的2种返回值用法

第一种返回值:返回一个对象,对象中的属性和方法在模板中可以直接使用

代码:

<template>
  <div class="hello">
    <h1>一个人的信息</h1>
    <h2>姓名:{
   
   {name}}</h2>
    <h2>年龄:{
   
   {age}}</h2>
    <button @click="sayHello">说话</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  setup(){
    let name='张三'
    let age = 18
    function sayHello(){
      alert(`我叫${name},我${age}岁了,你好啊!`)
    }
    return{
      name,
      age,
      sayHello
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

第2种返回值:返回一个渲染函数,可以自定义渲染内容(用得不多,了解即可)

代码:

<template>
  <div class="hello">
    <h1>一个人的信息</h1>
    <h2>姓名:{
   
   {name}}</h2>
    <h2>年龄:{
   
   {age}}</h2>
    <button @click="sayHello">说话</button>
  </div>
</template>

<script>
import { h } from 'vue'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  setup(){
    // let name='张三'
    // let age = 18
    // function sayHello(){
    //   alert(`我叫${name},我${age}岁了,你好啊!`)
    // }
    // return{
    //   name,
    //   age,
    //   sayHello
    // }
    return ()=> h('h1','尚硅谷')
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

结论:

使用了渲染函数之后。页面上的其他信息就被覆盖了。只显示了渲染函数里面的内容。

以上的例子只是为了演示。

代码:

<template>
  <div class="hello">
    <h1>一个人的信息</h1>
    <h2>姓名:{
   
   { name }}</h2>
    <h2>年龄:{
   
   { age }}</h2>
    <h2>性别:{
   
   { sex }}</h2>
    <h2>a的值是:{
   
   { a }}</h2>
    <button @click="sayHello">说话(Vue3所配置的——sayHello)</button>
    <br>
    <br>
    <button @click="sayWelcome">说话(Vue2所配置的——sayWelcome)</button>
    <br>
    <br>
    <button @click="test1">测试一下在Vue2的配置中去读取Vue3中的数据、方法</button>
    <br>
    <br>
    <button @click="test2">测试一下在Vue3的setup配置中去读取Vue2中的数据、方法</button>
  </div>
</template>

<script>
// import { h } from 'vue'

export default {
  name: 'a1setup',
  props: {
    msg: String
  },
  data() {
    return {
      sex: '男',
      a: 100
    }
  },
  methods: {
    sayWelcome() {
      alert('欢迎来到尚硅谷学习')
    },
    test1() {
      console.log(this.sex)
      console.log(this.name)
      console.log(this.age)
      console.log(this.sayHello)
    }
  },
  setup() {
    let name = '张三'
    let age = 18
    let a = 200
    function sayHello() {
      alert(`我叫${name},我${age}岁了,你好啊!`)
    }
    function test2() {
      console.log(name)
      console.log(age)
      console.log(sayHello)
      console.log(this.sex)
      console.log(this.sayWelcome)
    }
    return {
      name,
      age,
      sayHello,
      test2,
      a
    }
    // return ()=> h('h1','尚硅谷')
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

前3个按钮点击后结果都是正常的。第4个按钮获取不到某些值

猜你喜欢

转载自blog.csdn.net/qq_22182989/article/details/125247952
今日推荐