首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计算属性vue/no异步计算属性Vue3中的意外异步操作

计算属性vue/no异步计算属性Vue3中的意外异步操作
EN

Stack Overflow用户
提问于 2022-10-31 08:01:53
回答 2查看 68关注 0票数 0

我正在用Vue3开发我的项目,我在运行时得到了这个错误,这是我的全部代码。有人能帮我修好吗。谢谢你们

代码语言:javascript
运行
复制
<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>
代码语言:javascript
运行
复制
// Error  Unexpected asynchronous action in "getRole" computed property  vue/no-async-in-computed-properties

我尝试过异步等待,但似乎我弄错了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-31 08:36:03

尝试在created钩子中运行该调用:

代码语言:javascript
运行
复制
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);
      });
    },
  },
};
票数 1
EN

Stack Overflow用户

发布于 2022-10-31 08:22:12

GetRole使用承诺,这意味着它没有即时的价值,但是有副作用,这被linter (代码质量检查器)认为是脏代码。

如果您需要异步计算,则使用asyncComputed,它具有即时值,并且自动更新选项API的https://github.com/foxbenjaminfox/vue-async-computed承诺分辨率,@vueuse/core用于组合API。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74260283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档