Detailed explanation of incompatible changes of components in vue3

Detailed explanation of incompatible changes of components in vue3

Functional Components

The functional attribute in the single file component (SFC) <template> has been removed
The { functional: true } option when creating components via functions has been removed

// Use the <dynamic-heading> component, which is responsible for providing the appropriate heading (ie: h1, h2, h3, etc.). In 2.x, this would probably be written as a single file component:
// Vue 2 functional component example export default {
 functional: true,
 props: ['level'],
 render(h, { props, data, children }) {
 return h(`h${props.level}`, data, children)
 }
}

// Vue 2 functional component example using <template>
<template functional>
 <component
 :is="`h${props.level}`"
 v-bind="attrs"
 v-on="listeners"
 />
</template>

<script>
export default {
 props: ['level']
}
</script>

Now in Vue 3, all functional components are created with normal functions, in other words, there is no need to define the { functional: true } component option.
They will receive two parameters: props and context. The context parameter is an object containing the component's attrs, slots, and emit properties.
Additionally, instead of providing h implicitly in the render function, h is now imported globally.
Using the example of the <dynamic-heading> component mentioned earlier, here is what it looks like now.

//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 single file writing <template>
 <component
 v-bind:is="`h${$props.level}`"
 v-bind="$attrs"
 />
</template>

<script>
export default {
 props: ['level']
}
</script>

The main difference is

functional attribute to remove listeners in <template> is now passed as part of $attrs and can be removed

How to write asynchronous components and defineAsyncComponent method

Now use defineAsyncComponent helper method to explicitly define asynchronous components
component option renamed to loader
The Loader function itself no longer accepts resolve and rejuct parameters and must return a Promise

// vue2.x
// Previously asynchronous components were created by defining the component as a function that returns a Promise const asyncPage = () => import('./NextPage.vue')
// Or create it as an option const asyncPage = {
 component: () => import('./NextPage.vue'),
 delay: 200,
 timeout: 3000,
 error: ErrorComponent,
 loading: LoadingComponent
}

// vue3.x
In vue3.x, you need to use defineAsyncComponent to define import{ defineAsyncComponent } from 'vue'
import ErrorComponent from './components/ErrorComponent.vue'
import LoadingComponent from './components/LoadingComponent.vue'

// Definition method without options const asyncPage = defineAsyncComponent(() => import('./NextPage.vue'))

// Async component with options constasyncPageWithOptions = defineAsyncCopmonent({
 loader: () => import('./NextPage.vue'),
 delay: 200,
 timeout: 3000,
 errorComponent: ErrorComponent,
 LoadingComponent: LoadingComponent
})

The loader function no longer accepts resolve and reject parameters and must always return a Promise.

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

Component events need to be declared in the emits option

Vue3 now provides an emits option, similar to the props option. This option can be used to define events that components emit to their parent objects.

<!-- 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 -->
<!-- Now, similar to props, emits can be used to define events emitted by components-->
<!-- This option also receives a given object, which is used to validate the passed parameters like props-->
<!-- It is strongly recommended to record all emits from each component, because without the .native modifier, all listeners for events not declared using will be included in the component's $attr, which by default will be bound to the component's root node -->
<template>
 <div>
 <p>{{ text }}</p>
 <button v-on:click="$emit('accepted')">OK</button>
 </div>
</template>
<script>
 export default {
 props: ['text'],
 emits: ['accepted']
 }
</script>

The above is a detailed explanation of the incompatible changes of components in vue3. For more information about incompatible changes of components in vue3, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • In-depth explanation of Vue multi-select list component
  • Problems and solutions encountered when using v-model to two-way bind the values ​​of parent-child components in Vue
  • Vue example code using transition component animation effect
  • Summary of Vue component basics
  • In-depth understanding of Vue dynamic components and asynchronous components
  • Steps for Vue to use Ref to get components across levels
  • Vue implements multi-tab component
  • Easily implement the whole process of switch function components in vue3
  • How to customize dialog and modal components in Vue3
  • Correct way to force component to re-render in Vue
  • Two ways to dynamically create components in Vue

<<:  Detailed explanation of how to restore database data through MySQL binary log

>>:  Detailed explanation of Nginx reverse generation Mogilefs distributed storage example

Recommend

Tutorial on installing phpMyAdmin under Linux centos7

yum install httpd php mariadb-server –y Record so...

How to upgrade CentOS7 to CentOS8 (detailed steps)

This article uses a specific example to introduce...

How to use lazy loading in react to reduce the first screen loading time

Table of contents use Install How to use it in ro...

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

Analysis of HTTP interface testing process based on postman

I accidentally discovered a great artificial inte...

How to write object and param to play flash in firefox

Copy code The code is as follows: <object clas...

How to implement form validation in Vue

1. Installation and use First, install it in your...

Three ways to share component logic in React

Without further ado, these three methods are: ren...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

Interviewers often ask questions about React's life cycle

React Lifecycle Two pictures to help you understa...

Detailed tutorial on using the tomcat8-maven-plugin plugin in Maven

I searched a lot of articles online but didn'...