五种在 Vue 3 中定义组件的方法

开发 前端
Vue 正在不断发展,目前,在Vue 3 中有多种定义组件的方法。从选项到组合再到类 API,情况大不相同,如果您刚刚开始,可能会感到困惑。让我们定义一个简单的组件并使用所有可用的方法重构它。

Vue 正在不断发展,目前,在Vue 3  中有多种定义组件的方法。从选项到组合再到类 API,情况大不相同,如果您刚刚开始,可能会感到困惑。让我们定义一个简单的组件并使用所有可用的方法重构它。

1.  Options API

这是在 Vue 中声明组件的最常见方式。从版本 1 开始可用,您很可能已经熟悉它。一切都在对象内声明,数据在幕后由 Vue 响应。它不是那么灵活,因为它使用 mixin 来共享行为。

<script>
import TheComponent from './components/TheComponent.vue'
import componentMixin from './mixins/componentMixin.js'


export default {
  name: 'OptionsAPI',
  components: {
    TheComponent,
    AsyncComponent: () => import('./components/AsyncComponent.vue'),
  },
  mixins: [componentMixin],
  props: {
    elements: {
      type: Array,
    },
    counter: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      object: {
        variable: true,
      },
    }
  },
  computed: {
    isEmpty() {
      return this.counter === 0
    },
  },
  watch: {
    counter() {
      console.log('Counter value changed')
    },
  },
  created() {
    console.log('Created hook called')
  },
  mounted() {
    console.log('Mounted hook called')
  },
  methods: {
    getParam(param) {
      return param
    },
    emitEvent() {
      this.$emit('event-name')
    },
  },
}
</script>
<template>
  <div class="wrapper">
    <TheComponent />
    <AsyncComponent v-if="object.variable" />
    <div class="static-class-name" :class="{ 'dynamic-class-name': object.variable }">
      Dynamic attributes example
    </div>
    <button @click="emitEvent">Emit event</button>
  </div>
</template>


<style lang="scss" scoped>
.wrapper {
  font-size: 20px;
}
</style>

2.Composition API

经过多次讨论、来自社区的反馈,以及令人惊讶的是,在这个 RFC 中,有很多戏剧性的内容,在 Vue 3 中引入了 Composition API。 目的是提供更灵活的 API 和更好的 TypeScript 支持。这种方法在很大程度上依赖于设置生命周期挂钩。

<script>
import {
  ref,
  reactive,
  defineComponent,
  computed,
  watch,
} from 'vue'


import useMixin from './mixins/componentMixin.js'
import TheComponent from './components/TheComponent.vue'


export default defineComponent({
  name: 'CompositionAPI',
  components: {
    TheComponent,
    AsyncComponent: () => import('./components/AsyncComponent.vue'),
  },
  props: {
    elements: Array,
    counter: {
      type: Number,
      default: 0,
    },
  },
  setup(props, { emit }) {
    console.log('Equivalent to created hook')


    const enabled = ref(true)
    const object = reactive({ variable: false })


    const { mixinData, mixinMethod } = useMixin()


    const isEmpty = computed(() => {
      return props.counter === 0
    })


    watch(
      () => props.counter,
      () => {
        console.log('Counter value changed')
      }
    )


    function emitEvent() {
      emit('event-name')
    }
    function getParam(param) {
      return param
    }


    return {
      object,
      getParam,
      emitEvent,
      isEmpty
    }
  },
  mounted() {
    console.log('Mounted hook called')
  },
})
</script>


<template>
  <div class="wrapper">
    <TheComponent />
    <AsyncComponent v-if="object.variable" />
    <div class="static-class-name" :class="{ 'dynamic-class-name': object.variable }">
      Dynamic attributes example
    </div>
    <button @click="emitEvent">Emit event</button>
  </div>
</template>


<style scoped>
.wrapper {
  font-size: 20px;
}
</style>

如您所知,使用这种混合方法需要大量样板代码,而且设置函数很快就会失控。在迁移到 Vue 3 时,这可能是一个很好的中间步骤,但是语法糖可以让一切变得更干净。

3.Script setup

在 Vue 3.2 中引入了一种更简洁的语法。通过在脚本元素中添加设置属性,脚本部分中的所有内容都会自动暴露给模板。通过这种方式可以删除很多样板文件。

<script setup>
import {
  ref,
  reactive,
  defineAsyncComponent,
  computed,
  watch,
  onMounted,
} from "vue";


import useMixin from "./mixins/componentMixin.js";
import TheComponent from "./components/TheComponent.vue";
const AsyncComponent = defineAsyncComponent(() =>
  import("./components/AsyncComponent.vue")
);


console.log("Equivalent to created hook");
onMounted(() => {
  console.log("Mounted hook called");
});


const enabled = ref(true);
const object = reactive({ variable: false });


