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

Detailed explanation of key uniqueness of v-for in Vue

Table of contents 1. DOM Diff 2. Add key attribut...

Example code for achieving hollowing effect with pure CSS

I have recently studied the hollowing effect. bac...

Build a severe weather real-time warning system with Node.JS

Table of contents Preface: Step 1: Find the free ...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

Implementing CommonJS modularity in browsers without compilation/server

Table of contents introduction 1. What is one-cli...

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...

Kill a bunch of MySQL databases with just a shell script like this (recommended)

I was woken up by a phone call early in the morni...

A brief discussion on the use and analysis of nofollow tags

Controversy over nofollow There was a dispute bet...

MySQL 4G memory server configuration optimization

As the number of visits to the company's webs...

How to store images in MySQL

1 Introduction When designing a database, it is i...

Nginx/Httpd reverse proxy tomcat configuration tutorial

In the previous blog, we learned about the usage ...

How to use Maxwell to synchronize MySQL data in real time

Table of contents About Maxwell Configuration and...

Vue improves page response speed through lazy loading

Table of contents Overview What is lazy loading? ...

js data types and their judgment method examples

js data types Basic data types: number, string, b...

Vue implements video upload function

This article example shares the specific code of ...