Skip to content

Commit 68b20da

Browse files
author
zgxie
committed
vue3 typescript 模板工程
1 parent cc011c7 commit 68b20da

16 files changed

+12965
-2
lines changed

.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.DS_Store
2+
node_modules
3+
/dist
4+
5+
# local env files
6+
.env.local
7+
.env.*.local
8+
9+
# Log files
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
14+
# Editor directories and files
15+
.idea
16+
.vscode
17+
*.suo
18+
*.ntvs*
19+
*.njsproj
20+
*.sln
21+
*.sw?

README.md

+148-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,148 @@
1-
# vue3-typescipt
2-
vue3.x结合typescript环境
1+
## 开始
2+
3+
```
4+
npm install
5+
npm run serve
6+
npm run build
7+
```
8+
9+
vue3.x 结合 typescript 初体验
10+
11+
### 一、Vue3.0 的设计目标
12+
13+
- 更小\更快 - Vue 3.0 大小大概减少一半,只有 10kB
14+
- 加强 TypeScript 支持
15+
- 加强 API 设计一致性 - 易读
16+
- 提高自身可维护性
17+
- 开放更多底层功能
18+
19+
vue3.x 采用 Function-based API 形式组织代码,使其更容易压缩代码且压缩效率也更高,由于 修改了组件的声明方式,以函数组合的方式完成逻辑,天然与 typescript 结合。(vue2.x 中的组件是通过声明的方式传入一系列 options 的,所以在 2.x 下使用 typeScript 需要装饰器的方式`vue-class-component`才行)
20+
21+
```
22+
// vue2.x 要想使用ts 需要这样处理,详见官方文档 https://cn.vuejs.org/v2/guide/typescript.html
23+
<script lang="ts">
24+
import Vue from 'vue'
25+
import Component from 'vue-class-component'
26+
@Component
27+
export default class App extends Vue {}
28+
</script>
29+
```
30+
31+
### 二、typescript 有什么优点
32+
33+
#### 1、增加代码的可读性与可维护性
34+
35+
- 大部分函数看类型定义就知道是干嘛的
36+
- 静态类型检查,减少运行时错误
37+
38+
#### 2、社区活跃,大牛都在用
39+
40+
- **在 vue3 热潮下,之后 typescript 要成为主流,快学**
41+
42+
### 三、搭建 vue3.x + typescript + vue-router 环境
43+
44+
#### 1、全局安装 vue-cli
45+
46+
```
47+
npm install -g @vue/cli
48+
```
49+
50+
#### 2、初始化 vue 项目
51+
52+
```
53+
vue create vue-next-project
54+
```
55+
56+
这里在输入命令后,需要选择`Manually select features`, 至少要把 `babel` `typescript` `router` 选上,(`vuex` 看自身情况是否需要)。如下图:
57+
58+
![](https://user-gold-cdn.xitu.io/2020/5/22/1723b9c80913e36d?w=566&h=237&f=png&s=9590)
59+
60+
不清楚 vue-cli 的可参考[文章](https://juejin.im/post/5e69de93f265da570c75453e)
61+
62+
#### 3、升级为 vue3.x 项目
63+
64+
```
65+
cd vue-next-project
66+
vue add vue-next
67+
```
68+
69+
这个命令会自动帮你把 vue2.x 升级到 vue3.x ,包括项目中对应的依赖进行升级与模板代码替换,像:`vue-router` `vuex`
70+
71+
完成以上三步主体环境算搭建完成,看刚才创建的目录里多了个 tsconfig.json 配置文件,可以根据自身与项目需要,进行配置。
72+
73+
接下来需要简单处理一下,使其支持 typescript 形式。(模板 cli 还没完善 typescript 的模板代码)
74+
75+
![](https://user-gold-cdn.xitu.io/2020/5/22/1723bb003f8fc297?w=965&h=520&f=png&s=30033)
76+
77+
-`shims-vue.d.ts`文件中的内容修改一下,这步操作应该会少了一些报错。
78+
79+
```
80+
// declare module "*.vue" {
81+
// import Vue from 'vue';
82+
// export default Vue;
83+
// }
84+
declare module "*.vue" {
85+
import {defineComponent} from 'vue'
86+
const Component: ReturnType<typeof defineComponent>;
87+
export default Component
88+
}
89+
```
90+
91+
- 组件里使用需声明 `script lang="ts"` ,然后用`defineComponent`进行包裹,即可。
92+
93+
```
94+
<script lang="ts">
95+
import {
96+
defineComponent,
97+
ref,
98+
onMounted,
99+
onUpdated,
100+
onUnmounted,
101+
} from "vue";
102+
103+
export default defineComponent({
104+
name: "hello world",
105+
props: {},
106+
setup(props: any) {
107+
const count = ref(0);
108+
const increase = (): void => {
109+
count.value++;
110+
};
111+
function test(x: number): string {
112+
return props.toString();
113+
}
114+
test(1);
115+
// test('number');
116+
// 生命周期
117+
onMounted(() => {
118+
console.log("mounted vue3 typescript");
119+
});
120+
onUpdated(() => {
121+
console.log("updated vue3 typescript");
122+
});
123+
onUnmounted(() => {
124+
console.log("onUnmounted vue3 typescript");
125+
});
126+
// 暴露给模板
127+
return {
128+
count,
129+
increase
130+
};
131+
},
132+
});
133+
134+
</script>
135+
```
136+
137+
生命周期对应
138+
![](https://user-gold-cdn.xitu.io/2020/5/22/1723bc18105dcd9f?w=488&h=319&f=png&s=15352)
139+
140+
### 四、附上学习 vue-next 与 typescript 的官方秘籍
141+
142+
- [composition-api](https://composition-api.vuejs.org/zh/api.html#setup)
143+
- [typescript](https://www.tslang.cn/)
144+
- [typescript 教程](https://ts.xcatliu.com/introduction/what-is-typescript)
145+
146+
### 五、不想搭建你也可以直接拉去 github demo
147+
148+
[vue3+typescript 环境](xxx)

babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ["@vue/cli-plugin-babel/preset"]
3+
};

0 commit comments

Comments
 (0)