简体   繁体   English

如何在 Angular2 中配置路由?

[英]How to configure routes in Angular2?

I have requirement like我有这样的要求在此处输入图片说明

header, sidemenu, innerpages(profile,about..)标题,侧边菜单,内页(个人资料,关于..)

I want 3 seperate routes(1-header,2-sidemenu,3-innerpages) when application is loading profile page is come first当应用程序加载配置文件页面时,我想要 3 个单独的路由(1-header,2-sidemenu,3-innerpages)

I tried like我试过

app.rouutes.ts app.rouutes.ts

const appRoutes: Routes = [
  {
    path: '', redirectTo: '/business', pathMatch: 'full'
  }
];
@NgModule({
  imports: [
    RouterModule.forRoot(appRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {}

business.routes.ts business.routes.ts

const BUSINESS_ROUTES: Routes = [
  { path: 'header',  component: BusinessHeaderComponent },
  { path: 'sidemenu',  component: BusinessSidemeuComponent },

  { path: 'about',  component: BusinessComponent }

  ];

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

Please help me to configure it请帮我配置它

you're mixing the concept of routes with layouts, routes are reflection of urls, if you enter /business/header you will get BusinessHeaderComponent alone, that's routing... what you need is child routes, when you enter business/about, it should load the business header, sidemenu and about component您将路由的概念与布局混合在一起,路由是 url 的反映,如果您输入 /business/header,您将单独获得 BusinessHeaderComponent,这就是路由……您需要的是子路由,当您输入 business/about 时,它应该加载业务标题、侧边菜单和关于组件

{
  path: '', redirectTo: '/business', pathMatch: 'full'
},
{
    path: 'business',
    component: BusinessLayoutComponent,
    children: [
      {
        path: 'about',
        component: AboutComponent,
      },
    ]
  },
 {
    path: 'user',
    component: UserLayoutComponent,
    children: [
      {
        path: 'about',
        component: AboutComponent,
      },
    ]
  },

Now inside BusinessLayoutComponent and UserLayoutComponent templates, add your header and side bar as pure html, no need for components for that, because that's the only place they would be defined现在在 BusinessLayoutComponent 和 UserLayoutComponent 模板中,将您的标题和侧边栏添加为纯 html,不需要组件,因为这是唯一定义它们的地方

The layout components are very simple HTML pages with headers and sidebars and a single <router-outlet></router-outlet> tag to contain the child components.布局组件是非常简单的 HTML 页面,带有标题和侧边栏以及一个包含子组件的<router-outlet></router-outlet>标签。

The child components could be anything, like an about page with About content.子组件可以是任何东西,比如带有 About 内容的 about 页面。

Of course you can use the same about component in both routes当然,您可以在两条路线中使用相同的 about 组件

import { Component} from '@angular/core';

@Component({
    templateUrl: 'common/about.html'
})
export class AboutComponent {
    constructor() { }
}

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

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