我正在尝试在angular2中使用路由器。我已经使用一个链接重定向到DesignerComponent并将page._id作为RouteParams传递。
该代码在angular2-beta中正常工作。
下面是我的代码:

pages.component.ts

import { Component, OnInit ,Input} from '@angular/core';
import {SearchPipe} from './../../shared/pipes/search-pipe.pipe';
import { GlobalObjectsService} from './../../shared/services/global/global-objects.service';
import { Router,Routes , ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from '@angular/router';
import { DesignerComponent } from './../../+designer/designer.component';

@Component({
  moduleId: module.id,
  selector: 'app-pages',
  pipes: [SearchPipe],
  directives: [ROUTER_DIRECTIVES],
  providers: [ROUTER_PROVIDERS],
  templateUrl: 'pages.component.html',
  styleUrls: ['pages.component.css']
})
@Routes([
  {path: '/designer', component: DesignerComponent}
])
export class PagesComponent implements OnInit {

  @Input() pages:any;
  constructor(private router:Router) {

  }
  ngOnInit() { }
  selectPage(page_id:string){
  }

}


在我看来,我已经完成了以下工作:

<div [routerLink]="['Designer',{page_id: page._id}]" class="btn btn-xs btn-success" title="Design Page">
    <i class="fa fa-eye fa-fw"></i>
</div>


但它向我显示以下错误:

Argument of type '{ path: string; name: string; component: typeof DesignerComponent; }[]' is not assignable to parameter of type 'RouteMetadata[]'


我只想单击带有page_id作为参数的链接时重定向到DesignerComponent

有输入吗?

最佳答案

在路由元数据中添加参数:

@Routes([
  {path: '/designer/:page_id', component: DesignerComponent}
])


在没有对象的情况下,直接在routerLink中使用page._id:

[routerLink]="['/designer',page._id]"


更新

从组件访问参数值:

constructor(curr: RouteSegment) {
        let itemID = curr.getParam("page_id");
    }

09-17 06:40