代码之家  ›  专栏  ›  技术社区  ›  Abhijit Mondal Abhi

导航控制器在离子4中不工作

  •  11
  • Abhijit Mondal Abhi  · 技术社区  · 6 年前

    我正在我的构造器中注入NavController,因为我想推一个页面。但是,下面的代码在离子4中不起作用。在爱奥尼亚3里完全没问题。

    构造器

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

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

    Error

    9 回复  |  直到 6 年前
        1
  •  43
  •   Sáng Trần Công    6 年前

    现在,完成最后一步并在我们的 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页面中的setRoot

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

    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');
    

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

        2
  •  9
  •   Mihir Patel    5 年前

    导航控制器,因为这是不赞成在离子4。

    结构是这样的。

         V3                 V4
    /src/pages     ->   /src/app/pages
    /src/models    ->   /src/app/models
    /src/providers ->   /src/app/providers
    
    • 如果要传递参数,可以使用参数。

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

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

    示例:没有页面目录和参数。

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

    此链接对选项卡很有用。 有关更多信息,请访问此链接。 https://medium.com/modus-create-front-end-development/upgrading-an-ionic-3-application-to-ionic-4-eaf81a53cdea][1]


    附加条款:

    this.route.snapshot.paramMap.get('locs') 通过导入 private route: ActivatedRoute 在当前页构造函数中

    export class ListPage implements OnInit {
    
      constructor(private route: ActivatedRoute) {
        console.log("this.route.snapshot.paramMap.get : ", this.route.snapshot.paramMap.get('locs'));
      }
    
      ngOnInit() {
      }
    
    }
    
        3
  •  5
  •   Edward Daniel Vidal Garcia    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

        4
  •  2
  •   wagi malick    6 年前

    首次导入导航控制器 从'@ionic/angular'导入{NavController,AlertController}; 此.nav.navigateForward('/ChatPage') ionic 4支持,但我建议应该使用角路由

        5
  •  2
  •   bunny    5 年前

    试一下 离子4 )

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

    代替 )

    import { NavController } from 'ionic-angular'
    
        6
  •  1
  •   Abhijit Mondal Abhi    6 年前
    this.navCtrl.push(list);
    

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

    用于导航栏中的后退按钮

    粘贴下列代码 <ion-toolbar> </ion-toolbar> 第二页的后退按钮。

    <ion-buttons slot="start">
          <ion-back-button  defaultHref="home"></ion-back-button>
        </ion-buttons>
    
        7
  •  1
  •   Cesar Devesa    6 年前

    离子4-角度(离子启动应用程序名称--类型=角度)

    物理后退按钮

    在目标页(第二页)中,执行以下操作:

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

    <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>
    

    附笔。:

    [routerDirection] = "'forward'"
    

    就像在项目中的“app.component.html” 侧菜单

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

    https://beta.ionicframework.com/docs/api/buttons/

        8
  •  1
  •   Dart Vadar    5 年前

    你可以用“从‘离子angulr’导入{navController}”来解决这个问题 但这会给你一个Rxjs的错误。所以你可能想这么做。。。

    在app-routing.module.ts中,确保在path和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' },
    ];
    

    <ion-button size="large" routerLink="/register" routerDirection="forward" expand="block" #btnregister fill="clear" color="primary">Register</ion-button>
    
        9
  •  0
  •   Bhavik Kalariya    5 年前
     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)
            })
        })