Angular 从paramMap获取参数时出现问题
因此,我尝试用paramMap在URL上捕捉ID,但它总是返回null。任何人都能找到问题所在吗? 组件技术Angular 从paramMap获取参数时出现问题,angular,typescript,Angular,Typescript,因此,我尝试用paramMap在URL上捕捉ID,但它总是返回null。任何人都能找到问题所在吗? 组件技术 import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { Senators
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { SenatorsService } from '../senators.service';
@Component({
selector: 'app-senator-expenses',
templateUrl: './senator-expenses.component.html',
styleUrls: ['./senator-expenses.component.css']
})
export class SenatorExpensesComponent implements OnInit {
senator: Senator[] = []
id: number;
constructor(private SenatorsService: SenatorsService, private route: ActivatedRoute, private a: Router) { }
ngOnInit(): void {
this.route.paramMap.subscribe(paramMap => {
this.id = parseInt(paramMap.get('id'));
console.log(this.id)
})
}
}
路由文件
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SenatorExpensesComponent } from './senator-expenses/senator-expenses.component';
import { SenatorsListComponent } from './senators-list/senators-list.component';
const routes: Routes = [
{ path: 'senadores', component: SenatorsListComponent },
{path: '', redirectTo: 'senadores', pathMatch : 'full'},
{path: 'senadores/:id ', component: SenatorExpensesComponent} //This is the route i tryng to get the id
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
component.html
<p>
<mat-toolbar>
<span>Senators list</span>
</mat-toolbar>
</p>
<mat-nav-list>
<mat-list-item *ngFor="let senator of senators">
<a matLine [routerLink]="['/senadores', senator.id]">{{senator.nomeSenador}}</a>
</mat-list-item>
</mat-nav-list>
参议员名单
{{参议员诺梅塞纳多}
我尝试使用太多的“this.id=this.route.params”,但不起作用
this.id = parseInt(this.route.snapshot.params.id);
你能试试这个吗
你能试试这个吗?很抱歉耽误了时间,伙计们,实际上问题出在这一行:
{path: 'senadores/:id ', component: SenatorExpensesComponent}
正如您看到的,id和引号之间有一个空格,这就是我搜索的问题
this.id=parseInt(paramMap.get('id'))
而不是this.id=parseInt(paramMap.get('id')代码>但无论如何还是要感谢那些试图帮助我的人。很抱歉耽误了时间,伙计们,实际上问题出在下面:
{path: 'senadores/:id ', component: SenatorExpensesComponent}
正如您看到的,id和引号之间有一个空格,这就是我搜索的问题this.id=parseInt(paramMap.get('id'))
而不是this.id=parseInt(paramMap.get('id')代码>但无论如何还是要感谢那些试图帮助我的人。您如何从SenatorListComponent导航到SenatorexPenseComponent?你能在你做路线部分的地方添加代码吗?>我把代码放在上面。你能在详细信息页面使用不同的路线吗。例如{path:'senadores detail/:id',component:SenatorExpensesComponent}。到处都有同样的变化。。。。我不认为代码有任何问题。尝试但返回sameCan您可以创建一个基本的stackblitz复制您的问题吗?您如何从SenatorListComponent导航到SenatorexPenseComponent?你能在你做路线部分的地方添加代码吗?>我把代码放在上面。你能在详细信息页面使用不同的路线吗。例如{path:'senadores detail/:id',component:SenatorExpensesComponent}。到处都有同样的变化。。。。我不认为代码有任何问题。尝试但返回相同的值。你可以创建一个基本的stackblitz复制你的问题吗?实际上,parseInt没有太大区别,而不是返回“null”返回“NaN”,但我尝试了你的代码并返回null。实际上,parseInt没有太大区别,而不是返回“null”返回“NaN”但是我尝试了你的代码并返回null。