16命名空间
# 命名空间
- 命名空间里面的方法或者类需要
export暴露出来,否则外部无法使用
namespace A{
export class Person{
constructor(name:string){
this.name = name
}
name:string;
}
}
console.log(new A.Person("小明"))
namespace B{
export class Person{
constructor(name:string,age:number){
this.name = name
this.age = age
}
name:string;
age:number
}
}
console.log(new B.Person("小红",18))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- 输出和引入命名空间
//index.ts
export namespace A{
export class Person{
constructor(name:string){
this.name = name
}
name:string;
}
}
//other.ts
import {A} from "./index"
new A.Person("小明")
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41