const props = defineProps({
  elements: Array,
  counter: {
    type: Number,
    default: 0,
  },
});


const { mixinData, mixinMethod } = useMixin();


const isEmpty = computed(() => {
  return props.counter === 0;
});


watch(() => props.counter, () => {
  console.log("Counter value changed");
});


const emit = defineEmits(["event-name"]);
function emitEvent() {
  emit("event-name");
}
function getParam(param) {
  return param;
}
</script>


<script>
export default {
  name: "ComponentVue3",
};
</script>


<template>
  <div class="wrapper">
    <TheComponent />
    <AsyncComponent v-if="object.variable" />
    <div
      class="static-class-name"
      :class="{ 'dynamic-class-name': object.variable }"
    >
      Dynamic attributes example
    </div>
    <button @click="emitEvent">Emit event</button>
  </div>
</template>


<style scoped>
.wrapper {
  font-size: 20px;
}
</style>

4. Reactivity Transform

这是非常有争议的,被删除了!这使得脚本设置成为本文的明确答案。(26/1/2023 更新)

以下代码段中演示的脚本设置存在问题。

<script setup>
import { ref, computed } from 'vue'


const counter = ref(0)
counter.value++


function increase() {
  counter.value++
}


const double = computed(() => {
  return counter.value * 2
})
</script>




<template>
  <div class="wrapper">
    <button @click="increase">Increase</button>
    {{ counter }}
    {{ double }}
  </div>
</template>

正如您所注意到的,使用 .value 访问反应式计数器感觉不自然,并且是混淆和错误输入的常见来源。 

有一个实验性解决方案利用编译时转换来解决此问题。反应性转换是一个可选的内置步骤,它会自动添加此后缀并使代码看起来更清晰。

<scr
ipt setup>
import { computed } from 'vue'
let counter = $ref(0)
counter++
function increase() {
  counter++
}
const double = computed(() => {
return counter * 2
})
</script>
<template>
<div class="wrapper">
<button @click="increase">Increase</button>
    {{ counter }}
    {{ double }}
</div>
</template>

$ref 需要一个构建步骤,但在访问变量时删除了 .value 的必要性。启用后它在全球范围内可用。

5.Class API

Class API 已经可用很长时间了。通常与 Typescript 搭配使用是 Vue 2 的可靠选择,并且被认真考虑为默认的 Vue 3 语法。 

但经过多次长时间的讨论后,它被放弃了,取而代之的是 Composition API。 

它在 Vue 3 中可用,但工具严重缺乏,官方建议远离它。无论如何,如果您真的喜欢使用类,您的组件将看起来像这样。

<script lang="ts">
import { Options, Vue } from 'vue-class-component';


import AnotherComponent from './components/AnotherComponent.vue'  


@Options({
  components: {
    AnotherComponent
  }
})
export default class Counter extends Vue {
  counter = 0;


  get double(): number {
    return this.counter * 2;
  }
  increase(): void {
    this.quantity++;
  }
}
</script>




<template>
  <div class="wrapper">
    <button @click="increase">Increase</button>
    {{ counter }}
    {{ double }}
  </div>
</template>

结论

那哪个最好呢?这取决于典型的反应,尽管在这种情况下并非如此。从 Vue 2 迁移时,选项和类 API 可以用作中间步骤,但它们不应该是您的首选。 

如果您没有构建阶段,则组合 API 设置是唯一的选择,但由于大多数项目都是使用 Webpack 或 Vite 生成的,因此使用脚本设置既是可能的,也是鼓励的,因为大多数可访问的文档都使用这种方法。

责任编辑:华轩 来源: web前端开发
相关推荐

2021-09-29 11:33:19

异步组件Vue 3

2023-11-21 15:23:15

JavaScript工具

2023-06-02 15:42:51

JavaScript数据结构对象

2020-07-24 20:45:51

Spark数据集函数

2020-03-25 18:23:07

Vue2Vue3组件

2017-06-30 08:51:12

组件模板勒索软件项目管理

2021-08-27 14:03:05

远程团队沟通远程通信

2018-12-19 19:30:46

JavaScript创建对象前端

2022-09-20 12:21:25

Vue2Vue3$attrs

2023-04-19 15:29:53

通信技巧Vue 3开发

2022-05-06 08:47:10

Vue 3组件前端

2017-07-03 16:03:49

IT技术周刊

2022-08-01 11:41:00

Vue插件

2022-07-26 01:06:18

Vue3自定义指令

2023-05-09 15:01:43

JavaScript编程语言异常处理

2019-08-14 10:00:08

vue组件通信前端

2019-02-13 14:55:22

Windows 10视频删除声音

2023-07-04 15:11:30

TypeScript类型保护

2023-06-28 08:05:46

场景vue3自定义

2023-10-11 09:00:00

点赞
收藏

51CTO技术栈公众号