简体   繁体   English

角4子路由

[英]angular 4 child routing

Im trying to write routing for childs. 我正在尝试为孩子们写路由。 The problem Im having is with path not beeing inserted in url bar of thhe browser properly. 我遇到的问题是浏览器的网址栏中没有正确插入路径。 Below the code. 下面的代码。

router.module router.module

const routes: Routes = [

  {path: 'menu', component: MenuComponent, canActivate: [AuthGuard],
    children: [
      { path: 'upload', component: DrawingUploadComponent },
      { path: 'account', component: AccountComponent },
      { path: 'projects', component: ProjectListComponent }
    ]}
];

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

export class MenuRoutingModule {}

menu.component menu.component

<nav>
  <a routerLink="/projects" routerLinkActive="active">Projects</a>
  <a routerLink="/upload" routerLinkActive="active">Upload</a>
  <a routerLink="/account" routerLinkActive="active">Account</a>
</nav>
MENU COMPONENT
<router-outlet></router-outlet>

the parent component: 父组件:

const routes: Routes = [
  {path: 'login', component: LoginComponent},
  {path: 'register', component: RegistrationComponent},
  {path: "", redirectTo: "login", pathMatch: "full"},
  {path: 'menu', component: MenuComponent, canActivate: [AuthGuard]}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule {
}

So the problem is when im under "/menu" the menu component is displayed, now when Im clicking on any link eg 所以问题是当im在"/menu"下时显示菜单组件,现在当Im单击任何链接时,例如

<a routerLink="/upload" routerLinkActive="active">Upload</a>

im getting console error Cannot match any routes. 即时通讯收到控制台错误无法匹配任何路由。 URL Segment: 'upload' Error: Cannot match any routes. URL段:“上载”错误:无法匹配任何路由。 URL Segment: 'upload' 网址段:“上传”

but when i'd type the route by hand in browser bar "/menu/upload" I'm getting the proper result 但是当我在浏览器栏“ / menu / upload”中手动输入路线时,我得到了正确的结果

When your route starts with / , that refers to the root of your app. 当您的路线以/开头时,即表示您应用的根目录。 You'll need to remove that, or else use the full path eg routerLink="/menu/upload" . 您需要删除该路径,否则请使用完整路径,例如routerLink="/menu/upload"

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

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