简体   繁体   English

Angular2 - 从数组中注册新路由

[英]Angular2 - register new Routes from an array

I am using Angular2 for my frontend and now I need to register new Routes from an array. 我正在使用Angular2作为我的前端,现在我需要从数组中注册新的路由。

I've got a service which gets the data from a server. 我有一个从服务器获取数据的服务。 Within my AppComponent this data is stored in a variable. 在我的AppComponent中,此数据存储在变量中。

This is the function calling my Service: 这是调用我的服务的功能:

getSpaces() {
      this.spaceService.getData()
         .then(data => {
            this.spaces = data;
            for (var i = 0; i < this.spaces.length; i++) {
               var item = { label: this.spaces[i].name, icon: 'fa-list', routerLink: ['/database' + this.spaces[i].name] }
               this.itempanel[1].items[1].items.push(item);
            }
         });
   }

What I do now is pushing the name properties from "spaces" into my panelmenu (primeng). 我现在所做的是将名称属性从“空格”推送到我的panelmenu(primeng)。 As you can see I am appending a routerLink for each spaces.name, but by now these routes aren't within my @Routes decorator. 正如您所看到的,我为每个spaces.name附加了一个routerLink,但是现在这些路由不在我的@Routes装饰器中。

So my question is: How can I register my Routes in a dynamic way? 所以我的问题是:如何以动态的方式注册我的路线? They should look sth like this: 他们应该看起来像这样:

new Route {path: 'database'+this.spaces[i].name, component: SpaceComponent}

I couldnt find anything useful in the docs, but I might have missed something. 我在文档中找不到任何有用的东西,但我可能错过了一些东西。 Any ideas? 有任何想法吗?

I know that I can register Routes with a param: 我知道我可以用一个参数注册路由:

@Routes([
{path:'database/:spacename', component: SpaceComponent}
])

But I don't know how to work with that, cause I'm not that deep into Angular2(or even programming by itself). 但我不知道如何使用它,因为我不是那么深入Angular2(甚至自己编程)。 So could you guys either explain me how to work with the route Params (for example with how to get the spacename in to that link or whatever) or tell me how to register new routes from within a function? 所以你们能解释我如何使用Params路线(例如如何将空间名称输入到该链接或其他任何方式)或者告诉我如何从函数中注册新路由? Thanks! 谢谢!

Plunker Demo Plunker演示

You were right to try using params in your routes! 您尝试在路线中使用参数是正确的!

Router Config 路由器配置

Using @angular/router version 3.0.0-beta.2, setup the dynamically loaded spaces data to use a param like this: 使用@ angular / router version 3.0.0-beta.2,设置动态加载的空格数据以使用如下的参数:

let routes: RouterConfig = [
  {
    path: 'database/:spacename',
    component: SpaceComponent
  }
]

Data 数据

Setup SpaceService to return data in a format like this: 设置SpaceService以这样的格式返回数据:

let data = [
    {name: 'Space component #1', slug: 'space-1'},
    {name: 'Space component #2', slug: 'space-2'}
]

Routing Links 路由链接

In your navigation Html the links can be generated dynamically like this: 在您的导航Html中,链接可以像这样动态生成:

<li *ngFor="let space of spaces">
  <a [routerLink]="['/database', space.slug]">{{space.name}}</a>
</li>

Full app.ts 完整的应用app.ts

import {bootstrap} from '@angular/platform-browser-dynamic';
import {APP_ROUTER_PROVIDERS} from './app.routes';
import {ROUTER_DIRECTIVES} from '@angular/router';
import {Component, OnInit} from '@angular/core';
import {SpaceService} from './services/space.service.ts';

@Component({
  selector: 'my-app',
  template: `<h1> angular2 router @3.0.0-beta.2 example</h1>

  <h2 *ngIf="!spacesLoaded">Loading spaces...</h2>

  <ul>
    <li><a [routerLink]="['/item']">Item Component</a></li>
    <li *ngFor="let space of spaces">
      <a [routerLink]="['/database', space.slug]">{{space.name}}</a>
    </li>
  </ul>
  <br>
  <router-outlet></router-outlet>`,
  directives: [ROUTER_DIRECTIVES],
  providers: [SpaceService]
})
export class MyAppComponent implements OnInit {
  public spacesLoaded = false;
  public spaces = [];
  constructor (private spaceService: SpaceService) {
  }
  ngOnInit() {
    this.getSpaces();
  }
  getSpaces() {
    this.spaceService.getData().then(data => {
      this.spaces = data;
      this.spacesLoaded = true;
    });
  }
}
bootstrap(MyAppComponent, [
  APP_ROUTER_PROVIDERS,
]);

Full space.component.ts 完整的space.component.ts

import {Component} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
    selector: 'space',
    template: `This is space component <b>{{spaceslug}}</b>!`
})

export class SpaceComponent {
  public spacename;
  constructor (
    private router: Router,
    private activatedRoute: ActivatedRoute,) {
    this.router.events.subscribe(path => {
      this.spaceslug = this.activatedRoute.snapshot.url[1].path;
    });
  }
}

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

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