아직 Ionic 4를 처음 사용합니다. 푸시 알림을받는 앱을 만들고 있습니다. 앱 내부의 탐색은 다음과 같이 작동합니다.
Home page -> Detail page
사용자가 알림을 탭할 때마다 앱이 열리고 세부 정보 페이지로 이동합니다. 탐색은 작동하지만 탐색 기록이 비어 있으므로 사용자가 하드웨어 뒤로 버튼을 탭하면 앱이 종료됩니다. 대신 사용자를 홈 페이지로 리디렉션하고 싶습니다.
Ionic 4에서이를 어떻게 달성합니까? 페이지를 탐색 기록으로 푸시하는 방법이 있습니까? 설명서를 읽었지만 이것에 대해 아무것도 찾을 수 없습니다. 가장 가까운 것은 아마도 NavCtrl.push()
Ionic 4에서 더 이상 사용할 수 없습니다.
감사합니다.
이 작업을 수행하는 더 쉬운 방법이있을 수 있지만 다음 접근 방식은 사용자가 푸시 알림 또는 딥 링크가 열린 후 표시된 페이지에서 돌아가고 싶을 때 사용자 지정 논리를 실행할 수 있기 때문에 매우 유연한 방법입니다. .
이 StackBlitz 데모를 살펴보십시오 .
데모에서는 파일 DetailsPage
의 다음 코드로 인해 앱이로드되는 즉시로 리디렉션 app-routing.module
됩니다.
{
path: "",
redirectTo: "/details/1?fromDeepLink=true", // <-- here!
pathMatch: "full"
}
어쨌든 중요한 부분은 DetailsPage
. 여기에서 사용자가 a) 헤더의 뒤로 버튼 및 b) Android 기기의 물리적 뒤로 버튼을 사용하여 뒤로 돌아 가려고 할 때 발생하는 작업을 처리해야합니다.
코드는 매우 자명하지만 기본적으로 해당 페이지에서 fromDeepLink
쿼리 문자열 매개 변수를 찾고 있습니다. 그렇다면 true
앱은 헤더의 뒤로 버튼과 Android의 물리적 뒤로 버튼 모두에 대한 사용자 지정 작업을 등록합니다. 장치.
사용자 정의 조치는을 HomePage
루트 페이지로 설정하지만 animationDirection
매개 변수를로 설정합니다 'back'
. 이렇게하면 실제로 탐색 스택에 추가하는 경우에도 사용자가 해당 페이지로 돌아가는 것처럼 보입니다.
다른 페이지에서 뒤로 버튼의 기본 동작에 영향을주지 않도록 사용자가 페이지를 떠나는 즉시이 사용자 지정 처리기가 제거된다는 점에 유의하는 것이 중요합니다.
import { Component, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { IonBackButtonDelegate, NavController, Platform, ViewDidEnter, ViewWillLeave } from "@ionic/angular";
import { Subscription } from "rxjs";
@Component({
selector: "app-details",
templateUrl: "./details.page.html",
styleUrls: ["./details.page.scss"]
})
export class DetailsPage implements OnInit, ViewDidEnter, ViewWillLeave {
@ViewChild(IonBackButtonDelegate, { static: false })
public backButton: IonBackButtonDelegate;
public itemId: number;
public fromDeepLink: boolean = false;
private unregisterBackButtonAction: Subscription;
constructor(
private platform: Platform,
private route: ActivatedRoute,
private navCtrl: NavController,
) {}
ngOnInit() {
const itemIdParam = this.route.snapshot.paramMap.get("itemId");
const fromDeepLinkParam = this.route.snapshot.queryParamMap.get('fromDeepLink');
this.itemId = +itemIdParam;
this.fromDeepLink = fromDeepLinkParam
? fromDeepLinkParam.toLocaleLowerCase() === 'true'
: false;
}
ionViewDidEnter() {
if(this.fromDeepLink) {
this.initializeCustomBackButtonAction()
}
}
ionViewWillLeave() {
if(this.fromDeepLink) {
this.removeCustomBackButtonAction();
}
}
private initializeCustomBackButtonAction(): void {
const leavingCallback = () => {
console.log('Using custom back button action');
this.navCtrl.navigateRoot('/home', { animationDirection: 'back' });
};
// Override the back button from the header
if (this.backButton) {
this.backButton.onClick = leavingCallback;
}
// Override the physical back button from Android devices
this.unregisterBackButtonAction = this.platform.backButton.subscribeWithPriority(10, leavingCallback);
}
private removeCustomBackButtonAction(): void {
this.unregisterBackButtonAction?.unsubscribe();
}
}
ion-back-button
탐색 스택에서 현재 페이지 앞에 페이지가 없으면 기본적 defaultHref
으로이 표시되지 않으므로 데모에서 다음 과 같이 속성을 설정합니다 .
<ion-back-button defaultHref></ion-back-button>
구성 요소가 실제로이 뒤로 버튼이 내 사용자 정의 뒤로 버튼 동작으로 수행하는 작업을 재정의 할 것이기 때문에 비워 둡니다. 그러나 defaultHref
템플릿에 추가해야합니다. 그렇지 않으면 뒤로 버튼이 표시되지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다