Skip to content

Commit ec81f04

Browse files
update readme
1 parent b98b248 commit ec81f04

File tree

2 files changed

+149
-10
lines changed

2 files changed

+149
-10
lines changed

README.md

+148-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
<div align=center>
3-
<img src="https://p1.meituan.net/dpgroup/60168751a8f4a79ca3c59edffdde00e423571.jpg" width="92"/>
4-
<img src="https://p0.meituan.net/dpgroup/4a48f7ad4e6231c12c44b3422d67008116085.jpg" width="80"/>
3+
<img src="https://p1.meituan.net/dpgroup/60168751a8f4a79ca3c59edffdde00e423571.jpg" width="126"/>
4+
<img src="https://p0.meituan.net/dpgroup/4a48f7ad4e6231c12c44b3422d67008116085.jpg" width="110"/>
55
</div>
66

77
## 功能
@@ -21,7 +21,7 @@
2121
- [x] mock数据
2222
- [x] tsconfig.json
2323
- [x] webpack配置
24-
- [ ] vue-typecript-cli
24+
- [ ] vue-typescript-cli
2525

2626
## 完成后的简单例子
2727
基于类的写法加上静态类型检查,简直不能再嗨
@@ -50,23 +50,28 @@ export default class Shops extends Vue {
5050

5151
# 为什么使用TypeScript
5252

53+
### JavaScript的超集
54+
支持所有原生JavaScript的语法
5355
### 强类型语言
5456
现在很多主流语言都是强类型的,而这点也一直是JavaScript所被人诟病的地方。使用TypeScript之后,将会在代码调试、重构等步骤节省很多时间。
57+
58+
> 比如说:函数在返回值的时候可能经过复杂的操作,那我们如果想要知道这个值的结构就需要去仔细阅读这段代码。那如果有了TypeScript之后,直接就可以看到函数的返回值结构,将会非常的方便
59+
5560
### 强大的IDE支持
5661
现在的主流编辑器如`VSCode``WebStorm``Atom``Sublime`等都对TypeScript有着非常友好的支持,主要体现在智能提示上,非常的方便
5762
### 可运行于任何浏览器、计算机、操作系统
5863
强大的编译引擎
5964
### 迭代更新快
6065
不断更新,提供更加方便友好的Api
6166
### 微软和Google爸爸
62-
TypeScript是微软开发的语言,而Google的Angular使用的就是TypeScript,所以不用担心会停止维护,至少在近几年内TypeScript都会一门主流开发语言
67+
TypeScript是微软开发的语言,而Google的`Angular`使用的就是TypeScript,所以不用担心会停止维护,至少在近几年内TypeScript都会一门主流开发语言
6368
### npm下载量非常高
64-
截止2017.12.17, TypeScript在全球范围内的npm日均下载量在30w左右,这个数字将近是vue下载量的10倍,可见TypeScript还是非常受欢迎的
69+
截止2017.12.17, TypeScript在全球范围内的npm日均下载量在`30w`左右,这个数字将近是vue下载量的10倍,可见TypeScript还是非常受欢迎的
6570

6671
# TypeScript配置
6772
本项目已经配置完毕,这里记录一下当时的踩坑过程
6873

69-
## Webpack
74+
## 1. Webpack
7075
首先需要安装`ts-loader`,这是TypeScript为Webpack提供的编译器,类似于`babel-loader`
7176

7277
```bash
@@ -93,7 +98,7 @@ npm i ts-loader -D
9398
}
9499
```
95100

96-
## tsconfig.json
101+
## 2. tsconfig.json
97102

98103
创建tsconfig.json文件,放在根目录下,和`package.json`同级
99104

@@ -156,13 +161,13 @@ npm i ts-loader -D
156161
}
157162
```
158163

159-
## 修改main.js
164+
## 3. 修改main.js
160165
1. 把项目主文件`main.js`修改成`main.ts`,里面的写法基本不变,但是有一点需要注意:
161166
引入Vue文件的时候需要加上`.vue`后缀,否则编辑器识别不到
162167

163168
2. 把webpack的entry文件也修改成`main.ts`
164169

165-
## vue-shims.d.ts
170+
## 4. vue-shims.d.ts
166171
TypeScript并不支持Vue文件,所以需要告诉TypeScript`*.vue`文件交给vue编辑器来处理。解决方案就是在创建一个vue-shims.d.ts文件,建议放在src目录下再创建一个`typings`文件夹,把这个声明文件放进去,如:`src/typings/vue-shims.d.ts`,文件内容:
167172

168173
> `*.d.ts`类型文件不需要手动引入,TypeScript会自动加载
@@ -173,6 +178,134 @@ declare module '*.vue' {
173178
export default Vue
174179
}
175180
```
181+
182+
到这里TypeScript在Vue中配置就完成了,可以愉快的撸代码了~
183+
184+
# 第三方插件库
185+
现在Vue官方已经明确提出支持TypeScript,并考虑出一个对应的`vue-cli`,在这之前,Vue开发团队已经开发出了一些插件库来支持TypeScript,这里简单和大家介绍一下。
186+
187+
### Vue-Class-Component
188+
[vue-class-component](https://github.com/vuejs/vue-class-component)是官方维护的TypeScript装饰器,写法比较扁平化。Vue对其做到完美兼容,如果你在声明组件时更喜欢基于类的 API,这个库一定不要错过
189+
190+
ps:用了这个装饰器之后写方法不需要额外加逗号,贼嗨~~~
191+
192+
```javascript
193+
import Vue from "vue";
194+
import Component from "vue-class-component";
195+
196+
@Component
197+
export default class App extends Vue {
198+
name:string = 'Simon Zhang'
199+
200+
// computed
201+
get MyName():string {
202+
return `My name is ${this.name}`
203+
}
204+
205+
// methods
206+
sayHello():void {
207+
alert(`Hello ${this.name}`)
208+
}
209+
210+
mounted() {
211+
this.sayHello();
212+
}
213+
}
214+
```
215+
这个代码如果用原生Vue语法来写的话就是这样:
216+
217+
```javascript
218+
export default {
219+
data () {
220+
return {
221+
name: 'Simon Zhang'
222+
}
223+
},
224+
225+
mounted () {
226+
this.sayHello()
227+
},
228+
229+
computed: {
230+
MyName() {
231+
return `My name is ${this.name}`
232+
}
233+
},
234+
235+
methods: {
236+
sayHello() {
237+
alert(`Hello ${this.name}`)
238+
},
239+
}
240+
}
241+
```
242+
243+
### Vuex-Class
244+
[vuex-class](https://github.com/ktsn/vuex-class)是基于基于`vue-class-component`对Vuex提供的装饰器。它的作者同时也是`vue-class-component`的主要贡献者,质量还是有保证的。
245+
246+
```javascript
247+
import Vue from "vue";
248+
import Component from "vue-class-component";
249+
import { State, Action, Getter } from "vuex-class";
250+
251+
@Component
252+
export default class App extends Vue {
253+
name:string = 'Simon Zhang'
254+
@State login: boolean;
255+
@Action initAjax: () => void;
256+
@Getter load: boolean;
257+
258+
get isLogin(): boolean {
259+
return this.login;
260+
}
261+
262+
mounted() {
263+
this.initAjax();
264+
}
265+
}
266+
```
267+
上面的代码就相当于:
268+
269+
```javascript
270+
export default {
271+
data() {
272+
return {
273+
name: 'Simon Zhang'
274+
}
275+
},
276+
277+
mounted() {
278+
this.initAjax()
279+
},
280+
281+
computed: {
282+
login() {
283+
return this.$store.state.login
284+
},
285+
load() {
286+
return this.$store.getters.load
287+
}
288+
},
289+
290+
methods: {
291+
initAjax() {
292+
this.$store.dispatch('initAjax')
293+
}
294+
}
295+
}
296+
```
297+
298+
### Vue-Property-Decorator
299+
[vue-property-decorator](https://github.com/kaorun343/vue-property-decorator) 是在 vue-class-component 上增强了更多的结合 Vue 特性的装饰器,新增了这 7 个装饰器
300+
301+
- `@Emit`
302+
- `@Inject`
303+
- `@Model`
304+
- `@Prop`
305+
- `@Provide`
306+
- `@Watch`
307+
- `@Component` (从 vue-class-component 继承)
308+
176309
#
177310
### 引入部分第三方库的时候需要额外声明文件
178311
比如说我想引入`vue-lazyload`,虽然已经在本地安装,但是typescript还是提示找不到模块。原因是typescript是从`node_modules/@types`目录下去找模块声明,有些库并没有提供typescript的声明文件,所以就需要自己去添加
@@ -207,6 +340,12 @@ export default class modules extends Vue {
207340
}
208341
}
209342
```
343+
# 总结
344+
TypeScript还是非常值得学习和使用一个语言,还是有很多优点的
345+
346+
欢迎大家对我的项目提建议,欢迎Star~
347+
348+
QQ交流群:323743292
210349
## Build Setup
211350

212351
``` bash

src/views/main.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class App extends Vue {
4040
get isLogin(): boolean {
4141
return this.login;
4242
}
43-
// 声明周期钩子
43+
4444
mounted() {
4545
this.initAjax();
4646
}

0 commit comments

Comments
 (0)