可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm injecting NavController in my constructor as I want to push a page. But, below code doesn't work in Ionic 4. It was totally okay in Ionic 3.
Constructor
constructor(public menuCtrl: MenuController, public navCtrl: NavController) {
this.menuCtrl.enable(true);
}
Method
goToSecondPage()
{
this.navCtrl.push(list);
}
回答1:
Now, to complete the final step and implement those routes in our app-routing.module.ts file, they would look like this:
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', loadChildren: './pages/home/home.module#HomeModule' },
{ path: 'products', loadChildren: './pages/products/products.module#ProductsModule'},
{ path: 'products/:id', loadChildren: './pages/product-detail/product-detail.module#ProductDetailModule' },
{ path: 'products/categories', loadChildren: './pages/product-categories/product-categories.
{ path: 'support', loadChildren: './pages/support/support.module#SupportModule' }
];
setRoot in html page
<ion-button href="/support" routerDirection="root">
or in class
this.navCtrl.navigateRoot('/support');
Push
<ion-button href="/products/12" routerDirection="forward">
or
this.navCtrl.navigateForward('/products/12');
Pop
<ion-button href="/products" routerDirection="backward">
or
<ion-back-button defaultHref="/products"></ion-back-button>
you can also navigate backwards programatically:
this.navCtrl.navigateBack('/products');
p/s: https://www.joshmorony.com/converting-ionic-3-push-pop-navigation-to-angular-routing-in-ionic-4/
回答2:
this.navCtrl.push(list);
It doesn't work in Ionic 4. Ionic 4 is based on Angular Routing. So, just use the following code, and write a route for this.
this.navCtrl.goForward('/list');
For back button in NavBar
Paste following code in <ion-toolbar> </ion-toolbar>
for back button in the 2nd page.
<ion-buttons slot="start">
<ion-back-button defaultHref="home"></ion-back-button>
</ion-buttons>
回答3:
first import navcontroller
import { NavController, AlertController } from '@ionic/angular';
and now go forward also dont support it was best in ionic 3
this.nav.navigateForward('/ChatPage') supported in ionic 4 for but my sugesstion one should use angular routing
回答4:
IONIC 4 - Angular (ionic start appName --type=angular)
Physical Back Button and Back Button Menu
In the Target page (second page), do:
SecondPage TS
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { Platform } from '@ionic/angular';
@Component({
selector: 'app-second',
templateUrl: './second.page.html',
styleUrls: ['./second.page.scss'],
})
export class SecondPage implements OnInit, OnDestroy {
inscBackAction: Subscription;
element: HTMLElement;
constructor(
private router: Router,
public platform: Platform) {
}
ngOnInit() {
this.inscBackAction = this.platform.backButton.subscribe(() => {
// Check this log in chrome: "chrome://inspect/#devices"
console.log('Physical Back Button');
this.element = document.getElementById('backButton') as HTMLElement;
this.element.click();
// OR
// this.router.navigate(['/anyPage']);
}, error => {
console.log('\n\nERROR:\n' + error.message + '\n\n');
});
}
ngOnDestroy() {
this.inscBackButton.unsubscribe();
}
}
SecondPage HTML
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
<!-- Does not work with #backButton -->
<ion-back-button id="backButton" defaultHref="/"></ion-back-button>
</ion-buttons>
<ion-title>
Second
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding></ion-content>
_
P.S.:
May be necessary for menu links in HTML:
[routerDirection] = "'forward'"
Like in "app.component.html" on projects sidemenu
<ion-item [routerDirection]="'forward'" [routerLink]="[p.url]">
See More:https://beta.ionicframework.com/docs/api/buttons/
回答5:
NavController as this is deprecated in IONIC 4.
Structure is like this.
V3 V4
/src/pages -> /src/app/pages
/src/models -> /src/app/models
/src/providers -> /src/app/providers
- You can use pages if you have pages directory.
You can use parameters if you want to pass it.
this.router.navigate('/pages, { locs: this.locId }])');
Example: With Pages directory.
this.router.navigate(['/list', { locs: this.locId }]);
Example: Without Pages directory and parameters.
this.router.navigate(['/list');
This link is useful for the tabs.
For more Info go through this link.
[https://medium.com/modus-create-front-end-development/upgrading-an-ionic-3-application-to-ionic-4-eaf81a53cdea][1]
回答6:
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
...
})
export class LoginComponent {
constructor(private router: Router){}
navigate(){
this.router.navigate(['/detail'])
}
}
for more info click here