3

七个让我们成为更好 Vue 开发者的技巧

 1 year ago
source link: https://www.fly63.com/article/detial/11793
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我使用 vue 已经很多年了,特别是去年一直在使用 Vue3,因此,学到了很多东西。

1、脚本设置

如果以前使用过组合 api,我们需要始终执行 defineComponent 和 setup() {}:

<script lang="ts">
import {defineComponent} from 'vue';
export default defineComponent({
 name: 'Test',
 setup() {
   //Add business logic
 }
})
</script>

对每个组件执行此操作可能会相当麻烦,实际上,可以避免对组件进行引导。可以使用 <script setup> ,它是如上所示执行相同操作的简写。它基本上只是顶部的语法糖,因此,不必每次都进行手动引导:使用 <script setup>,您的组件将被简化为:

<script lang="ts" setup>
 //Add business logic
</script>

2、如何覆盖反应对象

默认情况下,当定义一个响应式对象时,您不能覆盖整个对象,如果这样做,您将失去响应性。

<script lang="ts">
import { defineComponent, reactive, onMounted } from "vue";
export default defineComponent({
 name: "HelloWorld",
 setup() {
   let myReactiveObject = reactive({
     name: "Nicky",
     age: "37",
     country: "DK",
   });
   let newObject = {
     name: "Nicky Christensen",
     age: "36",
     country: "DA-DK",
   };
   onMounted(() => {
     setTimeout(() => {
       //myReactiveObject = newObject //Wont work
       Object.assign(myReactiveObject, newObject) //Will work
     }, 2000)
   })
   return {
     myReactiveObject,
   };
 },
});
</script>

查看此 Codesandbox 以了解其实际效果:https://codesandbox.io/s/lingering-http-ryf2bj?file=/src/components/HelloWorld.vue

3、反应式 css

在新版本的 Vue 中,一件非常棒的事情是,可以将 CSS 直接绑定到我们的变量中。我发现这对于我在去年构建的一些应用程序非常有用。

........
const color = ref('#f000');<style>
.text {
  color: v-bind(color);  
}
</style>

4、全局组件

时不时地,我们希望拥有全局可用的组件,而不是每次需要它们时都必须导入它们。

我们可以通过转到 main.ts 并执行以下操作轻松完成此操作:

import App from "./App.vue";
import MyGlobalSection from '@/components/MyGlobalSection.vue';
const app = createApp(App);
// Make our <MyGlobalSection /> component globally available.
app.component(MyGlobalSection.name, MyGlobalSection);
app.mount("#app");

现在,我们应该能够通过在要使用全局组件的组件中的模板中编写 <MyGlobalSection /> 来全局使用该组件

5、组合 API 优于选项 API

使用 Vue 3,我们获得了 Composition API。这是对 Vue 的一个真正伟大的补充,我认为总是选择使用 Composition API 而不是 Options API。

我喜欢 Composition API 的一点是,我发现它使用起来更灵活,而且我开始使用 composeables,它被认为是 mixins 的替代品。非常强大且非常酷的补充。

6、使用 v-once 或 v-memo 提高性能

如果你关心快速渲染,可能希望使用 Vue 的内置指令之一,例如 v-once 或 v-memo 来提高应用程序的渲染性能。

v-once,可以在多个元素中应用 v-once,例如常规元素、循环中或组件。

<template>
 <!-- single -->
 <p v-once>{{ someProperty }}</p>
 <!-- with children -->
 <div v-once>
   <p>{{ someProperty}}</p>
 </div>
 <!-- components -->
 <my-component v-once />
 <!-- v-for directives -->
 <li v-for="”item" in items” v-once>{{item}}</li>
</template>

v-memo;简而言之,v-memo 用于记忆模板的子树,这意味着它存储先前渲染的结果以加快未来渲染的速度。

v-memo 指令可用于元素和组件,并且可以。v-memo 接受一个数组,并且只有当数组中的一个值发生变化时才会重新渲染。

<div v-memo="[valueA, valueB]">
 ...
</div>

如果 valueA 或 valueB 发生变化,它将更新。但请注意,v-memo 在 v-for 循环中不起作用。

7、组件的异步加载

为了让你的应用程序更高效并最小化你的主包,延迟加载你的组件是个好主意。在 Vue3 中,我们可以使用 defineAsyncComponent 来延迟加载组件。

这意味着该组件仅在需要时才加载。使用这种技术,您可以显着改善应用程序的负载。

定义异步组件的最简单方法可以像这样完成:

import { defineAsyncComponent } from "vue";
// Lazy Load
const myComponent = defineAsyncComponent(() =>
 import("./components/myComponent.vue")
);

但是,如果您需要它,您可以做更多的事情,将对象传递给 defineAsyncComponent:

const myComponent = defineAsyncComponent({
 loader: () => import("./myComponent.vue"),
 loadingComponent: myLoadingComponent /* shows while loading */,
 errorComponent: myErrorComponent /* shows if there's an error */,
 delay: 1000 /* delay in ms before showing loading component */,
 timeout: 3000 /* timeout after this many ms */,
});

链接: https://www.fly63.com/article/detial/11793


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK