首页前端开发JavaScript详解vue3中组件的非兼容变更

详解vue3中组件的非兼容变更

时间2024-02-01 00:38:03发布访客分类JavaScript浏览678
导读:收集整理的这篇文章主要介绍了详解vue3中组件的非兼容变更,觉得挺不错的,现在分享给大家,也给大家做个参考。 目录函数式组件异步组件的写法与defineAsyncComponent方法组...
收集整理的这篇文章主要介绍了详解vue3中组件的非兼容变更,觉得挺不错的,现在分享给大家,也给大家做个参考。
目录
  • 函数式组件
  • 异步组件的写法与defineAsyncComponent方法
  • 组件事件需要在emits选项中声明

函数式组件
@H_304_16@

functional attribute 在单文件组件 (Sfc) template> 已被移除
{ functional: true } 选项在通过函数创建组件已被移除

// 使用 dynamic-heading>
 组件,负责提供适当的标题 (即:h1,h2,h3,等等),在 2.x 中,这可能是作为单个文件组件编写的:// Vue 2 函数式组件示例export default {
 functional: true, PRops: ['level'], render(h, {
 props, data, children }
) {
 return h(`h${
props.level}
`, data, children) }
}
    // Vue 2 函数式组件示例使用 template>
    template functional>
 component :is="`h${
props.level}
    `" v-bind="attrs" v-on="listeners" />
    /template>
    script>
export default {
 props: ['level']}
    /script>

现在在 Vue 3 中,所有的函数式组件都是用普通函数创建的,换句话说,不需要定义 { functional: true } 组件选项。
他们将接收两个参数:props 和 context。context 参数是一个对象,包含组件的 attrs,slots,和 emIT proPErty。
此外,现在不是在 render 函数中隐式提供 h,而是全局导入 h。
使用前面提到的 dynamic-heading> 组件的示例,下面是它现在的样子。

// vue3.0import {
 h }
     From 'vue'const DynamicHeading = (props, context) =>
 {
 return h(`h${
props.level}
`, context.attrs, context.slots)}
    DynamicHeading.props = ['level']export default DynamicHeading// vue3.0单文件写法template>
 component v-bind:is="`h${
$props.level}
    `" v-bind="$attrs" />
    /template>
    script>
export default {
 props: ['level']}
    /script>
    

主要区别在于

functional attribute 在 template>
     中移除listeners 现在作为 $attrs 的一部分传递,可以将其删除

异步组件的写法与defineAsyncComponent方法

现在使用defineAsyncComponent助手方法,用于显示的定义异步组件
component选项重命名为loader
Loader函数本身不再接受resolve和rejuct参数,必须返回一个Promise

// vue2.x// 以前异步组件是通过将组件定义为返回Promise的函数来创建const asyncPage = () =>
 import('./NextPage.vue')// 或者以选项方式创建const asyncPage = {
     component: () =>
 import('./NextPage.vue'), delay: 200, timeout: 3000, error: ErrorComponent, loading: LoadingComponent}
// vue3.x在vue3.x中,需要使用defineAsyncComponent来定义import{
 defineAsyncComponent }
     from 'vue'import ErrorComponent from './components/ErrorComponent.vue'import LoadingComponent from './components/LoadingComponent.vue'// 不带选项的定义方法const asyncPage = defineAsyncComponent(() =>
 import('./NextPage.vue'))// 带选项的异步组件constasyncPageWithOptions = defineAsyncCopmonent({
     loader: () =>
 import('./NextPage.vue'), delay: 200, timeout: 3000, errorComponent: ErrorComponent, LoadingComponent: LoadingComponent}
    )

loader函数不再接收resolve和reject参数,且必须始终返回Promise

// vue2.xconst oldAsyncComponent = (resolve, reject) =>
 {
}
    // vue3.xconst asyncComponent = defineAsyncComponent(() =>
     new Promise((resolve, reject) =>
 {
}
    ))

组件事件需要在emits选项中声明

vue3中现在提供了一个emits选项,类似props选项
此选项可以用于定义组件向其父对象发出的事件

!-- vue2.x -->
    template>
     div>
     p>
{
{
 text }
}
    /p>
     button v-on:click="$emit('accepted')">
    OK/button>
     /div>
    /template>
    script>
 export default {
 props: ['text'] }
    /script>
    !-- vue3.x -->
    !-- 现在和prop类似,可以用emits来定义组件发出的事件 -->
    !-- 这个选项还接收已给对象,用来向props一样对传递的参数进行验证 -->
    !-- 强烈建议记录下每个组件发出的所有emits,因为去掉了.native修饰符,未使用声明的事件的所有监听器都将包含在组建的$attr中,默认情况下,该监听器将绑定到组件的根节点 -->
    template>
     div>
     p>
{
{
 text }
}
    /p>
     button v-on:click="$emit('accepted')">
    OK/button>
     /div>
    /template>
    script>
 export default {
 props: ['text'], emits: ['accepted'] }
    /script>
    

以上就是详解vue3中组件的非兼容变更的详细内容,更多关于vue3中组件的非兼容变更的资料请关注其它相关文章!

您可能感兴趣的文章:
  • Vue多选列表组件深入详解
  • vue 使用 v-model 双向绑定父子组件的值遇见的问题及解决方案
  • vue使用transition组件动画效果的实例代码
  • vue 组件基础知识总结
  • 深入了解Vue动态组件和异步组件
  • Vue使用Ref跨层级获取组件的步骤
  • Vue实现多页签组件
  • vue3中轻松实现switch功能组件的全过程
  • vue3自定义dialog、modal组件的方法
  • Vue中强制组件重新渲染的正确方法
  • vue 动态创建组件的两种方法

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

vue组件

若转载请注明出处: 详解vue3中组件的非兼容变更
本文地址: https://pptw.com/jishu/594690.html
C语言中for语句的执行过程是什么? strcat函数的作用是什么?

游客 回复需填写必要信息