typescript基础类型
基础类型
布尔型
、数字型
、字符串
1 2 3 4 5 6 7 8 9
| let boo: boolean = true; bool = 111;
let num: number = 111;
let str: string = 'simplelife';
|
引用类型
Array类型
、Object类型
、Fcuntion类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| let arr: Array<number> = [1,2,3,4]; let arr2: string[] = ['a','b']
function func(arg: number): void { }
function func2(): boolean { return true }
|
对象类型:更加复杂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| interface IPerson { name: string; age: number; sex: string; }
let obj: IPerson = { name: "simplelife", age: 20, sex: "男" } let obj2: IPerson = {}
interface IPerson2 { name?: string; age?: number; sex?: string; } let person12: IPerson2 = {}
interface IPerson3 { [propName: string] : any; }
|
接口继承
1 2 3 4 5 6 7 8 9 10 11 12
| interface IA extends IPerson { hobby:Array<string>; } let person2: IA = { }
interface IB extends IA,IPerson2 { }
|
特殊类型
Undefined类型
、Null类型
1 2 3 4 5
| let _undefined: undefined = undefined;
let _null: null = null;
|
多重类型声明 或 |
1 2
| let some_var: number | string | boolean = 123; some_var = true
|
any
类型
不进行数据校验,等同于 js
与 &
类型
1 2 3 4
| let person3: IA & IB = { }
|
接口实现 implements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| interface IPerson4 { name: string; age: number; sex: string; getName (): string; getAge: () => string; }
class Person implements IPerson4 { name: string = "simplelife"; age: number = 20; sex: string; getName (): string { return this.name; }; getAge (): number { return this.age; }; sayName (): void { } }
|
类型断言
1 2 3 4 5 6 7 8 9 10 11
| function getLength (str: number | string): number { if((str as string).length){ return (str as string).length return (<string>str).length } else { return str.toString().length } }
|
非空断言 !
1 2 3 4
| function func3 (arg?: string): number { return arg!.length } func3()
|
枚举
1 2 3 4 5 6 7
| enum Color { red, green, yellow } console.log(Color,red) console.log(Color,green)
|
反向映射
1 2 3 4 5 6 7 8 9 10 11
| console.log(Color[0])
{ red: 0, green: 1, yellow: 2, 0: 'red', 1: 'yellow', 2: 'green' }
|
赋值枚举
1 2 3 4 5 6 7 8 9 10 11 12
| enum Color2 { red = 1, green, yellow }
enum Color2 { red, green = 'life', yellow = 3 }
|
泛型
1 2 3 4 5 6
| function func4<T>(arg: T): void { }
func4<number>(44); func4<string>('simple');
|
Vue组件写法
构建 vue + typescript 项目,在 src 会新增 shims-tsx.d.ts
和 shims-vue.d.ts
文件,用于处理后缀名 .ts
文件,和 vue组件处理 ts
语法
vue组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import {Component,Vue} from 'vue-property-decorator';
@Component({ name:'Home' }) export default class Home extends Vue { username: string = "simplelife"; get fullName(){ return this.username } set fullName(val){ this.username = val; } modifyName(){} getName(){} }
|
Watch
侦听器
1 2 3 4 5 6 7 8 9 10 11 12 13
| import {Component,Vue,Watch} from 'vue-property-decorator';
age: number = 18; sex: string = "男";
@Watch('age') changeAge(newValue,oldValue){ }
|
Prop
父子通信
父组件
通过 v-bind="item"
将该对象item的属性值,全都传入到子组件使用
1 2 3 4 5
| <template> <div> <son v-for="item of userList" :key="item.id" v-bind="item"></son> </div> </template>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import {Component,Vue} from 'vue-property-decorator';
interface IUser { id: number; name: string; age: number; sex?: number; }
@Component({ name: "Father", components:{ Son } }) export default class extends Vue { userList: Array<IUser> = [ { id: 1 name: "张三", age: 20, sex: 1 }, { id: 1 name: "李四", age: 18, sex: 1 }, { id: 1 name: "王五", age: 21, sex: 0 } ] }
|
子组件 Son
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Compoment export default class extends Vue { @Prop(Number) id!: number; @Prop([String,Number]) name!: string | number;
@Prop({type: Number}) age!: number;
@Prop({type: Number, default: 1}) sex!: number; }
|
Emit
绑定事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import {Component, Vue, Emit} from 'vue-property-decorator';
@Component export default class extends Vue{ @Emit('on-remove') remove(){ console.log('当触发`on-remove`事件,执行remove函数'); return value } @Emit() deleteName(){ } }
|
组件绑定事件
1 2 3 4 5 6 7
| <child @on-remove="fatherRemove"></child>
fatherRemove(val){ console.log(val) }
|
Model
双向绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| firName:<input type="text" :value="firName" @input="onChangeName">
{ @Model('changeName',{type: String}) firName!: string;
@Emit('changeName') onChangeName(e){ return e.target.value } }
|
父组件
1 2 3
| <template> <child v-model="firName"></child> </template>
|
生命周期函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import {Component,Vue} from 'vue-property-decorator'; @Component export default class extends Vue { beforeCreate(){} created(){} beforeMount(){} mounted(){} beforeUpdate(){} updated(){} beforeDestroy(){} destoryed() }
|
Vuex
typescript 写法
./store/index.ts
模块集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import Vue from 'vue'; import Vux from 'vuex';
Vue.use(Vuex);
import { IAboutState } from './module/about';
interface TRootState { about: IAboutState; } export default new Vuex.Store<TRootState>({ modules: {
} })
|
./store/module/about.ts
一个模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import {Module, VuexModule, Mutataion, Action, getModule} from 'vuex-module-decorators';
import store from '../index.ts';
export interface IAboutState{ count: number; list: Array<number>; }
@Module({ name: 'about', dynamic: true, store }) export default class About extends VuexModule implements IAboutState { count: number = 1; list: Array<number> = [1,2,3,4]; get filterList () { return this.number } @Mutation updateCount(payLoad: number){ this.count += payLoad; } @Action async getList (payLoad?: any){ const res = await getName(); } }
export const AboutStore = getModule(About)
|
组件使用
1 2 3 4 5 6 7 8 9 10
| import { AboutStore } from "@/component/menu"; { get count(){ return AboutStore.count } get list(){ return AboutStore.filterList } }
|