簡體   English   中英

Vue.js,如何通過 axios 在 api 鏈接中發送對象?

[英]Vue.js, How to send object in api link via axios?

所以我有GET|HEAD api: api/users/{user} ,它調用函數App\\Http\\Controllers\\Api\\UserController@show Laravel 中的這個函數是:

   public function show($id){
    return new UserShowResource(User::findOrFail($id));
}

如何將變量id發送到我的函數。 我在某處找到了這個:

const helpVar = axios.get('/api/users/{this.$route.params.id}').then(response => {
           this.users = response.data;
           this.loading = false;
             
        });

但它不起作用。 我試過axios.get('/api/users/1')並且它返回給我id=1用戶,所以問題出在 axios 中的這個鏈接上。

您的代碼不起作用,因為您使用了簡單的引號。 當您嘗試使用模板文字進行插值時,您必須將代碼更改為:


const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
           this.users = response.data;
           this.loading = false;
             
        });
  • 注意將 '' 更改為 ``

我認為你應該使用 ` 而不是 ' 這樣就可以通過 {this.$route.params.id} ;


const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
           this.users = response.data;
           this.loading = false;
             
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM