「前端+鸿蒙」鸿蒙应用开发-TS-模块化

在 TypeScript 中,模块化是一种将代码分割成独立单元的方式,每个单元可以封装特定的功能或数据。模块化有助于保持代码的组织性、可维护性和可重用性。TypeScript 支持 ES6 模块标准,这意味着你可以使用 importexport 语句来创建模块。

TS快速入门-模块化

导出模块成员

使用 export 关键字可以将类、接口、类型、函数或变量导出,以便它们可以在其他文件中被导入。

// myModule.ts
export class Calculator {
    private value: number = 0;

    public add(x: number): void {
        this.value += x;
    }

    public getValue(): number {
        return this.value;
    }
}

// 或者使用 '*' 来导出所有
export * from './someModule';
导入模块成员

使用 import 关键字可以从其他文件导入导出的成员。

// app.ts
import { Calculator } from './myModule';

let calc = new Calculator();
calc.add(10);
console.log(calc.getValue()); // 输出 10
重命名导入和导出

在导入或导出时,你可以使用 as 关键字来重命名模块成员。

// myModule.ts
export class SomeUtility {
    // ...
}

// app.ts
import { SomeUtility as Utility } from './myModule';

let util = new Utility();
默认导出

每个模块可以有一个默认导出,这使得导入时不需要使用花括号。

// myDefaultModule.ts
export default class DefaultClass {
    // ...
}

// app.ts
import DefaultImportedClass from './myDefaultModule';
let defaultInstance = new DefaultImportedClass();
模块解析

TypeScript 支持不同的模块解析策略,如 CommonJSAMD 等,但默认使用 ES6 模块解析。

// 使用相对路径或绝对路径导入
import SomeClass from './someClass';
import SomeOtherClass from '/path/to/someOtherClass';
模块化示例

以下是一个模块化的示例,展示了如何在 TypeScript 中创建和使用模块:

// calculator.ts
export class Calculator {
    private value: number = 0;

    public add(x: number): void {
        this.value += x;
    }

    public subtract(x: number): void {
        this.value -= x;
    }

    public getValue(): number {
        return this.value;
    }
}

// shape.ts
export interface Shape {
    area(): number;
}

// circle.ts
import { Shape } from './shape';

export class Circle implements Shape {
    private radius: number;

    constructor(radius: number) {
        this.radius = radius;
    }

    area(): number {
        return Math.PI * this.radius * this.radius;
    }
}

// app.ts
import { Calculator } from './calculator';
import { Circle } from './circle';

let calc = new Calculator();
calc.add(5);
calc.subtract(3);
console.log(calc.getValue()); // 输出 2

let circle = new Circle(10);
console.log(circle.area()); // 输出 314.159...

TypeScript 中模块化的基本用法,包括如何导出和导入模块成员、如何使用默认导出和重命名导入/导出。模块化是现代 JavaScript 和 TypeScript 开发中的核心概念,它有助于构建大型、可维护的应用程序。

相关推荐

  1. 前端+鸿蒙鸿蒙应用开发-TS-模块

    2024-06-13 08:34:05       11 阅读
  2. 前端+鸿蒙鸿蒙应用开发-TS函数

    2024-06-13 08:34:05       9 阅读
  3. 前端+鸿蒙鸿蒙应用开发-TS声明和数据类型

    2024-06-13 08:34:05       11 阅读
  4. 前端+鸿蒙鸿蒙应用开发-TS接口-特殊用途

    2024-06-13 08:34:05       10 阅读
  5. 前端+鸿蒙鸿蒙应用开发简介

    2024-06-13 08:34:05       9 阅读
  6. 前端+鸿蒙鸿蒙应用开发-布局

    2024-06-13 08:34:05       10 阅读
  7. 前端+鸿蒙鸿蒙应用开发预览&模拟器运行

    2024-06-13 08:34:05       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 08:34:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 08:34:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 08:34:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 08:34:05       20 阅读

热门阅读

  1. 个人制作软件是否需要代码签名证书?

    2024-06-13 08:34:05       9 阅读
  2. 数据结构-二叉树

    2024-06-13 08:34:05       7 阅读
  3. C++ 线程的使用以及线程安全--学习笔记1

    2024-06-13 08:34:05       5 阅读
  4. 深入理解JVM类加载器与双亲委派模型机制

    2024-06-13 08:34:05       6 阅读
  5. Apache 网站服务基础

    2024-06-13 08:34:05       4 阅读
  6. Ubuntu根分区在线扩容

    2024-06-13 08:34:05       8 阅读
  7. 小程序中的模版语法

    2024-06-13 08:34:05       5 阅读