簡體   English   中英

如何在我的視圖中顯示Angular2異常

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

這是我用Angular2編寫的Javascript代碼。 這個問題很重要,但更重要的是:我如何在HTML頁面上打印此錯誤,因為此頁面將顯示在移動Web視圖中,以便另一側的開發人員可以獲取此錯誤。 即使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 ;
}

我的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>

我當前的輸出如下:

您可以看到一條鍵值顯示在消息中,並在控制台中顯示所有值

我究竟做錯了什么?

如果我沒錯,您想向其他開發人員展示異常,如果可以,可以使用此功能

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>

請注意,我將消息類型更改為任何要在其中包含對象的類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM