angular2 ActivatedRoute显示无法解析Detail_retailer的所有参数

时间:2018-01-09 11:08:58

标签: angular typescript routing

我正在使用角度js 2.0.0我正在通过

传递价值
import {Component} from "angular2/core"
import {bootstrap} from 'angular2/platform/browser';
import {Users} from "../users/component"
import {Users_detail} from "../users_detail/component"

import {
    RouteConfig,RouterModule,
    ROUTER_DIRECTIVES,
} from 'angular2/router';

@RouteConfig([
    {path:'/users', name: 'Users', component: Users},
    {path:'/users_detail/:id', name: 'Users_detail', component: User_retailer},
])

如何在'Users_detail'中获取'id'值 users_detail.ts

import { Router,ActivatedRoute } from 'angular2/router';

constructor( private route: ActivatedRoute) {


}

显示错误

“无法解析User_retailer(?)的所有参数。请确保它们都具有有效的类型或注释。”

1 个答案:

答案 0 :(得分:0)

您导入的组件名称为Users_detail

import {Users_detail} from "../users_detail/component"

但在RouteConfig中,您已component: User_retailer

所以,RouteConfig必须是:

@RouteConfig([
    {path:'/users', name: 'Users', component: Users},
    {path:'/users_detail/:id', name: 'Users_detail', component: Users_detail},
    ])

你可以这样得到id

let id = this.route.snapshot.params['id']

希望这会有所帮助。