路线
{path:'dashboard',component: DashboardComponent },
{path:'dashboard/:id',component: DashboardComponent },
路由器链接
<li><a [routerLink]="['/dashboard', 'kRX4eLiwmEau8X2SdoKScA==']">Appliances</a></li>
<li><a [routerLink]="['/dashboard', 'FQtZRfDqtkGrn2II8HobZw==']">Tools and Gadgets</a></li>
<li><a [routerLink]="['/dashboard', 'EMz9RMY4RESyKtvFVAJTVQ==']">Table Linen</a></li>
<li><a [routerLink]="['/dashboard', 'BamlddxbUk2lx3uhaT4Hbg==']">Bakeware</a></li>
<li><a [routerLink]="['/dashboard', 'VZxPmhcsxEmOGLTvp5Mvxw==']">Serveware</a></li>
<li><a [routerLink]="['/dashboard', 'QEkCu3nN5UWYBHmCOuvGUA==']">Serveware</a></li>
<li><a [routerLink]="['/dashboard', 'DKHH6dljMkWazcakJSxC1g==']">Decor</a></li>
代码
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = params['id']; // (+) converts string 'id' to a number
// In a real app: dispatch action to load the details here.
});
console.log( this.id )
let postdata = {
id :this.id
}
axios({
method: 'post',
data: this.serializeObj(postdata),
url: this.initialapi+'/Product/GetProducts',
headers: {
'Content-Type':'application/x-www-form-urlencoded',
}
}).then(response => {
console.log(_.uniqBy(response.data,'id'))
this.productlist = _.uniqBy(response.data,'id')
})
.catch(function (error) {
console.log(error);
});
}
我想要做的是,当我点击任何路由器链接然后params将存储在一个变量并转到我的帖子命中,它正在工作,虽然当我刷新我的页面因为我使用http命中ngOnInit()
。
当我点击一个链接并且它获取数据时我怎么能得到这个工作?有什么方法我可以在点击路线时获得这些数据并在同一路线上获取数据
答案 0 :(得分:0)
这就是它如何完成它现在正在运作
this.route.paramMap.subscribe(params => {
console.log(params.get('id'));
this.id = params.get('id');
// data hit
let postdata = {
id :this.id
}
axios({
method: 'post',
data: this.serializeObj(postdata),
url: this.initialapi+'/Product/GetProducts',
headers: {
'Content-Type':'application/x-www-form-urlencoded',
}
}).then(response => {
console.log(_.uniqBy(response.data,'id'))
this.productlist = _.uniqBy(response.data,'id')
})
.catch(function (error) {
console.log(error);
});
});