我正在使用 Vue3 開發我的專案,運行時出現此錯誤,這是我的整個代碼。有人可以幫我解決它。感謝你們
<script>
import axios from 'axios';
export default {
name: "RegisterView",
data() {
return {
user: {
username: "",
password: "",
email: "",
phoneNumber: "",
role: "",
},
role : []
};
},computed:{
getRole(){
axios.get('http://localhost:8080/api/role/get').then(res=>{
this.role = res.data;
})
return [];
}
},
methods: {
register() {
axios.post("http://localhost:8080/api/user/register", this.user).then((res) => {
console.log(res.data);
});
},
},
};
</script>
// Error Unexpected asynchronous action in "getRole" computed property vue/no-async-in-computed-properties
我嘗試了 async 和 await ,但似乎我弄錯了
uj5u.com熱心網友回復:
嘗試在created鉤子內運行該呼叫:
import axios from 'axios';
export default {
name: "RegisterView",
data() {
return {
user: {
username: "",
password: "",
email: "",
phoneNumber: "",
role: "",
},
role : []
};
},
created(){
this.getRole():
},
methods: {
getRole(){
axios.get('http://localhost:8080/api/role/get').then(res=>{
this.role = res.data;
}).catch(err=>{
this.role = []
})
},
register() {
axios.post("http://localhost:8080/api/user/register", this.user).then((res) => {
console.log(res.data);
});
},
},
};
uj5u.com熱心網友回復:
GetRole 使用 Promise,這意味著它沒有即時價值但有副作用,被 linter(代碼質量檢查器)認為是臟代碼
如果您需要異步計算,請改用 asyncComputed,它具有即時價值并自動更新承諾決議 https://github.com/foxbenjaminfox/vue-async-computed用于 Options API,@vueuse/core 用于 Composition API
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/523302.html
上一篇:如何使彈出視頻回應于移動設備
