路由器不在angular2-rc1中工作

时间:2016-05-26 10:31:46

标签: javascript typescript angular angular2-routing

我正在尝试在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[]'

我只想在点击链接时将DesignerComponent重定向到page_id作为参数。

任何输入?

2 个答案:

答案 0 :(得分:4)

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

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

直接在routerLink中使用page._id,不带对象:

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

<强>更新

从组件访问参数值:

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

答案 1 :(得分:1)

RouteDefinition没有name属性。所以,只需删除它:

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

视图将是:

<击> <div [routerLink]="['/designer',{page_id: page._id}]" ... >

<a [routerLink]="['/designer',{page_id: page._id}]"  ... >