所以我有GET|HEADapi: api/users/{user},它呼叫 function 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 中的這個鏈接上。
uj5u.com熱心網友回復:
您的代碼不起作用,因為您使用了簡單的引號。當您嘗試使用模板文字進行插值時,您必須將代碼更改為:
const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
this.users = response.data;
this.loading = false;
});
- 注意將 '' 更改為 ``
uj5u.com熱心網友回復:
我認為你應該使用 ` 而不是 ' 這樣就可以通過 {this.$route.params.id} ;
const helpVar = axios.get(`/api/users/{this.$route.params.id}`).then(response => {
this.users = response.data;
this.loading = false;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/338431.html
標籤:javascript 接口 Vue.js 公理
下一篇:插槽處于活動狀態時如何切換圖示
