关于javascript:Vue-3-setup-函数

感兴趣者能够关注我的公众号 《人生代码》

人生代码

也能够订阅我的 CSDN 专栏

Vue 3 入门根底到实战系列教程

setup

setup 函数能够说是 Vue 3 一个入口函数。

参数

应用 setup 函数时,它将承受两个参数:

  1. props
  2. context

让咱们更深刻地钻研如何应用每个参数。

Props

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

`<template>
  <div class=”template-m-wrap”>
    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 class=”template-m-wrap”>
    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 class=”template-m-wrap”>
    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 class=”template-m-wrap”>
  </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:

  • props
  • attrs
  • slots
  • emit

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

  • data
  • computed
  • methods

联合模板应用

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

`<template>
  <div class=”template-m-wrap” 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 class=”template-m-wrap” 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() 时可能会导致混同。

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据