简体   繁体   English

离子 Angular 路由与标签

[英]Ionic Angular Routing With Tabs

I have the following in app-routing.module:我在 app-routing.module 中有以下内容:

const routes: Routes = [
  {
    path: 'displayfile/:navitemid',
    loadChildren: () => import('./pages/file-viewer/file-viewer.module').then( m => m.FileViewerPageModule),
    canActivate: [DisplayFileGuardService]
  },
  {
    path: 'file-viewer',
    loadChildren: () => import('./pages/file-viewer/file-viewer.module').then( m => m.FileViewerPageModule)
  },
  {
    path: 'settings',
    loadChildren: () => import('./pages/settings/settings.module').then( m => m.SettingsPageModule)
  },
  { 
    path: '',
    loadChildren: () => import('./pages/file-viewer/file-viewer.module').then( m => m.FileViewerPageModule),
  }
];

A URL like "http://localhost:8100/displayfile/1234" should match the first route.像“http://localhost:8100/displayfile/1234”这样的 URL 应该匹配第一个路由。 It was doing so before I refactored my app to introduce a tabbed interface.在我重构我的应用程序以引入选项卡式界面之前,它就是这样做的。 But it just seems to ignore this route.但它似乎只是忽略了这条路线。

My file-viewer-routing.module where the tabs live lis included below.我的 file-viewer-routing.module 包含下面的选项卡。 I've read docs and tried many different combinations without success.我已经阅读了文档并尝试了许多不同的组合但没有成功。 I've got logging in the "DisplayFileGuardService" constructor and canActivate methods.我已经登录了“DisplayFileGuardService”构造函数和 canActivate 方法。 So I know it is not hit.所以我知道它没有被击中。 If I add that guard to the last route then it does get hit with the URL that is failing above.如果我将该警卫添加到最后一条路线,那么它确实会被上面失败的 URL 击中。 Why is it falling through to the empty route and ignoring my displayfile route?为什么它会掉到空路由并忽略我的显示文件路由?

const routes: Routes = [
  {
    path: 'tabs',
    component: FileViewerPage,
    children: [
      {
        path: 'tab1',
        loadChildren: () => import('../tab1/tab1.module').then(m => m.Tab1PageModule),
        //canActivate: [DisplayFileGuardService]
      },
      {
        path: 'tab2',
        loadChildren: () => import('../tab2/tab2.module').then(m => m.Tab2PageModule)
      },
      {
        path: 'tab3',
        loadChildren: () => import('../tab3/tab3.module').then(m => m.Tab3PageModule)
      },
      {
        path: 'tab4',
        loadChildren: () => import('../tab4/tab4.module').then(m => m.Tab4PageModule)
      },
      {
        path: 'tab5',
        loadChildren: () => import('../tab5/tab5.module').then(m => m.Tab5PageModule)
      },
      {
        path: '',
        redirectTo: '/tabs/tab1',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/tab1',
    pathMatch: 'full'
  }

I had a project with something similar and this is how I did it.我有一个类似的项目,我就是这样做的。 This is my app-routing:这是我的应用程序路由:

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./pages/tabs/tabs.module').then( m => m.TabsPageModule )
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

And then on the tabs module I have the following routing:然后在选项卡模块上,我有以下路由:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ListStoredMoviesComponent } from 'src/app/shared/components/list-stored-movies/list-stored-movies.component';
import { MoviesByCastComponent } from 'src/app/shared/components/movies-by-cast/movies-by-cast.component';

import { TabsPage } from './tabs.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'upcoming',
        loadChildren: () => import('./../../pages/upcoming/upcoming.module').then( m => m.UpcomingPageModule)
      },
      {
        path: 'search',
        children: [
          {
            path: '',
            loadChildren: () => import('./../../pages/search/search.module').then( m => m.SearchPageModule)
          },
          {
            path: 'cast',
            component: MoviesByCastComponent
          } 
        ]
      },
      {
        path: 'profile',
        children: [
          {
            path: '',
            loadChildren: () => import('./../../pages/profile/profile.module').then( m => m.ProfilePageModule)
          },
          {
            path: 'lists/details',
            component: ListStoredMoviesComponent
          }
        ]
      }
    ]
  },
  {
    path: '',
    redirectTo: 'tabs/upcoming',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class TabsPageRoutingModule {}

The second routes file is maybe what you are looking for that's the one taking all the requests.第二个路由文件可能是您正在寻找的那个接受所有请求的文件。 Also all my requests are like localhost:4200/search/XXX too.我所有的请求也都像 localhost:4200/search/XXX 一样。

When working with tabs all the routing must be held through the tabs.使用选项卡时,所有路由都必须通过选项卡进行。 Here's the Ionic's explanation: https://ionicframework.com/docs/angular/navigation#working-with-tabs这是离子的解释: https://ionicframework.com/docs/angular/navigation#working-with-tabs

Well I am a bit embarassed to say that in the end the reason I was having so much trouble with routing was that I had added some code to navigate to the route of my app in ngOnInit of app.component.好吧,我有点尴尬地说,最后我在路由方面遇到这么多麻烦的原因是我在 app.component 的 ngOnInit 中添加了一些代码来导航到我的应用程序的路由。 Anyway after sorting that out I was then able to get my routing working as desired with tabs using the following combination:无论如何,在整理出来之后,我就可以使用以下组合让我的路由使用选项卡按需要工作:

In app-routing.module:在 app-routing.module 中:

const routes: Routes = [
......
      {
        path: 'displayfile/:navitemid',
        redirectTo: 'tabs/displayfile/:navitemid'
      },
......

Then in routing module of the component that contains the tabs:然后在包含选项卡的组件的路由模块中:

const routes: Routes = [
  {
    path: 'tabs',
    component: FileViewerPage,
    children: [
      {
        path: 'displayfile/:navitemid',
        canActivate: [DisplayFileGuardService],
      },
.....

And then finally in my guard some code to identify the parameter, post an appropriate event and then navigate to the correct route:然后最后在我的守卫中使用一些代码来识别参数,发布适当的事件,然后导航到正确的路线:

canActivate(route: ActivatedRouteSnapshot): boolean {
    let navItemId = route.paramMap.get("navitemid");
    if (navItemId) {
      this.events.publishEventDeepLinkingInitiated(navItemId);
      this.router.navigate([""]);

      return false;
    }

    return true;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM