NavController 在 Ionic 4 中不起作用

NavController doesn't work in Ionic 4

我在构造函数中注入了 NavController,因为我想推送一个页面。但是,下面的代码在 Ionic 4 中不起作用。在 Ionic 3 中完全没问题。

Constructor

constructor(public menuCtrl: MenuController, public navCtrl: NavController) {
    this.menuCtrl.enable(true);
   }

Method

goToSecondPage()
  {
    this.navCtrl.push(list);
  }

this.navCtrl.push(list);

它在 Ionic 4 中不起作用。Ionic 4 基于 Angular 路由。因此,只需使用以下代码,并为此编写一条路线。

this.navCtrl.goForward('/list');

For back button in NavBar

在第 2 页的后退按钮 <ion-toolbar> </ion-toolbar> 中粘贴以下代码。

<ion-buttons slot="start">
      <ion-back-button  defaultHref="home"></ion-back-button>
    </ion-buttons>

IONIC 4 - Angular (ionic start appName --type=angular)

物理后退按钮后退按钮菜单

在目标页面(第二页)中,执行:

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

第二页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.:

HTML 中的菜单链接可能是必需的:

[routerDirection] = "'forward'"

喜欢 "app.component.html" 项目 sidemenu

<ion-item [routerDirection]="'forward'" [routerLink]="[p.url]">

查看更多:https://beta.ionicframework.com/docs/api/buttons/

现在,要完成最后一步并在我们的 app-routing.module.ts 文件中实现这些路由,它们将如下所示:

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' }
];

在html页中设置Root

<ion-button href="/support" routerDirection="root">

或 class

this.navCtrl.navigateRoot('/support');

推送

<ion-button href="/products/12" routerDirection="forward">

this.navCtrl.navigateForward('/products/12');

流行

<ion-button href="/products" routerDirection="backward">

<ion-back-button defaultHref="/products"></ion-back-button>

您还可以通过编程向后导航:

this.navCtrl.navigateBack('/products');

p/s: https://www.joshmorony.com/converting-ionic-3-push-pop-navigation-to-angular-routing-in-ionic-4/

NavController,因为它在 IONIC 4 中已弃用。

结构是这样的

     V3                 V4
/src/pages     ->   /src/app/pages
/src/models    ->   /src/app/models
/src/providers ->   /src/app/providers
  • 如果你有 pages 目录,你可以使用 pages。
  • 想传就可以用参数

    this.router.navigate('/pages, { locs: this.locId }])');

示例:使用 Pages 目录。

this.router.navigate(['/list'], { locs: this.locId }]);

示例:没有 Pages 目录和参数。

this.router.navigate(['/list']);

这 link 对选项卡很有用。 要了解更多信息,请阅读此 link。 [https://medium.com/modus-create-front-end-development/upgrading-an-ionic-3-application-to-ionic-4-eaf81a53cdea][1]


额外内容:

After navigate to new page, we can get the locsId using this.route.snapshot.paramMap.get('locs') by importing private route: ActivatedRoute inside the current page constructor

示例:

export class ListPage implements OnInit {

  constructor(private route: ActivatedRoute) {
    console.log("this.route.snapshot.paramMap.get : ", this.route.snapshot.paramMap.get('locs'));
  }

  ngOnInit() {
  }

}
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

首先导入导航控制器 从'@ionic/angular'导入{NavController, AlertController}; 现在继续前进也不支持它在 ionic 3 中是最好的 this.nav.navigateForward('/ChatPage') 在 ionic 4 中受支持,但我的建议应该使用 angular 路由

您可以使用“从 'ionic-angular' 导入 { navController }”来解决这个问题 但这会给你带来 Rxjs 的错误。所以你可能想这样做...

在您的应用程序 routing.module.ts 中,确保您的页面及其模块在路径和 loadChildren 中正确指定。这是我的

const routes: Routes = [ { path: '', redirectTo: 'login', pathMatch: 'full' },

{ path: 'register', loadChildren: './register/register.module#RegisterPageModule' },
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' },
{ path: 'register', loadChildren: './register/register.module#RegisterPageModule' },
];

在我的 login.page.html 中,我用它导航到 registerPage

<ion-button size="large" routerLink="/register" routerDirection="forward" expand="block" #btnregister fill="clear" color="primary">Register</ion-button>
 this.deeplink.route({
          '/registration/:userid': RegistrationPage,
          '/registration': RegistrationPage,
      }).subscribe((match) => {  
        this.router.navigateByUrl(match.$link.path, match.$args)  
        console.log("Deeplink =>", match);

        }, err => {
          console.log("Deeplink Error=>", err)
          alert("Deeplink Error=>" + err)
        })
    })

Try (in ionic 4)

import { NavController } from '@ionic/angular';

instead of (in ionic 3)

import { NavController } from 'ionic-angular'