我将Ionic 3中的params传递给组件。我知道你在收到的页面的构造函数中提取了参数,但我在ngOnInit页面中访问它们时遇到了麻烦。
所以我的构造函数看起来像这样:
constructor(public navCtrl: NavController, public navParams: NavParams, private contactsProvider: ContactsProvider) {
let contactId = this.navParams.get('contactId');
let ownerId = this.navParams.get('ownerId');
// this.contactsProvider.getContact(ownerId, contactId)
// .subscribe(response =>{
// this.contact = response.json();
// console.log('Response: ', this.contact);
// })
}
我的Init看起来像这样:
ngOnInit(){
this.contactsProvider.getContact(ownerId, contactId)
.subscribe(response =>{
this.contact = response.json();
console.log('Response: ', this.contact);
})
}
我收到一个TS错误,说“< owner"'在页面上不存在。
我在这里做错了什么。
感谢您提出任何意见!!
答案 0 :(得分:0)
需要
this.contactId = this.navParams.get('contactId');
this.ownerId = this.navParams.get('ownerId');
否则该值只能在构造函数中使用。
还在这里
ngOnInit(){
this.contactsProvider.getContact(this.ownerId, this.contactId)
答案 1 :(得分:0)
构造函数中声明的变量为local variables
,仅可从constructor()
获得。您必须将它们声明为Component属性才能在组件中的任何位置使用它们,这样:
@Component({...})
private contactId: number;
private ownerId: number;
...
然后你可以在你的组件方法中使用它们,使用this
作为Günter告诉你:
constructor(public navCtrl: NavController, public navParams: NavParams, private contactsProvider: ContactsProvider) {
this.contactId = this.navParams.get('contactId');
this.ownerId = this.navParams.get('ownerId');
}
ngOnInit(){
this.contactsProvider.getContact(this.ownerId, this.contactId)
}