收藏
回答

TypeScript开发小程序逻辑部分,自定义命名空间,在页面ts中调用无法找到命名空间,怎么解决?

使用TypeScript开发小程序逻辑部分,使用了自定义命名空间,但是在app.ts中初始化时,总是找不到自定义的命名空间。微信小程序有没有使用TypeSript开发开发的面向对象开发demo?可以参考下目录结构和调用方式

itech.d.ts

/// <reference path="./device/device.d.ts" />

/// <reference path="./device/device.api.d.ts" />


declare namespace Itech {

interface DeviceManager {

bindNewDevice(bindInfo: BindDeviceInfo): RemoteDevice | null;

saveDeviceCacche(device: RemoteDevice): boolean;

unbind(device: RemoteDevice): void;

}

interface getDeviceManager {

() : DeviceManager;

}

}

DeviceManager.ts文件

namespace Itech {

export class DeviceManager {

bindDevices: Map<string, RemoteDevice>;


static INSTANCE: DeviceManager;


static getInstance() : DeviceManager {

if (DeviceManager.INSTANCE === undefined) {

DeviceManager.INSTANCE = new DeviceManager();

}

return DeviceManager.INSTANCE;

}


constructor() {

this.bindDevices = new Map<string, RemoteDevice>();

}

....

}

我要在app.ts中创建一个DeviceManager对象,调用其中的接口,要怎么初始化?

使用如下调用

App<IAppOption>({

globalData: {},

onLaunch() {

let deviceManager = Itech.DeviceManager.getInstance();

console.log("-----deviceManager: " + deviceManager);

...

}

出错如下:

thirdScriptError

Itech is not defined;at App lifeCycleMethod onLaunch function

ReferenceError: Itech is not defined

    at he.onLaunch (http://127.0.0.1:51160/appservice/app.js:8:29)

    at he.<anonymous> (http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1045736)

    at http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1046174

    at new he (http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1046250)

    at Function.<anonymous> (http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1046581)

    at http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1034791

    at http://127.0.0.1:51160/appservice/app.js:3:1

    at require (http://127.0.0.1:51160/appservice/__dev__/WAService.js:1:1077084)

    at <anonymous>:1:1

    at HTMLScriptElement.scriptLoaded (http://127.0.0.1:51160/appservice/appservice?t=1588058560389:3659:21)




回答关注问题邀请回答
收藏

3 个回答

登录 后发表内容
问题标签