Home > Web Front-end > Vue.js > body text

How to implement component-level base classes in Vue3

PHPz
Release: 2023-05-23 15:19:49
forward
1260 people have browsed it

Use mixins and extends

vue3 provides mixins and extends, but after trying it, I found that these two methods only support pure OptionAPI. The set data will be recognized, but the reactive of return in the set setup is completely Invalid, setup is not executed.
So this method can only be used in the first method.

Use hooks (function, class)

Since the official does not provide it, let’s find a way ourselves. Let's first observe the component code (second case):

<template>
  <!--模板-->
  举例
</template>
<script lang="ts">
  import { defineComponent } from &#39;vue&#39;
  export default defineComponent({
    name: &#39;ui-core-&#39;,
    components: {
      // 注册共用组件
    },
    props: {
      // 定义共用属性
    },
    setup(props, context) {
      // 各种共用操作
      _logger()
      _setTitle()
      // 共用成员
      const foo = reactive ({})
      return {
        foo
      }
    }
  })
</script>
Copy after login

defineComponent method receives an object, which needs to have several specific attributes, such as name, components, props, setup, etc.
In other words, we can make a function to return such an object.
For example, let's create a js (or ts) file first:

export function base (name, callback) {
  return {
    name: &#39;ui-&#39; + name,
    components: {
      // 注册共用组件
    },
    props: {
      // 定义共用属性
    },
    setup(props, context) {
      // 各种共用操作
      _logger()
      _setTitle()
      // 共用成员
      const foo = reactive ({})
      // 执行其他操作
      const re = callback(props, context)
      return {
        foo,
        ...re
      }
    }
  }
}
Copy after login

It's a bit like template mode.

Pass in name and a callback function, props, and context as parameters. Internal members can also be passed as parameters.
Such a simple base class is created. If you think function is not good-looking, you can change it to class.

export default class BaseComponent {
  name: string
  components: any
  props: any
  setup: any
  constructor (name: string, callback: (props: any, context: any) => any) {
    this.name = name
    this.components = {}
    this.props = {}
    this.setup = (props: any, context: any) => {
      // 各种共用操作
      _logger()
      _setTitle()
      // 执行其他操作
      const re = callback(props, context)
      return {
        ...re
      }
    }
  }
}
Copy after login

After having class, you can also set subclasses, but it feels a bit cumbersome. In short, it can be achieved anyway.

What to do with script setup

The above method should also support pure composition API, but there is a slight problem. defineProps and defineEmits are not ordinary js functions, but a kind of "macro" .
Quoting the explanation from the official website:

defineProps and defineEmits are compiler macros that can only be used in

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template