导出的关键字 : export + 类名
举例:
export class Name {...}
导入的关键字: import { 类名或模块名称 } from 文件相对地址
举例:
import { Name, Location } from './userinfo';
对于导入模块名称重复的处理:
重命名: import { 类名 as 新类名 } from 文件相对地址
举例:
import { Name } from './userInfo';
import { Name as DepartmentName } from './departmentInfo';
整体导入:
通过关键字 * 整体导入所有允许导入的类,调用时通过.属性名的方式访问
举例:
import * as info from './userinfo';
name = new info.Name();
age = new info.Age();
类的继承
export class Name {
name;
// 构造函数中初始化数据
constructor(name) {
this.name = name;
}
// 对外开放的访问方法
getName() {
console.log(`My Name Is ${this.name}`);
}
}
通过super关键字,子类可以继承父类的构造,构造参数 以及
import { Name } from './userinfo';
export class UserInfo extends Name {
age;
constructor(name, age) {
super(name);
this.age = age;
}
getUserInfo() {
super.getName();
console.log('My Age Is ${this.age}');
}
}