Ionic 4路由url在不更改应显示页面的情况下显示了路由

时间:2019-02-12 19:22:35

标签: angular routing ionic4

我在Ionic 4路由中遇到问题。

成功登录后,我需要使用以下命令将用户重定向到/home模块:

this.navCtrl.navigateForward('home')

我已经使用以下方法在NavController中导入了constructor()

private navCtrl: NavController

app-routing.module.ts如下:

const routes: Routes = [
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  },
  {
    path: 'home',
    loadChildren: './home/home.module#HomePageModule'
  },
  {
    path: 'list',
    loadChildren: './list/list.module#ListPageModule'
  },
  { 
    path: 'login', 
    loadChildren: './login/login.module#LoginPageModule' 
  }
];

app.module.ts处,引导程序模块设置为LoginPage

@NgModule({
  declarations: [LoginPage],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    ReactiveFormsModule,
    HttpClientModule  
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [LoginPage]
})

当我运行移动应用程序时,显示的网址是:

http://localhost:8100/home

这很奇怪,应该是http://localhost:8100http://localhost:8100/login

HomePage包含带有router-outlet的侧面菜单:

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Home
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
    <ion-card class="welcome-card">
      <ion-img src="/assets/shapes.svg"></ion-img>
      <ion-card-header>
        <ion-card-subtitle>Get Started</ion-card-subtitle>
        <ion-card-title>Welcome to Ionic</ion-card-title>
      </ion-card-header>
      <ion-card-content>
        <p>Now that your app has been created, you'll want to start building out features and components. Check out some of the resources below for next steps.</p>
      </ion-card-content>
    </ion-card>
    <ion-list lines="none">
      <ion-list-header>
        <ion-label>Resources</ion-label>
      </ion-list-header>
      <ion-item href="https://ionicframework.com/docs/">
        <ion-icon slot="start" color="medium" name="book"></ion-icon>
        <ion-label>Ionic Documentation</ion-label>
      </ion-item>
      <ion-item href="https://ionicframework.com/docs/building/scaffolding">
        <ion-icon slot="start" color="medium" name="build"></ion-icon>
        <ion-label>Scaffold Out Your App</ion-label>
      </ion-item>
      <ion-item href="https://ionicframework.com/docs/layout/structure">
        <ion-icon slot="start" color="medium" name="grid"></ion-icon>
        <ion-label>Change Your App Layout</ion-label>
      </ion-item>
      <ion-item href="https://ionicframework.com/docs/theming/basics">
        <ion-icon slot="start" color="medium" name="color-fill"></ion-icon>
        <ion-label>Theme Your App</ion-label>
      </ion-item>
    </ion-list>
</ion-content>
<ion-router-outlet></ion-router-outlet>

P.S .:

  1. 我改用angular router,结果也一样。
  2. navCtrl.push()在Ionic 4中不再可用。

1 个答案:

答案 0 :(得分:0)

确定要在ion-router-outlet中使用app.component.html吗?

<ion-app>
  <ion-router-outlet></ion-router-outlet>
</ion-app>