ionic-framework - 在 Ionic4 中,如何在将调用放入编辑视图并导航回详细信息页面后刷新详细信息视图以获取更新的记录
问题描述
我正在用 Ionic4 制作一个简单的杂物,一切正常。当我通过 put 调用更新我的记录并导航回记录的详细信息以查看更新的值时,它会显示旧值。我的更新和导航代码是:
async updateClient() {
await this.api.updateClient(this.route.snapshot.paramMap.get('id'), this.clientForm.value)
.subscribe(res => {
let id = res['_id'];
this.router.navigate(['/client/detail', id]);
}, (err) => {
console.log(err);
});
}
详细页面代码是:
import { Component, OnInit } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { RestApiService } from '../../rest-api.service';
import { ActivatedRoute, Router } from '@angular/router';
import {Location} from '@angular/common';
@Component({
selector: 'app-detail',
templateUrl: './detail.page.html',
styleUrls: ['./detail.page.scss'],
})
export class DetailPage implements OnInit {
client: any = {};
constructor(public api: RestApiService,
public loadingController: LoadingController,
public route: ActivatedRoute,
public router: Router,
private location: Location) {}
async getClient() {
console.log('in getClient');
const loading = await this.loadingController.create({
});
await loading.present();
await this.api.getClientById(this.route.snapshot.paramMap.get('id'))
.subscribe(res => {
console.log(res);
this.client = res;
loading.dismiss();
}, err => {
console.log(err);
loading.dismiss();
});
}
ngOnInit() {
this.getClient();
}
解决方案
尝试this.getClient()
在方法中调用方法,ionViewWillEnter()
如下所示:
ionViewWillEnter(){
this.getClient();
}
每次进入页面时它都会调用你的this.getClient()
方法,无论它是否加载。
推荐阅读
- html - 使用 Google 幻灯片和 iframe 向 Google 协作平台添加动画
- excel - 如何根据上面的单元格突出显示excel中的单元格?
- excel - 从另一个工作表中动态获取使用复杂公式的值
- excel - 如何获取一个月的第一个实例并添加新行(包括屏幕截图)
- python - 如何在 python 中更改折线图上的 x 轴?
- c++ - 缺少 .qmake.stash 文件
- android - AdMob 匹配率仍然很低,但政策警告已在 1 个多月前被移除
- node.js - 在多条路线中使用猫鼬和快递
- azure-active-directory - 单点登录作为替代 - Azure AD B2C
- python - jupyter notebook 无法导入 gdal 而普通终端 python 可以