简体   繁体   English

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

[英]Cant navigate to specific page in Angular

I cant navigate o specific page in my angular app, in this case i want to navigate to (tabs/market-palce/fornecedores) from app.component.ts here my routing Modules. 我无法在我的有角度的应用程序中浏览特定页面,在这种情况下,我想从我的路由模块中的app.component.ts导航至(tabs/market-palce/fornecedores)

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

and if go from other page thats not app.component.hmtl and print the url it show this 如果从其他页面而不是app.component.hmtl并打印网址,则显示此内容

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

How could i go to fornedores page from app.component.ts ??? 我怎么能从app.component.ts转到fornedores页面?

How could i make this work?? 我该怎么做? i have no ideia. 我没有任何想法。

im trying this in app.comoponent.ts 我正在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"
        }
]

tabs routing 标签路由

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"
            }
        ]
     }
];

market-place Routing 市场路由

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

];

As you can see in the documentation 如您在文档中所见


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

to the end of the array. 到数组的末尾。

Technically, pathMatch = 'full' results in a route hit when the remaining, unmatched segments of the URL match ''. 从技术上讲,当URL的其余未匹配段匹配''时,pathMatch ='full'导致路由命中。 In this example, the redirect is in a top level route so the remaining URL and the entire URL are the same thing. 在此示例中,重定向位于顶层路由中,因此其余URL和整个URL是同一件事。

The other possible pathMatch value is 'prefix' which tells the router to match the redirect route when the remaining URL begins with the redirect route's prefix path. 另一个可能的pathMatch值是'prefix',当剩余的URL以重定向路由的前缀路径开头时,它告诉路由器匹配重定向路由。

Don't do that here. 不要在这里做。 If the pathMatch value were 'prefix', every URL would match ''. 如果pathMatch值为'prefix',则每个URL都将匹配“”。

Try setting it to 'prefix' then click the Go to sidekicks button. 尝试将其设置为“前缀”,然后单击“转到同伴”按钮。 Remember that's a bad URL and you should see the "Page not found" page. 请记住,这是一个错误的URL,您应该看到“找不到页面”页面。 Instead, you're still on the "Heroes" page. 相反,您仍在“英雄”页面上。 Enter a bad URL in the browser address bar. 在浏览器地址栏中输入错误的URL。 You're instantly re-routed to /heroes. 您立即被重新路由到/ heroes。 Every URL, good or bad, that falls through to this route definition will be a match. 符合此路由定义的每个URL(无论好坏)都是一个匹配项。

The default route should redirect to the HeroListComponent only when the entire url is ''. 仅当整个网址为“”时,默认路由才应重定向到HeroListComponent。 Remember to restore the redirect to pathMatch = 'full'. 请记住,将重定向恢复为pathMatch ='full'。

Learn more in Victor Savkin's post on redirects. 在Victor Savkin的重定向文章中了解更多信息。

Configuration 组态

First I suppose you are being redirected to the login route or page. 首先,我想您将被重定向到login路径或页面。 It's quite normal. 这很正常。

When angular matches paths, the "" comes first, which redirects on the "/login" , after what the "login" and then the "tabs" . 当角匹配路径,则""至上,被重定向的"/login" ,之后是什么"login" ,然后将"tabs"

Your problem is that the route "" matches with every route, even "tabs" so you're always redirected to the /login path. 您的问题是路由""与所有路由(甚至是"tabs"都匹配,因此您总是被重定向到/login路径。

You may then need to place the empty path at the end, such as: 然后,您可能需要将空路径放在最后,例如:

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

(Personally I usually use "**" instead of "" because I want to be aware that It's the end of my roads, and to be redirected on unknowns paths) (我个人通常使用"**"代替""因为我想知道这是我的路的尽头,并且会在未知路径上重定向)

You can read more about all these tricks on the official documentation's page . 您可以在官方文档的页面上阅读有关所有这些技巧的更多信息。

Navigation 导航

Now if you want to navigate, you should use the Router -service and give the path. 现在,如果要导航,则应使用Router -service并提供路径。

You may use the absolute path to be faster, like using 您可以使用绝对路径更快,例如使用

/tabs/market-palce/fornecedores

instead of 代替

tabs/market-palce/fornecedores . tabs/market-palce/fornecedores

(Notice the / at the beginning) (请注意/开头)

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

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

You may also want to learn more ways of navigating using angular here , again on the official documentation's page 您可能还想在此处 (在官方文档的页面上)了解更多使用angular导航的方法

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

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