![](/img/trans.png)
[英]Typescript TypeError: Cannot read property 'collection' of undefined
[英]Typescript TypeError: Cannot read property 'set' of undefined
我正在尝试在android上建立一个非常基本的推送通知示例。 我使用nativescript + typescript(即使代码有点混乱,因为我不明白如何在typescript中正确重写“ var Observable = require(“ data / observable”);“和” viewModel“。该代码基于在此示例上https://bradmartin.net/2015/12/28/use-google-cloud-messaging-for-push-notifications-with-nativescript/
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);
}
);
}
}
当我单击注册按钮时,出现以下错误:
TypeError:“无法读取未定义的属性'set'”我是Typescript和通知处理的新手,可以给我提示吗?
提前致谢
this.viewModel
包含registrationId
但是您将regId
更改为其中之一,它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.