简体   繁体   English

Angular2动态模板

[英]Angular2 dynamic templating

How do I use nested templates in angular2. 如何在angular2中使用嵌套模板。 So basically I have a base component and it's childs 所以基本上我有一个基本组成部分,它是孩子

- entry.component.ts
- entry.component.html
- navigation
-- sidenav.ts
-- sidenav.html
-route1
--route1.ts
--route1.html

entry.component.html should be a skeleton and the content should be generated dynamically on route changes. entry.component.html应该是骨架,并且内容应该在路线更改时动态生成。

Is it possible to achieve this? 有可能实现这一目标吗?

Use templates with child routes. 将模板与子路线一起使用。

I separate mine in public and secure routing everything through the layout then loading the routes for which ever layout is chosen. 我单独在我的publicsecure通过布局然后加载被选择用于其不断布局级路由的一切。

Make sure to export the child routes and that the correct routes are called in the layout route. 确保导出子路径,并在布局路径中调用正确的路径。 Also make sure you use redirectTo in each child routes file. 还要确保在每个子路由文件中使用redirectTo

app.routing.ts

    const APP_ROUTES: Routes = [
        { path: '', redirectTo: '/home', pathMatch: 'full', },
        { path: '', component: PublicComponent, data: { title: 'Public Views' }, children: PUBLIC_ROUTES },
        { path: '', component: SecureComponent, canActivate: [Guard], data: { title: 'Secure Views' }, children: SECURE_ROUTES }
    ];

Then I have a layouts folder 然后我有一个布局文件夹

layouts 版面

layouts/public/public.components.ts
layouts/public/public.components.html
layouts/secure/secure.components.ts
layouts/secure/secure.components.html

child routes /public/piublic.routes.ts 子路线 /public/piublic.routes.ts

 export const PUBLIC_ROUTES: Routes = [
        { path: '', redirectTo: 'home', pathMatch: 'full' },
        { path: 'p404', component: p404Component },
        { path: 'e500', component: e500Component },
        { path: 'login', component: LoginComponent },
        { path: 'register', component: RegisterComponent },
        { path: 'home', component: HomeComponent }
    ];

/secure/secure.routes.ts /secure/secure.routes.ts

export const SECURE_ROUTES: Routes = [
    { path: '', redirectTo: 'overview', pathMatch: 'full' },
    { path: 'items', component: ItemsComponent },
    { path: 'overview', component: OverviewComponent },
    { path: 'profile', component: ProfileComponent },


 { path: 'reports', component: ReportsComponent }
];

Creating components to load into the template 创建要加载到模板的组件

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

@Component({
  templateUrl: './benefits.component.html',
})
export class BenefitsComponent {

  constructor() { }

}

The create its html template, 创建其html模板,

/public/benefits.component.html'

Now that it is part of the public route. 现在,它已成为公共路线的一部分。 When someone goes to a public route it will be loaded in the chile routes. 当有人去公共路线时,它将被加载到智利路线中。 so inside your /public/public.routes.ts file you would add it for every new route you wanted to create to load your new html page. 因此,在您的/public/public.routes.ts文件中,您将为要创建的每条新路线添加该文件,以加载新的html页面。

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

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