我正在尝试将对象传递给另一个组件。
请求-list.html
<tr [routerLink]="['Request', { id: request.id }]" *ngFor="let request of requests" [req]="request">
<td class=" ">{{ request.datum }}</td>
<td class=" ">{{ request.artist.name }} </td>
<td class=" ">{{ request.artist.user.first_name }}
<td class=" ">{{ request.user.first_name }}</td>
<td class=" ">{{ request.duration.price }}</td>
<td class=" ">{{ request.created_at }}</td>
<td class=" ">{{ request.rejected_at }}</td>
</tr>
RequestListComponent
import {Component} from '@angular/core';
import {Service} from './../services/service.service';
import {Request} from './../classes/request';
import {RouteConfig, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {RequestComponent} from './request.component';
@Component({
directives: [ROUTER_DIRECTIVES, RequestComponent],
templateUrl: 'partials/request-list.html'
})
export class RequestListComponent {
requests: Request[];
constructor(private _Service: Service) {}
ngOnInit(){
this._Service.getRequests().subscribe(data => this.requests = data);
}
}
RequestComponent
import {Component, Input} from '@angular/core';
import {Service} from './../services/service.service';
import {Request} from './../classes/request';
@Component({
templateUrl: 'partials/request.html'
})
export class RequestComponent {
@Input() req: Request;
constructor(private _Service: Service) {}
}
但是我收到错误:“无法绑定到'req',因为它不是已知的本机属性”
我做错了什么?
答案 0 :(得分:3)
正如我在评论中提到的,如果@Input()
加载了RequestComponent
,则无法使用Router
。很遗憾,无法通过RouteData
- 指令传递RouteLink
。
要解决此问题,我会从服务Request
中加载RequestComponent
,您用来加载Requests
列表。要减少后端的流量,如果有的话,可以构建一些缓存机制。
export class RequestComponent {
req: Request;
constructor(private _Service: Service, routeParams : RouteParams) {
this.req = _Service.getRequestById(routeParams.get('id'));
}
}