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

Teach you 5 knowledge points to make Vue3 development smoother

WBOY
Release: 2022-02-18 17:12:35
forward
2278 people have browsed it

This article brings you knowledge about development in Vue, and compiles 5 little knowledge that can improve development efficiency, making your Vue3 project development smoother and smoother. I hope it will be helpful to everyone.

Teach you 5 knowledge points to make Vue3 development smoother

1. Setup name enhancement

Vue3’s setup syntax sugar is a good thing, but the first problem caused by using setup syntax is that it cannot be customized. name, and when we use keep-alive, we often need name. This problem is usually solved by writing two script tags, one using setup and one not using it, but this is definitely not elegant enough.

<script>
import { defineComponent, onMounted } from &#39;vue&#39;
export default defineComponent({
  name: &#39;OrderList&#39;
})
</script>
<script setup>
onMounted(() => {
  console.log(&#39;mounted===&#39;)
})
</script>
Copy after login

At this time, with the help of the plug-in vite-plugin-vue-setup-extend, we can solve this problem more elegantly. Instead of writing two script tags, we can directly define the name on the script tag.

Installation

npm i vite-plugin-vue-setup-extend -D
Copy after login

Configuration

// vite.config.ts
import { defineConfig } from &#39;vite&#39;
import VueSetupExtend from &#39;vite-plugin-vue-setup-extend&#39;
export default defineConfig({
  plugins: [
    VueSetupExtend()
  ]
})
Copy after login

Use

<script setup name="OrderList">
import { onMounted } from &#39;vue&#39;
onMounted(() => {
  console.log(&#39;mounted===&#39;)
})
</script>
Copy after login

2. API automatic import

setup syntax allows us to use the variables and methods on the template without returning them one by one, which greatly frees up our hands. However, for some commonly used VueAPIs, such as ref, computed, watch, etc., we still need to manually import them on the page every time.

We can realize automatic import through unplugin-auto-import, and you can use Vue's API in the file without importing.

Installation

npm i unplugin-auto-import -D
Copy after login

Configuration

// vite.config.ts
import { defineConfig } from &#39;vite&#39;
import AutoImport from &#39;unplugin-auto-import/vite&#39;
export default defineConfig({
  plugins: [
    AutoImport({
       // 可以自定义文件生成的位置,默认是根目录下,使用ts的建议放src目录下
      dts: &#39;src/auto-imports.d.ts&#39;,
      imports: [&#39;vue&#39;]
    })
  ]
})
Copy after login

The auto-imports.d.ts file will be automatically generated after installation and configuration.

// auto-imports.d.ts
// Generated by &#39;unplugin-auto-import&#39;
// We suggest you to commit this file into source control
declare global {
  const computed: typeof import(&#39;vue&#39;)[&#39;computed&#39;]
  const createApp: typeof import(&#39;vue&#39;)[&#39;createApp&#39;]
  const customRef: typeof import(&#39;vue&#39;)[&#39;customRef&#39;]
  const defineAsyncComponent: typeof import(&#39;vue&#39;)[&#39;defineAsyncComponent&#39;]
  const defineComponent: typeof import(&#39;vue&#39;)[&#39;defineComponent&#39;]
  const effectScope: typeof import(&#39;vue&#39;)[&#39;effectScope&#39;]
  const EffectScope: typeof import(&#39;vue&#39;)[&#39;EffectScope&#39;]
  const getCurrentInstance: typeof import(&#39;vue&#39;)[&#39;getCurrentInstance&#39;]
  const getCurrentScope: typeof import(&#39;vue&#39;)[&#39;getCurrentScope&#39;]
  const h: typeof import(&#39;vue&#39;)[&#39;h&#39;]
  const inject: typeof import(&#39;vue&#39;)[&#39;inject&#39;]
  const isReadonly: typeof import(&#39;vue&#39;)[&#39;isReadonly&#39;]
  const isRef: typeof import(&#39;vue&#39;)[&#39;isRef&#39;]
  // ...
}
export {}
Copy after login

Use

<script setup name="OrderList">
// 不用import,直接使用ref
const count = ref(0)
onMounted(() => {
  console.log(&#39;mounted===&#39;)
})
</script>
Copy after login

Above we only imported vue in the configuration of vite.config.ts, imports: ['vue'], in addition to vue, you also You can import other ones such as vue-router, vue-use, etc. according to the documentation.

Personally, it is recommended to only automatically import some familiar APIs. For example, we are familiar with the vue API during development and can write it with our eyes closed. For some unfamiliar libraries like VueUse, It is better to use import. After all, the editor has prompts and it is not easy to make mistakes.

Solution to eslint error reporting problem

Using it without import will cause eslint to prompt an error reporting. This can be solved by installing the plug-in **vue-global-api** in eslintrc.js.

// 安装依赖
npm i vue-global-api -D
// eslintrc.js
module.exports = {
  extends: [
    &#39;vue-global-api&#39;
  ]
}
Copy after login

3. Farewell to .value

As we all know, ref requires us to add .value when accessing variables, which makes many developers feel uncomfortable.

let count = ref(1)
const addCount = () => {
  count.value += 1
}
Copy after login

This is especially true later. A new syntactic sugar proposal for ref was also submitted.

ref: count = 1
const addCount = () => {
  count += 1
}
Copy after login

This proposal caused a lot of discussion in the community as soon as it came out. It has been a long time, so I will no longer talk nonsense about this topic here.

What I introduce here is another way of writing, which is also an official solution later. Add $ before ref. This function is turned off by default and needs to be turned on manually.

// vite.config.ts
import { defineConfig } from &#39;vite&#39;
import vue from &#39;@vitejs/plugin-vue&#39;
export default defineConfig({
  plugins: [
    vue({
      refTransform: true // 开启ref转换
    })
  ]
})
Copy after login

After turning it on, you can write like this:

let count = $ref(1)
const addCount = () => {
  count++
}
Copy after login

The configuration of this syntax sugar is slightly different according to different versions. Here is the version of the relevant plug-in I use:

"vue": "^3.2.2",
"@vitejs/plugin-vue": "^1.9.0",
"@vue/compiler-sfc": "^3.2.5",
"vite": "^2.6.13"
Copy after login

4. Automatically import images

In Vue2 we often quote images like this:

<img :src="require(&#39;@/assets/image/logo.png&#39;)" />
Copy after login

But require is not supported in Vite, and the quoted images become the following:

<template>
  <img :src="Logo" />
</template>
<script setup>
import Logo from &#39;@/assets/image/logo.png&#39;
</script>
Copy after login

Every time you use an image, you have to import it, which obviously delays everyone's time to fish. At this time, we can use vite-plugin-vue-images to automatically import images.

It feels great, but variable conflicts are easy to occur, so use with caution!

Installation

npm i vite-plugin-vue-images -D
Copy after login

Configuration

// vite.config.ts
import { defineConfig } from &#39;vite&#39;
import ViteImages from &#39;vite-plugin-vue-images&#39;
export default defineConfig({
  plugins: [
    ViteImages({
      dirs: [&#39;src/assets/image&#39;] // 指明图片存放目录
    })
  ]
})
Copy after login

Use

<template>
  <!-- 直接使用 -->
  <img :src="Logo" />
</template>
<script setup>
// import Logo from &#39;@/assets/image/logo.png&#39;
</script>
Copy after login

5. Ignore the .vue suffix

I believe many people ignore the .vue suffix when importing files when developing Vue2. But in Vite, ignoring the .vue suffix will cause an error.

import Home from &#39;@/views/home&#39; // error
import Home from &#39;@/views/home.vue&#39; // ok
Copy after login

According to You Dada’s answer, the requirement to write suffixes is actually deliberately designed this way, that is, everyone is encouraged to write like this.

But if you really don’t want to write, the official support is provided.

// vite.config.ts
import { defineConfig } from &#39;vite&#39;
export default defineConfig({
  resolve: {
    extensions: [&#39;.js&#39;, &#39;.ts&#39;, &#39;.jsx&#39;, &#39;.tsx&#39;, &#39;.json&#39;, &#39;.vue&#39;]
  }
})
Copy after login

It should be noted here that when configuring extensions manually, remember to add the suffix of other types of files, because other types of files such as js can ignore the suffix import by default. If you do not write it, the import of other types of files will be ignored. It becomes necessary to add a suffix.

Although this can be done, the official document says that it is not recommended to ignore the .vue suffix, so it is recommended that you still write .vue honestly in actual development.

[Related recommendations: "vue.js Tutorial"]

The above is the detailed content of Teach you 5 knowledge points to make Vue3 development smoother. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template