Menu

QuickStart 08 - Components In Vue3 (2021.03.11) - 图1

Functional components can only be created using a plain function

  1. import { h } from 'vue'
  2. const DynamicHeading = (props, context) => {
  3. return h(`h${props.level}`, context.attrs, context.slots)
  4. }
  5. DynamicHeading.props = ['level']
  6. export default DynamicHeading

functional attribute on single-file component (SFC) <template> and functional component option are deprecated

In vue2, we may create a functional component like this:

  1. export default {
  2. functional: true,
  3. props: ['level'],
  4. render(h, { props, data, children }) {
  5. return h(`h${props.level}`, data, children)
  6. }
  7. }

Now the syntax is converted to demo1.

functional attribute on single-file component (SFC) is also deprecated and you can write like this:

  1. <template>
  2. <component
  3. v-bind:is="`h${$props.level}`"
  4. v-bind="$attrs"
  5. />
  6. </template>
  7. <script>
  8. export default {
  9. props: ['level']
  10. }
  11. </script>

Async components now require defineAsyncComponent method to be created

In vue2, we difenite an async component like below:

  1. const asyncPage = () => import('./NextPage.vue')
  2. // OR
  3. const asyncPage = {
  4. component: () => import('./NextPage.vue'),
  5. delay: 200,
  6. timeout: 3000,
  7. error: ErrorComponent,
  8. loading: LoadingComponent
  9. }

Now in vue3, we write like this:

  1. import { defineAsyncComponent } from 'vue'
  2. import ErrorComponent from './components/ErrorComponent.vue'
  3. import LoadingComponent from './components/LoadingComponent.vue'
  4. // Async component without options
  5. const asyncPage = defineAsyncComponent(() => import('./NextPage.vue'))
  6. // Async component with options
  7. const asyncPageWithOptions = defineAsyncComponent({
  8. loader: () => import('./NextPage.vue'),
  9. delay: 200,
  10. timeout: 3000,
  11. errorComponent: ErrorComponent,
  12. loadingComponent: LoadingComponent
  13. })

In addition, unlike 2.x, the loader function no longer receives the resolve and reject arguments and must always return a Promise.

  1. // 2.x version
  2. const oldAsyncComponent = (resolve, reject) => {
  3. /* ... */
  4. }
  5. // 3.x version
  6. const asyncComponent = defineAsyncComponent(
  7. () =>
  8. new Promise((resolve, reject) => {
  9. /* ... */
  10. })
  11. )

Component events should now be declared with the emits option

Details can be see here: QuickStart 03 - Fragments And Emits Component Option