无法导航到Angular中的特定页面

时间:2019-09-11 17:55:08

标签: angular angular6 angular5 nativescript angular-routing

我无法在我的角度应用程序中浏览特定页面,在这种情况下,我想从我的路由模块中的app.component.ts导航到(tabs/market-palce/fornecedores)

 Error: Cannot match any routes. URL Segment: 'tabs/market-palce/fornecedores'

如果从不是app.component.hmtl的其他页面转到并打印显示此内容的网址

/tabs/default/(marketTab:market-place/fornecedores//cartoesTab:cartoes/default//profileTab:profile/default//experienciaTab:experiencia/default)

我该如何从app.component.ts转到fornedores页面?

我该如何做?我没有任何想法。

请在app.comoponent.ts中尝试

 this.routerExtensions.navigate(['tabs/market-place/fornecedores', { relativeTo: this.activeRoute });

app.routing

    const routes: Routes = [

        {   path: "",
            redirectTo: "/login",
            pathMatch: "full"
        },

        {   path: "login",
            component: LoginComponent
        },

        {
            path: "tabs",
            loadChildren: "~/app/tabs/tabs.module#TabsModule"
        }
]

标签路由

const routes: Routes = [

    {
        path: "default", component: TabsComponent, children: [


            {
                path: "market-place",
                component: NSEmptyOutletComponent,
                loadChildren: "~/app/market-place/market-place.module#MarketPlaceModule",
                outlet: "marketTab"
            },
            {
                path: "cartoes",
                component: NSEmptyOutletComponent,
                loadChildren: "~/app/cartoes/cartoes.module#CartoesModule",
                outlet: "cartoesTab"
            },
            {
                path: "profile",
                component: NSEmptyOutletComponent,
                loadChildren: "~/app/profile/profile.module#ProfileModule",
                outlet: "profileTab"
            },
            {
                path: "experiencia",
                component: NSEmptyOutletComponent,
                loadChildren: "~/app/experiencia/experiencia.module#ExperienciaModule",
                outlet: "experienciaTab"
            }
        ]
     }
];

市场路由

const routes: Routes = [
    {path: "", redirectTo: "default" },
    {path: "default", component: MarketPlaceComponent},
    {path: "fornecedores", component: FornecedoresComponent }

];

2 个答案:

答案 0 :(得分:0)

如您在documentation

中所见

{   path: "",
    redirectTo: "/login",
    pathMatch: "full"
},

到数组的末尾。

  

从技术上讲,当URL的其余不匹配段匹配''时,pathMatch ='full'导致路由命中。在此示例中,重定向位于顶层路由中,因此其余URL和整个URL都是相同的。

     

另一个可能的pathMatch值是'prefix',它告诉路由器   当其余网址以开头时,匹配重定向路由   重定向路由的前缀路径。

     

在这里不要这样做。如果pathMatch值为'prefix',则每个URL   会匹配“”。

     

尝试将其设置为“前缀”,然后单击“转到同伴”按钮。   请记住,这是一个错误的URL,您应该看到“找不到页面”   页。相反,您仍在“英雄”页面上。在中输入错误的网址   浏览器地址栏。您立即被重新路由到/ heroes。每一个   属于此路由定义的URL(好或坏)将是   一场比赛。

     

仅当以下情况时,默认路由才应重定向到HeroListComponent:   整个网址是“”。记住将重定向恢复到pathMatch =   “满”。

     

在Victor Savkin的重定向文章中了解更多信息。

答案 1 :(得分:0)

配置

首先,我想您将被重定向到login路由或页面。这很正常。

当角度与路径匹配时,""首先出现,它在"/login"上重定向,之后是"login"然后是"tabs"

您的问题是路由""与每条路由都匹配,甚至与"tabs"匹配,因此您总是被重定向到/login路径。

然后您可能需要将空路径放在最后,例如:

const routes: Routes = [
  {   
      path: "login",
      component: LoginComponent
  },
  {
      path: "tabs",
      loadChildren: "~/app/tabs/tabs.module#TabsModule"
  }
  {   
      path: "",
      redirectTo: "/login",
      pathMatch: "full"
  },
]

(我个人通常使用"**"而不是"",因为我想知道这是我的路的尽头,并且会在未知路径上重定向)

您可以在the official documentation's page上了解有关所有这些技巧的更多信息。

导航

现在,如果要导航,则应使用Router服务并提供路径。

您可以使用绝对路径更快,例如使用

/tabs/market-palce/fornecedores

代替

tabs/market-palce/fornecedores

(请注意开头的/

constructor(
   ...
   private router: Router,
   ...
) {...}

navigateToTabs() {
   const path = '/tabs/market-palce/fornecedores'
   this.router.navigate([path]); // <-- Here the is your path
}

您可能还想了解更多使用角度here进行导航的方法,再次在the official documentation's page