2017-01-30 25 views
1

npmの起動時に問題が発生する角2の発行エラーTS1068:予期しないトークン。コンストラクタ、メソッド、アクセサまたはプロパティが必要です。 app/chat.component.ts

app/chat.component.ts(20,9):エラーTS1068:予期しないトークン。コンストラクタ、メソッド、アクセサまたはプロパティが必要です。 app/chat.component.ts(47,1):エラーTS1128:宣言または文が必要です。

import {Component} from '@angular/core'; 
import {messageData} from './message'; 
import * as io from 'socket.io-client'; 

@Component({ 
    selector: 'chat-message', 
    templateUrl: './app/middlepanel/chat.component.html', 
}) 

export class chatComponent { 
    socket: any; 


    this.socket = io('http://localhost:8000'); 
    console.log(this.socket); 

    this.socket.emit('welcome', " demo msg"); 

    this.socket.on('sample', function(msg) { 
     console.log(msg); 

    }); 

    this.socket.emit('joinServer', { 
     userName: "punit", 
     orgId: "786", 
     deviceToken: "11111", 
     osType: "window" 
    }); 

    this.socket.on('update-people', function(msg) { 
     //console.log(JSON.stringify(msg)); 
     console.log(msg); 
    }); 
} 

答えて

0

あなたは、コンストラクタやライフサイクルフックに割り当てを行う必要があります。

... 
export class chatComponent { 
    socket: any; 

    constructor(){ 
    this.socket = io('http://localhost:8000'); 
    console.log(this.socket); 

    this.socket.emit('welcome', " demo msg"); 

    this.socket.on('sample', function(msg) { 
     console.log(msg); 
    }); 

    this.socket.emit('joinServer', { 
     userName: "punit", 
     orgId: "786", 
     deviceToken: "11111", 
     osType: "window" 
    }); 

    this.socket.on('update-people', function(msg) { 
     //console.log(JSON.stringify(msg)); 
     console.log(msg); 
    }); 
    }  
} 
+0

iはsocket.ioチャットアプリで角度2.0を実装していますあなたのフィードバックのためにどうもありがとうございます.I必要性をあなたの提案は、私はこれらの2つのモジュールをインポートする必要がありますいくつかのデモアプリケーション私は彼らがソケットクライアントのためにこのモジュールを使用していたと見ている import {Subject}から 'rxjs/Subject'; 'rxjs/Observable'から{Observable}をインポートします。 – punit

+0

@punit ehmここからそのような質問に答えるのは私にとっては少し不明です。ユースケースなどを確認する必要があります。ユースケースとコードで別の質問をする必要があります。 – echonax

+0

私は 'joinServer' emit関数をコンストラクタの外に出すときにエラーを返す "予期しないトークン、コンストラクタ、メソッド、アクセサ、またはプロパティが予期されていた" "すべてのソケットを" emit "と" on "いくつかのコンストラクタ関数では、私はイベントを呼び出す必要がありますので、ボタンをクリックすると、テキストフィールドのメッセージがサーバーに送信されます.soどのようなアプローチを使用する必要があります。私はObservableが助けてくれることを頼むのです。 – punit

関連する問題