В Ionic4 как обновить подробный просмотр, чтобы получить обновленную запись после ввода вызова в режиме редактирования и перехода обратно на страницу сведений

#ionic-framework #angular-ui-router #ionic4

#ionic-framework #angular-ui-router #ionic4

Вопрос:

Я делаю простой crud с Ionic4, все работает нормально. Когда я обновляю свою запись с помощью put call и возвращаюсь к деталям записи, чтобы увидеть обновленные значения, она показывает старые значения. Мой код обновления и навигации:

 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();
  }
  

Ответ №1:

Попробуйте вызвать this.getClient() метод в ionViewWillEnter() методе, как показано ниже :

 ionViewWillEnter(){
    this.getClient();
}
  

Он будет вызывать ваш this.getClient() метод каждый раз, когда вы заходите на страницу, независимо от того, загружен он или нет.

Комментарии:

1. Отлично! Отлично, спасибо