简体   繁体   English

Angular2嵌套路由

[英]Angular2 nested routing

I have a dashboard which has a link to the Profile page. 我有一个仪表板,该仪表板具有指向“个人资料”页面的链接。 When a user goes to Profile page it displays his profile, which has various sections eg basic information, portfolio, hobbies etc. Each section has a Add button clicking on which displays Add Section form (eg Add Portfolio) 当用户转到“个人资料”页面时,会显示其个人资料,其中包含各个部分,例如基本信息,投资组合,兴趣爱好等。每个部分都有一个“添加”按钮,单击该按钮会显示“添加部分”表单(例如“添加投资组合”)

Dashboard url: /dashboard/ 仪表板网址: /dashboard/

Profile url: /dashboard/profile/ 个人资料网址: /dashboard/profile/

Add Portfolio url: /dashboard/profile/portfolio/new 添加投资组合网址: /dashboard/profile/portfolio/new

Edit Portfolio url: /dashboard/profile/portfolio/edit/:id 编辑投资组合网址: /dashboard/profile/portfolio/edit/:id

What I want is that when user clicks the Add Portfolio button it should display the Add Portfolio form just above the Portfolio list, without replacing the complete profile. 我想要的是,当用户单击“添加项目组合”按钮时,它应该在“项目组合”列表上方显示“添加项目组合”表单,而不替换完整的配置文件。 Similarly when edit link corresponding to a portfolio record is clicked it should display Edit Portfolio form above the list. 同样,当单击与项目组合记录相对应的编辑链接时,它应在列表上方显示“编辑项目组合”表单。

I am very new to Angular2 routing and can do simple routing, but if I implement it above with my current approach, I end up with links which when clicked replaces whole contents of the Profile page, probably because it only has one router outlet. 我是Angular2路由的新手,可以执行简单的路由,但是如果我用当前的方法在上面实现它,则会得到链接,当单击该链接时,它会替换Profile页面的全部内容,可能是因为它只有一个路由器出口。

Any help, probably a plunker demonstrating a above case would be highly appreciated. 任何帮助,可能是证明上述情况的小伙子,将不胜感激。

The Add Portfolio form should be a part of the Portfolio page rather than it's own page (ie they shouldn't have separate URLs). “添加投资组合”表单应该是“投资组合”页面的一部分,而不是它自己的页面(即,它们不应有单独的URL)。

You could make the Add Portfolio form into it's own component, and set it as hidden initially. 您可以将“添加投资组合”表单放入其自己的组件中,并将其设置为初始隐藏状态。 Assuming you make an AddPortfolio component with selector add-portfolio , then in the HTML for your Portfolio component you could have eg. 假设您使用选择器add-portfolio创建一个AddPortfolio组件,则在Portfolio组件的HTML中,您可以使用例如。

<add-portfolio [hidden]="hide"></add-portfolio>

Then in the AddPortfolio class you have a boolean field, hide , which is initially true , and is set to false on the Add button click. 然后,在AddPortfolio类中,您有一个布尔字段hide ,其最初为true ,并且在Add按钮上单击时设置为false

if you want to have separated url for create and edit portfolio scenario i might offer you following url separtation: 如果您想为创建和编辑投资组合方案使用单独的url ,我可以为您提供以下网址划分:

Dashboard url: /dashboard/view --> all profiles under dashboard 仪表板网址: /dashboard/view >仪表板下的所有配置文件

Profile url: /dashboard/profile/ 个人资料网址: /dashboard/profile/

Add Portfolio url: /dashboard/profile/portfolio/new 添加投资组合网址: /dashboard/profile/portfolio/new

Edit Portfolio url: /dashboard/profile/portfolio/edit/:id 编辑投资组合网址: /dashboard/profile/portfolio/edit/:id

Your entry point file gonna have look like this: 您的入口点文件将如下所示:

App.ts 应用程式

...
@RouteConfig([
  { path: '/dashboard', component: DashboardRoutedComponent, name: 'Dashboard' }
])
export class App {  }
}
...

DashboardRoutedComponent - we going to name components which serve routing purposes like RoutedComponent in the end DashboardRoutedComponent我们将最后命名用于路由目的的组件,例如RoutedComponent

App.html App.html

@Component({
  selector: 'app',
  providers: [ ...FORM_PROVIDERS ],
  directives: [ ...ROUTER_DIRECTIVES ],
  template: `
    <header>
    </header>

      <router-outlet></router-outlet>

    <footer>
    </footer>
  `
})

your DashboardRoutedComponent goint to define child routes 您的DashboardRoutedComponent定义了子路线

DashboardRoutedComponent.ts DashboardRoutedComponent.ts

@Component({
    selector:   'someSelectorDoesntMatter',
    template: '<router-outlet></router-outlet>' -->that's why we name it routed component
    directives: [
        ROUTER_DIRECTIVES
    ]
})
@RouteConfig([
    {
        path: '/view',
        name: 'View',
        useAsDefault: true,
        component: DashboardViewComponent,  --> here you going to display list of profiles
        data: { name: 'View' }
    },
    {
        path: '/profile/...',  --> here we show that component going to have child routes
        name: 'Profile',
        component: ProfileRoutedComponent,
        data: { name: 'Profile' }
    }
])
export class DashboardRoutedComponent {}

ProfileRoutedComponent will have same structure for view\\edit\\new. ProfileRoutedComponent具有与view \\ edit \\ new相同的结构。

In order to have list of portfolio in new\\edit scenarios they just going to share some portfolio list component and which will be re-render once new\\edit portfolio component going to share same services to fetch data. 为了在新的\\编辑方案中拥有some portfolio list component他们将只共享some portfolio list component ,并且一旦新的\\编辑投资组合组件共享相同的服务以获取数据时,它将重新呈现。

i will update answer with plunk 我会用塞子更新答案

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

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