好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

函数式组件

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.0

import { 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.x

const oldAsyncComponent = (resolve, reject) => {}

// vue3.x

const 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中组件的非兼容变更的资料请关注服务器之家其它相关文章!

原文链接:https://segmentfault.com/a/1190000039318888

查看更多关于详解vue3中组件的非兼容变更的详细内容...

  阅读:58次