我无法找到问题的解决方案。我有类设备然后我有DeviceService进行通信。最后,在页面场景我需要创建一个Device数组,但以错误结束。我几乎尝试了一切。
错误如:无法读取属性' undefined'未定义的; 无法读取属性' id'未定义的 谢谢你的帮助: - )
Device.ts
export class Device {
id: number;
name: string;
number: string;
constructor() { }
get deviceId(): number {return this.id; }
get deviceNumber(): string {return this.number; }
get deviceName(): string {return this.name; }
set deviceNumber(number: string) {this.number = number; }
set deviceName(name: string) {this.name = name;}
}
然后我制作了我已添加到提供商的DeviceService 的 DeviceService.ts
import {Injectable} from '@angular/core';
import {Device} from '../providers/device';
@Injectable()
export class DeviceService {
deviceCount: number;
devices: Array<Device>;
constructor(){}
getDevices(): Array<Device> {
return this.devices;
}
pushDevice(name, number) {
number.toString();
this.devices[this.deviceCount].id++;
this.devices[this.deviceCount].deviceName = name;
this.devices[this.deviceCount].deviceNumber = number;
this.deviceCount++;
}
}
Scene.ts :我需要创建设备数组
import {Component} from '@angular/core';
import {NavController, NavParams, PopoverController, ModalController} from 'ionic-angular';
import {CreateDevicePage} from '../create-device/create-device';
import {PopoverPage} from '../../providers/popover';
import {DeviceService} from '../../providers/device-service';
@Component({
selector: 'page-scenes',
templateUrl: 'scenes.html',
})
export class ScenesPage {
constructor(public navCtrl: NavController,
public navParams: NavParams,
public modalCtrl: ModalController,
public popoverCtrl: PopoverController,
public deviceCtrl: DeviceService,
) { }
ionViewDidLoad() { }
launchSecondPage() {
let modal = this.modalCtrl.create(CreateDevicePage);
modal.onDidDismiss((data) => {
/*Problem here*/
this.deviceCtrl.pushDevice(data.name, data.number);
});
modal.present();
}
}
答案 0 :(得分:0)
pushDevice(name, number) {
number.toString();
this.devices[this.deviceCount] = new Device(); // <= add this
this.devices[this.deviceCount].id++;
this.devices[this.deviceCount].deviceName = name;
this.devices[this.deviceCount].deviceNumber = number;
this.deviceCount++;
}
也代替devices: Array<Device>;
使用devices: Device[] = [];
答案 1 :(得分:0)
你应该试试这个:
export class Device {
static ID: number = 0;
private id: number;
private name: string;
private number: string;
constructor(name: string, number: string) {
this.id = Device.ID++;
this.name = name;
this.number = number;
}
public get deviceId() {
return this.id;
}
public set deviceName(value: string) {
this.name = value;
}
public get deviceName(): string {
return this.name;
}
public set deviceNumber(value: string) {
this.number = value;
}
public get deviceNumber() {
return this.number;
}
}
和DeviceService.ts
import {Injectable} from '@angular/core';
import {Device} from '../providers/device';
@Injectable()
export class DeviceService {
private _devices: Device[] = [];
public get devices() {
return this._devices.slice();
}
public get deviceCount() {
return this.devices.length;
}
constructor(){}
public pushDevice(name: string, number: string) {
this._devices = this._devices.concat(new Device(name, number));
}
}
如果你在你的设置者和你的Device类的getter中没有做任何特别的事情,那就去除它们并使用公共属性:
export class Device {
static ID: number = 0;
private id: number;
public name: string;
public number: string;
constructor(name: string, number: string) {
this.id = Device.ID++;
this.name = name;
this.number = number;
}
public get deviceId() {
return this.id;
}
}
答案 2 :(得分:0)
id: number = 0;
name: string;
number: string;
constructor(_id:number,_name:string,_number:string) {
this.id=_id;
this.name = _name;
this.number = _number;
}
在 DeviceService 中
devices: Array<Device> = [];
idCount: number = 0;
pushDevice(name: string, number: string) {
let device = new Device(this.idCount, name, number);
this.devices.push(device);
this.idCount++;
}