5

プッシュ アクションでプロパティの値を更新したいのですが、関数からプロパティにアクセスする方法がわかりません。

export class Datas {
    prop1 = "my val";
}

var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
     // prop1 of Datas = e.data;
};

何か案は ?

編集:ページが読み込まれた後、プッシュメッセージを受信したときにデータを更新したい.

編集 2: コードのテスト

data.js :

export class Data {
    static information = '';
}

viewModel.js

import {bindable} from 'aurelia-framework';
import { Data } from 'data';

export class ViewModel {
    constructor() { 
        this.informaton = Data.information;
    }

    logState(){
        console.log("state of Data.information : " + Data.information);
        console.log("state of this.information : " + this.information);
    }
}

var connection = new WebSocket('ws://localhost:8787', 'json');
connection.onmessage = function (e) {
    Data.information = e.data;
    console.log("receiving a message : Data.information = " + Data.information);
};

ビューモデル.html:

<template>
   <span id="spanAff">${information}</span>
   <br/>
   <input type="button" value="log" click.trigger="logState()" />
</template>

ログ:

「メッセージを受信中 : Data.information = 4」
「Data.information の状態 : 4」
「this.information の状態 : undefined」

4

1 に答える 1

7

別のフォーラムでこのソリューションを入手しました:

export class ViewModel {
  constructor() { 
    this.information = 'my val';
    var connection = new WebSocket('ws://localhost:8787', 'json');
    connection.onmessage = e => {
        this.information = e.data;
    };
  }
}
于 2015-05-11T15:31:32.420 に答える