Skip to content

Commit 1311ec0

Browse files
author
徐梦强
committed
22
1 parent fc8d2ac commit 1311ec0

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

case/No4.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,73 @@ export default defineConfig({
101101
},
102102
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
103103
}
104-
```
104+
```
105+
106+
## unplugin 自动导入
107+
108+
> 现有的组件库都可以按需导入了,像`Element Plus``Vant`
109+
110+
- 为了避免在多个页面重复引入 API 或 组件,由此而产生的自动导入插件来节省重复代码和提高开发效率。
111+
112+
| 插件 | 概念 | 自动导入对象 |
113+
| ------------- | :-----------: | ----: |
114+
| unplugin-auto-import | 按需自动导入API | ref,reactive,watch,computed 等API |
115+
| unplugin-vue-components | 按需自动导入组件 | Element Plus 等三方库和指定目录下的自定义组件 |
116+
117+
- 看下自动导入插件未使用和使用的区别:
118+
119+
120+
| 插件名 | 未使用自动导入 | 使用自动导入 |
121+
| ------------- | :-----------: | ----: |
122+
| unplugin-auto-import | ![image](https://github.com/squid-Xu/picx-images-hosting/raw/master/image.8ojnip7fho.webp) | ![image](https://github.com/squid-Xu/picx-images-hosting/raw/master/image.esgujl8wo.webp) |
123+
| unplugin-vue-components | ![image](https://github.com/squid-Xu/picx-images-hosting/raw/master/image.4qra20z83y.webp) | ![image](https://github.com/squid-Xu/picx-images-hosting/raw/master/image.2vep9gsk80.webp) |
124+
125+
##### 安装插件依赖
126+
127+
```sh
128+
npm install -D unplugin-vue-components unplugin-auto-import
129+
```
130+
131+
##### vite.config.ts - 自动导入配置
132+
133+
```ts
134+
import { defineConfig } from 'vite';
135+
import vue from '@vitejs/plugin-vue';
136+
137+
// 引入path模块
138+
import path from 'path';
139+
140+
import AutoImport from 'unplugin-auto-import/vite'; // [!code ++]
141+
import Components from 'unplugin-vue-components/vite'; // [!code ++]
142+
143+
// https://vitejs.dev/config/
144+
export default defineConfig({
145+
plugins: [
146+
vue(),
147+
AutoImport({ // [!code ++]
148+
// 自动导入 Vue 相关函数,如:ref, reactive, toRef 等 // [!code ++]
149+
imports: ['vue'], // [!code ++]
150+
dts: path.resolve(path.resolve(__dirname, 'src'), 'auto-imports.d.ts'), // 指定自动导入函数TS类型声明文件路径 // [!code ++]
151+
}), // [!code ++]
152+
Components({ // [!code ++]
153+
dts: path.resolve(path.resolve(__dirname, 'src'), 'components.d.ts'), // 指定自动导入组件TS类型声明文件路径 // [!code ++]
154+
}), // [!code ++]
155+
],
156+
//路径别名
157+
resolve: {
158+
alias: {
159+
'@': path.resolve(__dirname, 'src'),
160+
},
161+
},
162+
});
163+
```
164+
165+
##### 自动导入效果
166+
167+
- 运行项目 `npm run dev` 自动
168+
169+
![image](https://github.com/squid-Xu/picx-images-hosting/raw/master/image.9rjctnu5wv.webp)
170+
171+
172+
## 整合 Element Plus
173+
## 整合 Vant

0 commit comments

Comments
 (0)