【问题标题】:How to fire ngOnInit on page in ionic如何在离子页面上触发 ngOnInit
【发布时间】:2020-11-17 23:51:35
【问题描述】:

我正在使用 ionic 5 和 angular 9。并使用 angular Router 在页面之间移动。但这给我带来了一个问题,在离子中,ngOnInit 不再被调用。我知道 ionic 中有一个 NavController 以及它抛出的生命周期事件......但我需要知道是否有一种方法可以使用角度路由器并再次调用 ngOnInit。

一些代码来说明...

第 1 页:

goToPerson(person){
this.router.navigateByUrl("/personProfile", {state:person, replaceUrl:true});
}

第 2 页:

ngOnInit() {
this.person = this.router.getCurrentNavigation().extras.state as Person;

if(this.person.photo != null){
  this.photoPerfil = environment.img_url + this.person.photo.fileName;
}else{
  this.photoPerfil = "../../../assets/images/person.png";
}
}

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    你必须设置自己的Route Reuse Strategy并将shouldReuseRoute设置为false https://angular.io/api/router/RouteReuseStrategy

    【讨论】:

      【解决方案2】:

      我使用 ionViewWillEnter() 代替,因为 onInit 只会触发一次

      ngOnInit() 在 Angular 首次显示数据绑定属性并设置指令或组件的输入属性后初始化指令或组件。 在第一个 ngOnChanges() 之后调用一次。

      【讨论】:

        猜你喜欢
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-15
        • 1970-01-01
        • 2018-12-14
        相关资源
        最近更新 更多