我正在 VueJs、Typescript 和 WebPack 中开发一个 Web 应用程序,我对如何管理/拆分功能组(实用程序和服务)感到有些困惑。
我在 GitHub 的各种项目中看到一些函数是直接从文件 ex 中声明和导出的:
实用程序.ts
export function Sum(a:number, b:number):number{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
这可以与导入一起使用:
import {Sum} from "./utilities.ts"
let result = Sum(5,6);
Run Code Online (Sandbox Code Playgroud)
另一个常见的解决方案是声明一个 const 类:
otherUtilities.ts
export const OtherUtilities = {
Sum(a:number, b:number) : number => {
return a+b;
},
Hello() : string => {
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
并导入为:
import {OtherUtilities} from "./otherUtilities.ts"
let result = OtherUtilities.Sum(5,6);
Run Code Online (Sandbox Code Playgroud)
有什么区别?
过去,JS 存在名称冲突问题,但现在通过 Loaders 的导出/导入技术,这个问题应该已经过时了,对吧?
谢谢