Ionic 2:生成后退按钮点击事件
Ionic 2: Generated back button click event
我正在尝试记录用户点击导航栏中生成的后退按钮的操作,但找不到处理点击事件的方法?
这里的 ion-nav-back-button 好像不能用了?
如果您也想手动操作:
将此添加到您的 page.html
<ion-header>
<ion-toolbar>
<ion-buttons start>
<button (click)="goBack()" royal>
<ion-icon name="arrow-round-back"></ion-icon>
</button>
</ion-buttons>
<ion-title>Details page</ion-title>
</ion-toolbar>
</ion-header>
在您的 page.ts 文件中添加:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
templateUrl: 'build/pages/awesome/awesome.html',
})
export class AwesomePage {
constructor(private navCtrl: NavController) {
}
goBack(){
this.navCtrl.pop();
}
}
您需要先将 hideBackButton
添加到 ion-navbar
。它将删除默认的后退按钮。
然后添加您自己的按钮,您可以通过单击事件轻松管理它。
代码:
<ion-header>
<ion-navbar hideBackButton>
<ion-buttons left>
<button ion-button (click)="doYourStuff()">
<ion-icon class="customIcon" name="arrow-back"></ion-icon>
</button>
</ion-buttons>
<ion-title>Page Title</ion-title>
</ion-navbar>
</ion-header>
doYourStuff()
{
alert('cowabonga');
this.navCtrl.pop(); // remember to put this to add the back button behavior
}
最后一件事:Css.
该图标将比通常的后退按钮小。如果你想让它类似于 Ionic2 中使用的默认值,你需要增加它的大小。
.customIcon {
font-size: 1.7em;
}
根据the official Ionic docs,您可以覆盖NavBar组件的backButtonClick()
方法:
import { ViewChild } from '@angular/core';
import { Navbar } from 'ionic-angular';
@Component({
selector: 'my-page',
template: `
<ion-header>
<ion-navbar>
<ion-title>
MyPage
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
...
</ion-content>
`
})
export class MyPage {
@ViewChild(Navbar) navBar: Navbar;
constructor(private navController: NavController){}
ionViewDidLoad() {
this.navBar.backButtonClick = (e:UIEvent)=>{
// todo something
this.navController.pop();
}
}
}
要自定义默认后退按钮操作,您需要覆盖 NavBar 组件的 backButtonClick() 方法。
在您的 "customClass.ts" 中导入 Navbar 组件。创建 auxMethod 以覆盖默认行为并在您的 ionViewDidLoad 方法中调用。
import { Navbar } from 'ionic-angular';
export class myCustomClass {
@ViewChild(Navbar) navBar: Navbar;
...
ionViewDidLoad() {
this.setBackButtonAction()
}
//Method to override the default back button action
setBackButtonAction(){
this.navBar.backButtonClick = () => {
//Write here wherever you wanna do
this.navCtrl.pop()
}
}
}
此代码已在 ionic 3 中测试。
以防万一有人在看。 Ionic 3 提供 life-cycle 事件。 "ionViewDidLeave" 或 "ionViewWillLeave" 均可用于此类目的。
查看文档以查看更多事件。
https://ionicframework.com/docs/api/navigation/NavController/
以防有人在使用后仍有问题
@ViewChild(Navbar) navBar: Navbar;
尝试不要将this.navbar.backButtonClick
放在constructor()
中
或者,您可以将它放在 ionViewDidLoad()
它应该可以工作。
我正在尝试记录用户点击导航栏中生成的后退按钮的操作,但找不到处理点击事件的方法?
这里的 ion-nav-back-button 好像不能用了?
如果您也想手动操作:
将此添加到您的 page.html
<ion-header>
<ion-toolbar>
<ion-buttons start>
<button (click)="goBack()" royal>
<ion-icon name="arrow-round-back"></ion-icon>
</button>
</ion-buttons>
<ion-title>Details page</ion-title>
</ion-toolbar>
</ion-header>
在您的 page.ts 文件中添加:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
templateUrl: 'build/pages/awesome/awesome.html',
})
export class AwesomePage {
constructor(private navCtrl: NavController) {
}
goBack(){
this.navCtrl.pop();
}
}
您需要先将 hideBackButton
添加到 ion-navbar
。它将删除默认的后退按钮。
然后添加您自己的按钮,您可以通过单击事件轻松管理它。
代码:
<ion-header>
<ion-navbar hideBackButton>
<ion-buttons left>
<button ion-button (click)="doYourStuff()">
<ion-icon class="customIcon" name="arrow-back"></ion-icon>
</button>
</ion-buttons>
<ion-title>Page Title</ion-title>
</ion-navbar>
</ion-header>
doYourStuff()
{
alert('cowabonga');
this.navCtrl.pop(); // remember to put this to add the back button behavior
}
最后一件事:Css.
该图标将比通常的后退按钮小。如果你想让它类似于 Ionic2 中使用的默认值,你需要增加它的大小。
.customIcon {
font-size: 1.7em;
}
根据the official Ionic docs,您可以覆盖NavBar组件的backButtonClick()
方法:
import { ViewChild } from '@angular/core';
import { Navbar } from 'ionic-angular';
@Component({
selector: 'my-page',
template: `
<ion-header>
<ion-navbar>
<ion-title>
MyPage
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
...
</ion-content>
`
})
export class MyPage {
@ViewChild(Navbar) navBar: Navbar;
constructor(private navController: NavController){}
ionViewDidLoad() {
this.navBar.backButtonClick = (e:UIEvent)=>{
// todo something
this.navController.pop();
}
}
}
要自定义默认后退按钮操作,您需要覆盖 NavBar 组件的 backButtonClick() 方法。
在您的 "customClass.ts" 中导入 Navbar 组件。创建 auxMethod 以覆盖默认行为并在您的 ionViewDidLoad 方法中调用。
import { Navbar } from 'ionic-angular';
export class myCustomClass {
@ViewChild(Navbar) navBar: Navbar;
...
ionViewDidLoad() {
this.setBackButtonAction()
}
//Method to override the default back button action
setBackButtonAction(){
this.navBar.backButtonClick = () => {
//Write here wherever you wanna do
this.navCtrl.pop()
}
}
}
此代码已在 ionic 3 中测试。
以防万一有人在看。 Ionic 3 提供 life-cycle 事件。 "ionViewDidLeave" 或 "ionViewWillLeave" 均可用于此类目的。
查看文档以查看更多事件。 https://ionicframework.com/docs/api/navigation/NavController/
以防有人在使用后仍有问题
@ViewChild(Navbar) navBar: Navbar;
尝试不要将this.navbar.backButtonClick
放在constructor()
或者,您可以将它放在 ionViewDidLoad()
它应该可以工作。