猿问

如何使用 vue js 传递和检索 id?

我正在尝试将 id 从 1 vue 传递到另一个 vue 并获取我的 api 服务的数据。


我能够从第一个 vue (ClientListing.vue) 传递 id,但不知何故我的第二个 vue (Client.vue) 无法检索 id。


我的客户列表.vue



    <th scope="row" v-on:click="rowClicked(row)" style="cursor: pointer"> 

       <div class="media align-items-center" @click="showClient(row)">

          <a class="avatar rounded-circle mr-3">

              <img alt="Image placeholder" :src="row.customerTypeIcon">

          </a>

          <div class="media-body">

              <span class="name mb-0 text-sm">{{row.name}}</span>

          </div>

       </div>

    </th>


methods: {

   showClient(item) {

   router.push({ path: 'Clients/client', query: { id: item.id } });

   }

}

我的客户端.vue


mounted: function () {

   CifRepository

   .getCustomerDetails(this.$route.query)

   .then(response => {

      this.model = response.data.Results;

   }

)

.catch(error => {

   console.log(error)

   this.errored = true

})

.finally(() => this.loading = false);

}

getCustomerDetails(id) {

   return Repository.get("/api/Accounts/GetCustomerDetails");

}

我的 api 服务 - AccountsController


[HttpGet("GetCustomerDetails")]

public async Task<CustomerListingDto> GetCustomerDetails(long id)

{


   CustomerServiceModel customers = await

   _accountService.GetCustomerDetailsByCustomerId(id);


   return _mapper.Map<CustomerListingDto>(customers);

}

我的 AccountController/GetCustomerDetails 不断获取 0/null 值,并且无法从 ClientListing.vue 获取 id 传递


人到中年有点甜
浏览 122回答 1
1回答

吃鸡游戏

它应该this.$route.query.id代替this.$route.querymounted: function () {&nbsp; CifRepository&nbsp; .getCustomerDetails(this.$route.query.id)&nbsp; .then(response => {&nbsp; &nbsp; this.model = response.data.Results;&nbsp; })&nbsp; .catch(error => {&nbsp; &nbsp; console.log(error)&nbsp; &nbsp; this.errored = true&nbsp; })&nbsp; .finally(() => this.loading = false);}和getCustomerDetails(id) {&nbsp; &nbsp;return Repository.get("/api/Accounts/GetCustomerDetails/" + id);}我不确定 C# 部分,但可能是[HttpGet("GetCustomerDetails/${id}")]public async Task<CustomerListingDto> GetCustomerDetails(long id){&nbsp; &nbsp;CustomerServiceModel customers = await&nbsp; &nbsp;_accountService.GetCustomerDetailsByCustomerId(id);&nbsp; &nbsp;return _mapper.Map<CustomerListingDto>(customers);}
随时随地看视频慕课网APP
我要回答