Javascript Typescript TypeError:无法读取属性';设置';未定义的

Javascript Typescript TypeError:无法读取属性';设置';未定义的,javascript,typescript,firebase,push-notification,nativescript,Javascript,Typescript,Firebase,Push Notification,Nativescript,我正在尝试在android上建立一个非常基本的推送通知示例。我使用nativescript+typescript(即使代码有点凌乱,因为我不知道如何在typescript中正确重写“var Observable=require(“data/Observable”);”和“viewModel”。代码基于此示例 从“@angular/core”导入{Component}; 从“nativescript推送通知”导入*作为推送插件; var可观测=要求(“数据/可观测”); @组成部分({ 选择器:“

我正在尝试在android上建立一个非常基本的推送通知示例。我使用nativescript+typescript(即使代码有点凌乱,因为我不知道如何在typescript中正确重写“var Observable=require(“data/Observable”);”和“viewModel”。代码基于此示例

从“@angular/core”导入{Component};
从“nativescript推送通知”导入*作为推送插件;
var可观测=要求(“数据/可观测”);
@组成部分({
选择器:“我的应用程序”,
模板:`
`
})
导出类AppComponent{
//你的打字脚本逻辑在这里
viewModel=新的可观察的。可观察的({
注册ID:“
});
页面已加载(args){
var page=args.object;
page.bindingContext=this.viewModel;
}
registerTap(args){
变量设置={
//安卓设置
senderID:'0434Blabla',//Android:发件人/项目编号的必需设置
notificationCallbackAndroid:function(message){//Android:Callback在接收到新推送时调用。
log(JSON.stringify(message));
警报(JSON.stringify(message));
},
//iOS设置
badge:true,//通过推送通知启用设置badge
声音:true,//启用播放声音
警报:true,//启用创建警报
//在iOS上接收推送时调用的回调
notificationCallbackIOS:函数(消息){
警报(JSON.stringify(message));
}
};
pushPlugin.register(设置、,
//成功回调
功能(令牌){
警报(“测试”);
//如果我们在安卓设备上,我们可以订阅onMessageReceived函数
//用于推送通知
if(pushPlugin.onMessageReceived){
pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
}
警报('成功注册的设备:'+令牌);
this.viewModel.set(“regId”,token);
},
//错误回调
函数(错误){
警报(“错误”);
console.log(错误);
警报(错误);
}
);
}
}
单击“注册”按钮时,出现以下错误:

TypeError:“无法读取未定义的属性“set” 我不熟悉打字脚本和通知处理,你能给我一个提示吗


提前感谢

这一点。viewModel
包含
注册ID
但u set
regId
更改其中一个,它应该可以工作

不幸的是,它不能工作。我认为问题与viewModel对象有关,我认为它是空的或未定义的。我不知道如何使用typescript synt正确实例化它ax.您在函数中引用
this
,函数从调用方接收
this
上下文。如果您使用的是Typescript,则可以使用胖箭头表达式。因此,使用
标记=>{…}
,而不是
函数(标记){…}
。通过这种方式,
上下文引用了正确的位置。谢谢!它解决了我的问题,但我不明白为什么。Typescript在“转换”到javascript时发挥了一些神奇的作用。它在后台创建了一个名为
\u This
的变量,它引用了您的实例以及您处理
的任何地方,它都是变化d到
\这是
。看看生成的javascript就知道了。啊,我明白了!谢谢!
import { Component } from "@angular/core";
import * as pushPlugin from "nativescript-push-notifications";
var Observable = require("data/observable");


@Component({
selector: "my-app",
template: `
    <ActionBar title="My App" icon="" class="action-bar">
    </ActionBar>     
<StackLayout>
<Label text="Tap the button to trigger the register function." textWrap="true" class=""></Label>
<Button text="REGISTER" (tap)="registerTap()" ></Button>    
<label text="Your device id/token:" textWrap="true" ></label>
<TextView text="{{ registrationId }}" class="title" textWrap="true"></TextView>
<Label text="{{ message }}" class="message" textWrap="true" ></Label>
</StackLayout>  `
})

export class AppComponent {
// Your TypeScript logic goes here

viewModel = new Observable.Observable({
registrationId: ""
});

pageLoaded(args) {
var page = args.object;
page.bindingContext = this.viewModel;
}


registerTap (args) {

var settings = {
    // Android settings 
    senderID: '0434blablabla', // Android: Required setting with the sender/project number 
    notificationCallbackAndroid: function(message) { // Android: Callback to invoke when a new push is received. 
        console.log(JSON.stringify(message));
        alert(JSON.stringify(message));            
    },

    // iOS settings 
    badge: true, // Enable setting badge through Push Notification 
    sound: true, // Enable playing a sound 
    alert: true, // Enable creating a alert 

    // Callback to invoke, when a push is received on iOS 
    notificationCallbackIOS: function(message) {
        alert(JSON.stringify(message));
    }
};




pushPlugin.register(settings,
    // Success callback 
    function(token) {
      alert("test");
          // if we're on android device we have the onMessageReceived function to subscribe 
        // for push notifications 
        if(pushPlugin.onMessageReceived) {
            pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
        }

        alert('Device registered successfully : ' + token);
        this.viewModel.set("regId", token);
    },
    // Error Callback 
    function(error){
      alert("errore");
        console.log(error);
        alert(error);
    }
);
}



}