Skip to content

Commit 8b5d9ed

Browse files
committedMay 18, 2022
初始阶段完善代码格式化配置和工具链
1 parent 41b85c5 commit 8b5d9ed

8 files changed

+132
-6
lines changed
 

‎.eslintrc.js

Whitespace-only changes.

‎.prettierignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/dist/*
2+
.local
3+
.output.js
4+
/node_modules/**
5+
6+
**/*.svg
7+
**/*.sh
8+
9+
/public/*

‎.prettierrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"trailingComma": "none",
5+
"printWidth": 80,
6+
"endOfLine": "lf",
7+
"overrides": [
8+
{
9+
"files": ".prettierrc",
10+
"options": {
11+
"parser": "json"
12+
}
13+
}
14+
]
15+
}

‎note.md

+88-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
文件内容:笔记(包含 vue3、ts、vite等相关内容
1+
文件内容:笔记(包含 vue3、ts、vite 等相关内容
22

33
# Vue 3 + TypeScript + Vite
44

@@ -17,8 +17,7 @@ Since TypeScript cannot handle type information for `.vue` imports, they are shi
1717

1818
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
1919

20-
21-
一、单文件组件 `<script setup>`
20+
## 一、单文件组件 `<script setup>`
2221

2322
`<script setup>` 是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖。相比于普通的 `<script>` 语法,它具有更多优势:
2423

@@ -27,3 +26,89 @@ You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/
2726
- 更好的运行时性能 (其模板会被编译成与其同一作用域的渲染函数,没有任何的中间代理)。
2827
- 更好的 IDE 类型推断性能 (减少语言服务器从代码中抽离类型的工作)。
2928

29+
## 二、UI 框架引入的三种方式利弊分析总结
30+
31+
- 1、全局完整注册
32+
33+
```
34+
import Antd from 'ant-design-vue';
35+
import 'ant-design-vue/dist/antd.css';
36+
```
37+
38+
以上代码便完成了 Antd 的全局注册。需要注意的是,样式文件需要单独引入。
39+
40+
- 2、全局部分注册
41+
42+
```
43+
import { Button, message } from 'ant-design-vue';
44+
/* 会自动注册 Button 下的子组件, 例如 Button.Group */
45+
app.use(Button).mount('#app');
46+
app.config.globalProperties.$message = message;
47+
```
48+
49+
- 3、局部注册组件
50+
【重要说明】 此种方式需要分别注册组件子组件,如 Button、ButtonGroup,并且注册后仅在当前组件中有效。所以我们推荐使用上述两种方式。
51+
52+
```
53+
<template>
54+
<a-button>Add</a-button>
55+
</template>
56+
<script>
57+
import { Button } from 'ant-design-vue';
58+
const ButtonGroup = Button.Group;
59+
60+
export default {
61+
components: {
62+
AButton: Button,
63+
AButtonGroup: ButtonGroup,
64+
},
65+
};
66+
</script>
67+
```
68+
69+
## 三、按需加载
70+
71+
如果你仅需要加载使用的组件,可以通过以下的写法来按需加载组件。
72+
73+
```
74+
import Button from 'ant-design-vue/lib/button';
75+
import 'ant-design-vue/lib/button/style'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
76+
```
77+
78+
如果你使用了 babel,那么可以使用 `babel-plugin-import` 来进行按需加载,加入这个插件后。你可以仍然这么写:
79+
80+
```
81+
import { Button } from 'ant-design-vue';
82+
```
83+
84+
插件会帮你转换成 ant-design-vue/lib/xxx 的写法。另外此插件配合 style 属性可以做到模块样式的按需自动加载。
85+
86+
> 注意,babel-plugin-import 的 style 属性除了引入对应组件的样式,也会引入一些必要的全局样式。如果你不需要它们,建议不要使用此属性。你可以 import 'ant-design-vue/dist/antd.css 手动引入,并覆盖全局样式。
87+
88+
【总结:】既然这样的话,那不管是全量引入还是按需加载,样式文件干脆直接 `import 'ant-design-vue/dist/antd.css` 手动全量引入全局样式。
89+
90+
如果你使用的 Vite,你可以使用 `unplugin-vue-components` 来进行按需加载。但是此插件无法处理非组件模块,如 message,这种组件需要手动加载:
91+
92+
```
93+
import { message } from 'ant-design-vue';
94+
import 'ant-design-vue/es/message/style/css'; //vite只能用 ant-design-vue/es 而非 ant-design-vue/lib
95+
```
96+
97+
什么是 `Vite` 的按需加载?就是在 `vite.config.js` 中进行配置,配了以后,你就可以直接使用 `antd-vue` 里面的组件了,`vite` 会自动知道你引入了哪些组件,但就像上面所说的,无法处理非组件模块。
98+
99+
```
100+
// vite.config.js
101+
import Components from 'unplugin-vue-components/vite';
102+
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
103+
104+
export default {
105+
plugins: [
106+
/* ... */
107+
Components({
108+
resolvers: [AntDesignVueResolver()],
109+
}),
110+
],
111+
};
112+
```
113+
114+
【总结:】如果确实想要使用按需加载的话,借助 `babel-plugin-import` 是一个很好的方案吧,但还是那句话,样式还是全量引入吧。

‎src/App.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import HelloWorld from './components/HelloWorld.vue'
55
</script>
66

77
<template>
8-
<HelloWorld />
8+
<ConfigProvider>
9+
<HelloWorld />
10+
</ConfigProvider>
911
</template>
1012

1113
<style>

‎src/components/HelloWorld.vue

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script setup lang="ts">
22
import { ref } from 'vue'
33
4-
defineProps<{ msg: string }>()
54
65
const count = ref(0)
76
</script>

‎src/main.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { createApp } from 'vue'
2+
import Antd from 'ant-design-vue'
23
import App from './App.vue'
4+
// 引入less后缀的样式文件,方便后续更改主题颜色
5+
import 'ant-design-vue/dist/antd.less'
36

4-
createApp(App).mount('#app')
7+
const app = createApp(App)
8+
app.use(Antd)
9+
app.mount('#app')

‎vscode-eslint-prettier.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# vscode+eslint+prettier 超详细配置使用手册
2+
3+
【文章目标】:彻底搞明白,1、在 VSCode 中如何安装插件 `eslint``prettier`,来进行代码检查、和代码格式化(美化);2、以及如何在项目中优雅的新增`eslint`配置文件(`.eslintrc.js`)和`prettier`配置文件(`.prettierrc`)来增强(自定义)代码检查功能和代码格式化功能。
4+
5+
`eslint``prettier` 是不同(不一样)功能的两个工具;因为它们有一部分相似性,所以很多同学经常搞混它们;也经常搞混项目中的`eslint`配置文件(`.eslintrc.js`)和`prettier`配置文件(`.prettierrc`)的使用区别;以及 VSCode 插件`prettier`与项目中`prettier`配置文件(`.prettierrc`)之间的使用关系,以及如何搭配使用。
6+
7+
最终实现,保存代码时,自动格式化代码。
8+
9+
要做的就是,首先得在 VSCode 中配置 eslint+prettier(一劳永逸),这是基础。
10+
11+
其次,如果你有团队合作(多人参与写代码);或者,对默认的vscode插件prettier的格式化不是很满意,你可以在项目中增加`prettier`配置文件(`.prettierrc`)来自定义更多规则来增强vscode中你安装的prettier插件的不足。

0 commit comments

Comments
 (0)
Please sign in to comment.