如何在路由解析器服务中获取Id的值,因为使用动态的invoiceId调用singleInvoice方法,并且在URL中,例如invoice / 4,在这种情况下,解析器服务中应使用4。
{
path: 'invoices',
component: InvoiceComponent,
resolve: {
invoiceDetails: InvoiceResolverService
},
data: {
title: ' Invoices'
}
},
{
path: 'invoice/:id',
component: InvoiceDetailComponent,
resolve: {
invoiceDetails: SingleInvoiceResolverService
},
data: {
title: 'Invoice Detail'
}
}
解析器
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { AuthService } from '../auth.service';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class SingleInvoiceResolverService implements Resolve<any> {
constructor(private auth: AuthService, private http: HttpClient) { }
resolve(route: ActivatedRouteSnapshot) {
return this.auth.singleInvoice(invoiceId);
}
}
答案 0 :(得分:1)
route.params['id']
在resolve方法中,您可以获得ID。
答案 1 :(得分:0)
这样,您将获得一个包含参数的数组
constructor(
private route: ActivatedRoute,
) {
this.route.params.subscribe(
(params) => {
console.log(params);
}
}