所以我正在構建我的第一個 vue 專案,我一直在嘗試將路由引數傳遞給 axios get 請求,但它不起作用。這是頁面的代碼,它在用戶單擊測驗表中的動態鏈接后呈現
<template>
<v-app>
<app-navbar />
<v-main>
<h3>test {{$route.params.name}}, {{$route.query.status}},{{$route.query.tag}}</h3>
<h3>{{items}}</h3>
</v-main>
</v-app>
</template>
<script>
import appNavbar from '../../../components/appNavbar.vue';
import axios from "axios";
export default {
components : {appNavbar},
name: "App",
data() {
return {
items: [],
};
},
async created() {
try {
const res = await axios.get(`http://localhost:3004/tests`,{ params: $route.params.name });
this.items = res.data;
} catch (error) {
console.log(error);
}
},
};
</script>
<style lang="scss" scoped>
</style>
如何將路由引數傳遞給 axios get 函式?
uj5u.com熱心網友回復:
這應該this.$route.params.name在腳本中。
const res = await axios.get(`http://localhost:3004/tests`,{ params: this.$route.params.name });
uj5u.com熱心網友回復:
Router.js const routes = [ { path: 'path/:name', name: 'Name', component: ComponentName, } ]
uj5u.com熱心網友回復:
我使用了@Nitheesh 解決方案,我只需要指定引數名稱,它就可以完美運行解決方案:
const res = await axios.get(`http://localhost:3004/tests`,{ params: {name:this.$route.params.name} });
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/444699.html
