1
     For days now I've been trying to route/link pages via the side-menu. So that I'm able to move to different pages from the side-menu itself. I've searched for tutorials on Angular 8 and Ionic 4 but couldn't find the specific article that relates to my issue.

我想通过创建页面组件(将在侧面菜单上的项目)来路由页面,以便于路由。我也有点知道要使用哪些代码进行路由,但不知道在哪里使用它,因为在错误的地方使用代码不会为我提供任何解决方案。

This is my app.component.ts file.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule, routeComponents } from './app-routing.module';

@NgModule({
  declarations: [AppComponent, routeComponents],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}  

下面是app-routing.module.ts文件。

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { TrackComponent } from './track/track.component';
import { AboutComponent } from './about/about.component';
import { CategoryComponent } from './category/category.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: () => import('./home/home.component').then( m => 
m.HomeComponent)},
  { path: 'category', loadChildren: () => import('./category/category.component').then( m => 
m.CategoryComponent)},
  { path: 'track', loadChildren: () => import('./track/track.component').then( m => 
m.TrackComponent)},
  { path: 'about', loadChildren: () => import('./about/about.component').then( m => 
m.AboutComponent)}
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }
export const routeComponents = [CategoryComponent, TrackComponent, AboutComponent, 
HomeComponent];

如果我还需要修改每个菜单项的 html 文件,请告诉我。如果可能,请尝试用外行的方式向我解释。

4

1 回答 1

0

在 ts 文件中:

import { Router } from "@angular/router";

constructor(private router:Router){}
navtigateToPage(pageName){
 this.router.navigate([pageName]);
}

在 html 文件中:

<ion-item (click)="navtigateToPage('track')">
<ion-label>track</ion-label>
</ion-item>

(单击)可以添加到任何节点元素(div、span、ion-icon...)

于 2020-02-18T15:05:06.330 回答