简体   繁体   English

为什么在 ionic / angular 的 ngInit 中未定义在构造函数中初始化的成员变量?

[英]Why would a member variable that is initialized in the constructor be undefined in ngInit in ionic / angular?

I have a modal consisting of a form that is bound to an instance of "Foo."我有一个由绑定到“Foo”实例的表单组成的模式。 During construction of the modal, foo is set correctly.在模态的构造过程中, foo 设置正确。 I double-checked both using breakpoints and console.log .我使用断点和console.log仔细检查了。 However, at any time after the constructor, this.foo is undefined .但是,在构造函数之后的任何时候, this.foo都是undefined

-- --

EDIT: I have found a solution, but my question still stands.编辑:我找到了解决方案,但我的问题仍然存在。 My solution was to move my assignment of foo to ngOnInit() instead of the constructor.我的解决方案是将我对foo的分配移动到ngOnInit()而不是构造函数。

-- --

Template:模板:

<ion-header>
    <ion-toolbar>
      <ion-title>New Foo</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content>
    <ion-list>
      <ion-item>
        <ion-input placeholder="Name" [(ngModel)]="foo.name"></ion-input>
      </ion-item>
      <!-- etc... -->
    </ion-list>

    <ion-button expand="block" color="primary" (click)="create()">
      Create
    </ion-button>
</ion-content>

Component:零件:

export class FooModalComponent implements OnInit {

  foo: Foo = new Foo();

  constructor(
    private modalController: ModalController,
    private navParams: NavParams
  ) { 
    const inputFoo = this.navParams.get("foo");

    if (inputFoo) {
      // this shouldn't matter, as foo would have to be truthy (not undefined)
      this.foo = inputFoo;
    }

    // logs this.foo as expected
    console.log(this.foo);
  }

  ngOnInit() {
    // logs: undefined
    console.log(this.foo);
  }

  close() {
    this.modalController.dismiss();
  }

  create() {
    this.modalController.dismiss(this.foo);
  }
}

Model: Model:

export class Foo {
    name: string;
}

In newer Ionic versions (probably 4+), data passed to modals as componentProps / NavParams get automatically assigned to corresponding class variables in the component to be presented.在较新的 Ionic 版本(可能是 4+)中,作为componentProps / NavParams传递给模态的数据会自动分配给要呈现的组件中相应的 class 变量。 If you pass an undefined variable, the corresponding field will effectively get deleted.如果您传递未定义的变量,则相应的字段将有效地被删除。 This happens after object construction, but before ngOnInit is called, that's why your workaround did the trick.这发生在 object 构造之后,但在调用ngOnInit之前,这就是您的解决方法成功的原因。

Ionic 5 documentation mentions @Input annotations for this, but the same seems to happen without as well, though I can't find any confirmation for that claim. Ionic 5 文档为此提到@Input注释,但同样的情况似乎也发生了,尽管我找不到该声明的任何确认。

What you probably did:你可能做了什么:

this.modalController.create({
  component: FooModalComponent,
  componentProps: { foo: foo } // foo might be undefined
})

To avoid passing an undefined foo, you could do something like:为避免传递未定义的 foo,您可以执行以下操作:

this.modalController.create({
  component: FooModalComponent,
  componentProps: foo ? { foo } : {}
})

Or, as you did, re-assign variables in ngOnInit as needed.或者,正如您所做的那样,根据需要在ngOnInit中重新分配变量。 On a side note, you might not need NavParams anymore in the constructor, since passed params are already assigned at this point.附带说明一下,您可能不再需要在构造函数中使用NavParams ,因为此时已经分配了传递的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM