首页 > 解决方案 > Angular Router 参数在同一条路线上

问题描述

路线

{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);

    });
}

我想要做的是,当我点击任何路由器链接时,参数将存储在一个变量中并转到我的帖子点击,但当我刷新我的页面时它正在工作,因为我正在使用 http 点击ngOnInit()

当我单击链接并获取数据时,我如何才能完成这项工作,并且有什么方法可以在单击路线时获取此数据并在同一路线上获取数据

标签: angularangular-ui-router

解决方案


这就是它的完成方式,它现在正在工作

 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);
        });
    });

推荐阅读