我正在用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我尝试过异步等待,但似乎我弄错了。
发布于 2022-10-31 08:36:03
尝试在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);
});
},
},
};发布于 2022-10-31 08:22:12
GetRole使用承诺,这意味着它没有即时的价值,但是有副作用,这被linter (代码质量检查器)认为是脏代码。
如果您需要异步计算,则使用asyncComputed,它具有即时值,并且自动更新选项API的https://github.com/foxbenjaminfox/vue-async-computed承诺分辨率,@vueuse/core用于组合API。
https://stackoverflow.com/questions/74260283
复制相似问题