模块
export 导出
每一个.ts
都应该加一个export {}
,否则定义变量、函数、类的时候会提示Duplicate identifier
的报错
class Person{
public name:string;
public age:number;
public phone:string;
constructor(name:string,age:number,phone:string){
this.name = name;
this.age = age;
this.phone = phone;
}
public eat(who:string):void{
console.log(this.name+'和'+who+'在一起吃饭');
}
}
const xiaohong = new Person('小红',23,'18235130928');
xiaohong.eat('小白');
export {}
import 导入
import 重命名导入
home.ts
export type ListType = {
label: string;
}
me.ts
export type ListType = {
name: string;
}
index.ts
import { ListType as HomeListType } from './home'
import { ListType as MeListType } from './me'
const list1: HomeListType = {
label: '哈哈'
}
const list2: MeListType = {
name: '嘻嘻'
}
console.log(list1);
console.log(list2);
import 导入整个模块为一个变量
home.ts
export type ListType = {
label: string;
}
me.ts
export type ListType = {
name: string;
}
index.ts
import * as HomeListType from './home'
import * MeListType from './me'
const list1: HomeListType.ListType = {
label: '哈哈'
}
const list2: MeListType.ListType = {
name: '嘻嘻'
}
console.log(list1);
console.log(list2);