当前位置:网站首页>Modularity in typescrtipt
Modularity in typescrtipt
2022-06-09 06:15:00 【BloggerM】
TypeScrtipt Modularity in
Directory structure

First initialize the project :
// Input at the console npm init Choose the appropriate option ( Keep going back ) npm initPartial installation
typescript// Input at the console npm i typescrtiptSet auto compilation
typescript// Console input npm i ts-node-dev --save-devpackages.jsonAdd script to{ "name": "demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "ts-node-dev --respawn --transpile-only index.ts" }, "author": "", "license": "ISC", "dependencies": { "typescript": "^4.7.2" }, "devDependencies": { "ts-node-dev": "^2.0.0" } }Use... In terminals
npm startstart-up
New folder
src- Folder
srcNext, create a new foldermodularization - Create a new file under folder modularization
foo.tsandmian.ts
- Folder
Create a new file at the root
index.tsAs an entry fileimport "./src/ modularization /main";tsModularization case 1 ( Unified export , Deconstruction import )stay
srcUnder the foldermodularizationUnder the folderfoo.tsinconst username = "admin"; const sum = (a: number, b: number): number => { return a + b; }; // Unified export export { sum, username };stay
srcUnder the foldermodularizationUnder the foldermain.tsinimport { username, sum} from "./foo"; console.log(username); console.log(sum(10, 20));Print result display

tsModularization case 2 ( Export... Separately , Deconstruction import )stay
srcUnder the foldermodularizationUnder the folderfoo.tsin// Export... Separately export const sayHello = (name: string): void => { console.log(name); };stay
srcUnder the foldermodularizationUnder the foldermain.tsinimport { sayHello } from "./foo"; sayHello(" Zhang San ");Print result display

tsModularization case III ( Export uniformly or separately ) As an objectstay
srcUnder the foldermodularizationUnder the folderfoo.tsinconst username = "admin"; const sum = (a: number, b: number): number => { return a + b; }; // Unified export export { sum, username }; // Export... Separately export const sayHello = (name: string): void => { console.log(name); };stay
srcUnder the foldermodularizationUnder the foldermain.tsin// Make up an alias foo import * as foo from "./foo"; console.log(foo); console.log(foo.username); foo.sayHello(" Zhang San ");Print result display

tsModularization case 4 : The default is derived ( There can only be one default export in a file )stay
srcUnder the foldermodularizationUnder the folderfoo.tsin// The default is derived export default function () { console.log(" The default is derived "); }stay
srcUnder the foldermodularizationUnder the foldermain.tsinimport a from "./foo"; a();Print result display

tsModularization case 5 : Import the default exported and unified ( A separate ) Derivedstay
srcUnder the foldermodularizationUnder the folderfoo.tsinconst username = "admin"; const sum = (a: number, b: number): number => { return a + b; }; // Unified export export { sum, username }; // Export... Separately export const sayHello = (name: string): void => { console.log(name); }; // The default is derived export default function () { console.log(" The default is derived "); }stay
srcUnder the foldermodularizationUnder the foldermain.tsinimport a, { username, sum, sayHello } from "./foo"; a(); console.log(username); console.log(sum); sayHello(" Zhang San ");Print result display

tsModular case 6 :exportandimportUse a combination ofCreate a new directory structure
Directory structure display

stay
formatDate.tsin/** * Format time * @param date * @returns date */ export const formatDate = (date: Date): string => { return date.toLocaleDateString(); };stay
formatString.tsinexport const formatString = (a: any) => { return a.toString(); };stay
math.tsinexport const random = () => { return Math.random(); };stay
Module twoIn a folderindex.tsin ( Do unified export )export * from "./formatDate"; export * from "./formatString"; export * from "./math";In the root directory
index.tsin ( Do import )import { formatDate, formatString, random } from "./src/ modularization 2/index"; console.log(formatDate(new Date())); console.log(formatString(10)); console.log(random());Print result display

边栏推荐
- iTOP-2K1000開發板啟動ramdisk-制作啟動U盤
- Creating a digital elevation model (DEM) using point cloud data
- C reflection
- Coredns part 2- compiling and installing external plugins
- fatal: repository not found
- C event
- Easyexcel export, self encapsulating header and sequence
- 全志平台BSP裁剪(2)附件一 General setup配置说明
- unity平台相关宏
- Ctf: find the most collinear points in the point set
猜你喜欢

Coredns part 3-access Prometheus monitoring

全志平台BSP裁剪(3)附件二 Kernel hacking配置说明

Wireshark illustrates TCP three handshakes and four waves

邂逅 NodeJS

One side of a small company in Hangzhou

DNS principles 01 introduction to DNS principles

MT2712 Boot Flow Introduction

Comparison between the most cost-effective processor and domestic processor i.mx6ul/a40i/t3

CountDownLatch

Postman 安装
随机推荐
Unity location service GPS API
1433: [example 1] angry cattle
Parallels Desktop 安装 Windows10 提示“安全启动功能防止操作系统启动”解决方法
Itop-2k1000 development board startup ramdisk production startup USB flash disk
ORACLE锁表解决办法
Creating a digital elevation model (DEM) using point cloud data
C event
Coredns part 4-compiling and installing unbound
Jdbc-dbutils
Shopify 主题样式开发
Google colab tips
About the problem of code wrapping when formatting code with vscode
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) K - Send the Fool Further! (medi
C iterator
Abstract classes and interfaces
C # characteristic
VK Cup 2017 - Round 1 C. Bear and Tree Jumps 换根dp
MT2712 SOC DMIPS
fatal: repository not found
Testing and threading