web前端全系列 教程
1839个小节阅读:2243.9k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
从 ECMAScript 2015 开始,JavaScript 引入了模块的概念。TypeScript 也沿用这个概念
模块在其自身的作用域里执行,而不是在全局作用域里;这意味着定义在一个模块里的变量,函数,类等等在模块外部是不可见的,除非你明确地使用export
形式之一导出它们。 相反,如果想使用其它模块导出的变量,函数,类,接口等的时候,你必须要导入它们,可以使用import
形式之一
xxxxxxxxxx
interface Animal{
name:string
}
class Cat implements Animal{
name: string
constructor(name:string){
this.name = name;
}
sayHi(){
console.log(this.name)
}
}
class Dog implements Animal{
name: string
constructor(name:string){
this.name = name
}
sayHello(){
console.log(this.name)
}
}
const c = new Cat("猫")
c.sayHi()
const d = new Dog("狗")
d.sayHello()
我们用模块化的形式实现
xxxxxxxxxx
// Animal.ts
export interface Animal{
name:string
}
xxxxxxxxxx
// Cat.ts
import { Animal } from "./Animal"
export class Cat implements Animal{
name: string
constructor(name:string){
this.name = name;
}
sayHi(){
console.log(this.name)
}
}
xxxxxxxxxx
// Dog.ts
import { Animal } from "./Animal"
export class Dog implements Animal{
name: string
constructor(name:string){
this.name = name
}
sayHello(){
console.log(this.name)
}
}
xxxxxxxxxx
// index.ts
import { Cat } from "./Cat"
import { Dog } from "./Dog"
const c = new Cat("猫")
c.sayHi()
const d = new Dog("狗")
d.sayHello()
实时效果反馈
1. 在TypeScript中,实现模块化导入的关键字是:
A export
B import
C default
D namespace
答案
1=>B