类未注册



我是TypeScript新手。我得到错误时,试图实例化类。下面是我的示例代码,实际代码不同不能分享。

 module ABC {
    export class A {    
       public execute<T>(action: string, data?: any, callerContext?: any): IAsyncResult<T> {
        // CODE::   
        var requestMessage = new Common.ClientClasses.ClientRequestMessage(); **// **ERROR- "WinRTError: Class not registered"****
        requestMessage.requestUri = actionRequest;
        requestMessage.method = "POST";
        requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
        Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
         .done((result: Common.ClientClasses.ClientResponeMessage) => {
             // CODE:
        }
        // Code::
      }
   }
}
declare module Common.ClientClasses {
    class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }
    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }
    class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any;
    }
}

我做了一些改进,应该可以工作:

module ABC {
    export class A {
        public execute<T>(action: string, data?: any, callerContext?: any) {
            var requestMessage = new Common.ClientClasses.ClientRequestMessage();
            requestMessage.method = "POST";
            requestMessage.body = data ? JSON.stringify(data, null, 2) : null;
            Common.ClientClasses.ClientRequest.executeAsync(requestMessage)
        }
     }
}
module Common.ClientClasses {
    export class ClientRequestMessage {
        public requestUri: string;
        public method: string;
        public body: string;
    }
    class ClientResponeMessage {
        public status: number;
        public statusText: string;
        public responseText: string;
    }
    export class ClientRequest {
        static executeAsync(clientRequestMessage: ClientRequestMessage): any {
            console.log("test");
        }
    }
}

可以这样运行:

var a = new ABC.A();
a.execute("some string");

declare module创建了一个用于智能感知的定义文件,但它不提供任何实现,这就是为什么我改变了你的代码,所以这个片段可以工作。

另外,如果你想使用模块中的任何类,你必须将它们export,以便它们可以从该模块外部可见。

相关内容

  • 没有找到相关文章

最新更新