Functional Components The functional attribute in the single file component (SFC) <template> 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. //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 // 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 optionVue3 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:
|
<<: Detailed explanation of how to restore database data through MySQL binary log
>>: Detailed explanation of Nginx reverse generation Mogilefs distributed storage example
Table of contents 1. DOM Diff 2. Add key attribut...
I have recently studied the hollowing effect. bac...
Table of contents Preface: Step 1: Find the free ...
1. Brief Introduction Vue.js allows you to define...
Table of contents introduction 1. What is one-cli...
WeChat applet uniapp realizes the left swipe to d...
I was woken up by a phone call early in the morni...
Controversy over nofollow There was a dispute bet...
As the number of visits to the company's webs...
1 Introduction When designing a database, it is i...
In the previous blog, we learned about the usage ...
Table of contents About Maxwell Configuration and...
Table of contents Overview What is lazy loading? ...
js data types Basic data types: number, string, b...
This article example shares the specific code of ...