简体   繁体   English

如何在我的视图中显示Angular2异常

[英]How can I display Angular2 exceptions in my view

Here my Javascript code that is written in Angular2. 这是我用Angular2编写的Javascript代码。 The issue is important but more important is: how can I print this error on Html page because this page is going to be displayed in a mobile web-view, so that developers from the other side can get the error. 这个问题很重要,但更重要的是:我如何在HTML页面上打印此错误,因为此页面将显示在移动Web视图中,以便另一侧的开发人员可以获取此错误。 Even if JSON.stringify() is not work. 即使JSON.stringify()不起作用。

let webSocketURL =  "ws://localhost:7000";
message = '';
webSocket:any;
try {
    this.webSocket = new WebSocket(webSocketURL);

    this.webSocket.onopen = (openEvent)=>{
        this.message = "WebSocket OPEN";
        this.webSocket.send('hello from client');
    };
    this.webSocket.onclose =  (closeEvent) =>{
        console.log('closeEvent',closeEvent);
        this.message = "WebSocket CLOSE"+ JSON.stringify(closeEvent);
        alert("WebSocket CLOSE: " + JSON.stringify(closeEvent, null, 4));
    };
} catch (exception) {
    console.error(exception);
    this.message = " Got exception" + exception ;
}

My HTML code is as below 我的HTML代码如下

<p>
  This is stage 11 for socket demo and we are going to listion socket on ws://localhost:7000
</p>

<h3>Here is Message output ::{{message}} </h3>

My current output is as below: 我当前的输出如下:

您可以看到一条键值显示在消息中,并在控制台中显示所有值

What am I doing wrong? 我究竟做错了什么?

If I get you correctly, you want to show the exception to other devs, if yes you can use this 如果我没错,您想向其他开发人员展示异常,如果可以,可以使用此功能

let webSocketURL =  "ws://localhost:7000";
message : any;
webSocket:any;
try {
    this.message = null;

    this.webSocket = new WebSocket(webSocketURL);

    this.webSocket.onopen = (openEvent)=>{
       this.message = "WebSocket OPEN";
       this.webSocket.send('hello from client');
    };
    this.webSocket.onclose =  (closeEvent) =>{
        console.log('closeEvent',closeEvent);
        this.message = "WebSocket CLOSE"+ JSON.stringify(closeEvent);
        alert("WebSocket CLOSE: " + JSON.stringify(closeEvent, null, 4));
    };
} catch (exception) {
    console.error(exception);
    this.message = exception ;
}


<p>
  This is stage 11 for socket demo and we are going to listion socket on ws://localhost:7000
</p>

<h3 *ngIf="message">Here is Message output ::{{message | json}} </h3>

Pay attention I changed message type to any to have an object in there 请注意,我将消息类型更改为任何要在其中包含对象的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM