如何在AssemblyScript中将类作为命名空间的一部分重新导出?
在AssemblyScript中将类重新导出到命名空间的正确方法
你之前的写法无法编译,是因为export class Action = EntityAction;不是合法的AssemblyScript语法——不能用这种赋值方式把已有的类绑定到命名空间的成员上。
在AssemblyScript中可以实现你的需求,正确的做法是使用export import语法在命名空间内重新导出这些类:
import { Action as EntityAction, Asset as EntityAsset, Batch as EntityBatch, Batcher as EntityBatcher, Stream as EntityStream, Watcher as EntityWatcher, } from "./bindings/schema"; export namespace Entity { export import Action = EntityAction; export import Asset = EntityAsset; export import Batch = EntityBatch; export import Batcher = EntityBatcher; export import Stream = EntityStream; export import Watcher = EntityWatcher; }
这种写法会把每个导入的类直接挂载到Entity命名空间下,外部代码可以通过Entity.Action、Entity.Asset等方式访问这些类,同时完全保留类的原有功能(比如构造函数、实例方法等)。
你也可以简化导入流程,直接在命名空间内完成导入和导出:
export namespace Entity { export import Action = require("./bindings/schema").Action; export import Asset = require("./bindings/schema").Asset; export import Batch = require("./bindings/schema").Batch; export import Batcher = require("./bindings/schema").Batcher; export import Stream = require("./bindings/schema").Stream; export import Watcher = require("./bindings/schema").Watcher; }
两种写法效果一致,可根据代码风格偏好选择。
内容的提问来源于stack exchange,提问作者Paul Razvan Berg
相关产品推荐
相关产品推荐

