我试图通过 routerLink 将 2 个参数传递给我的组件,然后传递给我的服务以获取数据。
Html 看起来像这样:-
<h3><a routerLink="['/repository', {{res.owner.login}}, {{res.name}} ]">{{res.owner.login}}</a></h3>
在我的组件中,我有以下内容:-
ngOnInit() {
this._route.params
.map(params => params['userName,repoName'])
.subscribe((params) => {
this.userName = params['userName']; this.repoName = params['repoName']
console.log('params = ' + params)
this._githubService.getRepo(params)
.subscribe(repository => {
this.repository = repository;
})
})
}
在我的服务中,我有以下代码:-
getRepo(params: any){
let headers = new Headers();
headers.append('Content-type', 'application/json');
return this._http.get("https://api.github.com/repos/"+ params.userName +"/" + params.repoName+ "/commits&sort=stars&order=desc&page=1", { headers: headers, withCredentials: false })
.map(this.extractRepo)
.catch(error => this.handleError(error, this.router));
}
路由器配置如下:-
import {ModuleWithProviders} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import { HomeComponent } from '../app/components/home/home.component';
import { RepositoryComponent } from
'../app/components/repository/repository.component';
const appRoutes: Routes = [
{path: '', component: HomeComponent, pathMatch: 'full'},
{path: 'repository', component: RepositoryComponent}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
但是,当我运行应用程序时,链接显示如下:-
Error: Cannot match any routes. URL Segment: '%5B'/repository'%2C%20tetris%2C%20react-tetris%20%5D'
如何格式化 routerLink 并获取要发送到我的服务的参数?
感谢您的帮助和时间!