将路线名称添加到数组

时间:2018-12-28 07:12:05

标签: vue.js vue-router

是否可以将路由添加到数组而不是使用<a href>作为字符串?

在此示例中,我创建了一个指向update-building的链接,并将buildingId添加为参数。

是否可以使用路由名称进行“更新构建”?

 axios
      .get("https://localhost:44349/api/Building/List", headers)
      .then(response => {
        response.data.forEach(el => {

          this.dataset.push([
              el.city, 
              el.street, 
              el.number, 
              el.projectName, 
             `<a href='#/update-building/${el.buildingId}'>update</a>`, // use route name instead this
             ]);
        });

我的路由器文件:

 {
      path: '/update-building:buildingId',
      name: 'UpdateBuilding',
      component: UpdateBuilding,
      props: true,
      params: null,
      meta: {
        requiresAuth: true
      }
    },

1 个答案:

答案 0 :(得分:0)

基于nemesv

是答案!

axios
      .get("https://localhost:44349/api/Building/List", headers)
      .then(response => {
        response.data.forEach(el => {

         var url = this.$router.resolve({ name: 'UpdateBuilding', params: { buildingId: el.buildingId }}).href
          this.dataset.push([
              el.city, 
              el.street, 
              el.number, 
              el.projectName, 
             `<a href='#/${url}/${el.buildingId}'>update</a>`, 
             ]);
    });