gpt4 book ai didi

angular - 如何在Ionic 4中刷新页面

转载 作者:行者123 更新时间:2023-12-03 15:15:00 25 4
gpt4 key购买 nike

我想在成功删除数据后刷新页面。当我删除或更新时,我必须先刷新页面,然后刷新我的数据。如何在Ionic 4中使用navController请帮助我...

book-list.page.html

<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Book List
</ion-title>
<ion-buttons slot="end">
<ion-button routerLink="/user-detail">
<ion-icon name="add-circle"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>

<ion-content padding>
<ion-card>


<ion-card-content>
<ion-card-subtitle>Books:</ion-card-subtitle>

<ion-list *ngFor="let book of books">
<ion-item>
<ion-label text-wrap>
<ion-text>
<p>Title: {{book.title}}</p>
</ion-text>
<ion-text>
<p>Description: {{book.description}}</p>
</ion-text>
</ion-label>
<ion-button [routerLink]="['/edit-book/',book.id]" (click)="editBook(book.id)">
<ion-icon name="create" slot="end"></ion-icon>
</ion-button>
<ion-button color="danger" (click)="delete(book.id)">
<ion-icon name="trash" slot="end"></ion-icon>
</ion-button>

</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
</ion-content>

book-list.page.ts
import { Component, OnInit } from '@angular/core';
import { Book } from '../book';
import { first } from 'rxjs/operators';
import { UserService } from '../user.service';
import { AlertController } from '@ionic/angular';
import { ToastController } from '@ionic/angular';
import { Router } from '@angular/router';
import { NavController, NavParams } from '@ionic/angular';
@Component({
selector: 'app-book-list',
templateUrl: './book-list.page.html',
styleUrls: ['./book-list.page.scss'],
})
export class BookListPage implements OnInit {
books: Book[] = [];
constructor(private router: Router,public navCtrl: NavController,public toastController: ToastController, private userService: UserService, public alertController: AlertController) { }

ngOnInit() {
this.getBook();
}

getBook() {
this.userService.getBook().pipe(first()).subscribe(books => {
this.books = books;


});
}

async delete(id: number) {

const alert = await this.alertController.create({
header: 'Confirm!',
message: 'Are you sure want to delete this book details?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
this.userService.delete(id).pipe(first()).subscribe(async books => {

books = books
const toast = await this.toastController.create({
message: 'Your Book details have been deleted.',
duration: 2000

});
toast.present();
this.router.navigate(['book-list']);
});
}

}
]
});

await alert.present();

}
editBook($id) {
this.userService.setCurrentId($id);

}

}

这是我的代码,请帮助我如何刷新ionic中的页面以及如何在此代码中使用NavController请帮助我

最佳答案

我通过将ngOnInit()中的函数调用移到ionViewWillEnter()中解决了这个问题。尝试将book-list-page.ts中的代码更改为:

    ngOnInit() {
}

ionViewWillEnter() {
this.getBook();
}

我不知道这是正确的方法还是最好的方法,但是对我来说,解决方案很好用。

关于angular - 如何在Ionic 4中刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com