代码之家  ›  专栏  ›  技术社区  ›  Bitwise

安装-vue后运行计算函数

  •  1
  • Bitwise  · 技术社区  · 6 年前

    我正在尝试运行一个函数,它需要一些从挂载方法中得到的数据。现在我试着用 computed 创建函数,但不幸的是,对于这种情况,之前计算的运行 mounted 所以我没有函数所需的数据。以下是我的工作内容:

        computed: {
          league_id () {
            return parseInt(this.$route.params.id)
          },
          current_user_has_team: function() {
            debugger;
          }
        },
        mounted () {
          const params = {};
    
          axios.get('/api/v1/leagues/' +this.$route.params.id, {
            params,
            headers: {
              Authorization: "Bearer "+localStorage.getItem('token')
            }
          }).then(response => {
            debugger;
            this.league = response.data.league
            this.current_user_teams = response.data.league
          }).catch(error => {
            this.$router.push('/not_found')
            this.$store.commit("FLASH_MESSAGE", {
              message: "League not found",
              show: true,
              styleClass: "error",
              timeOut: 4000
            })
          })
        }
    

    如你所见,我有 debugger 在被调用的计算函数中 current_user_has_team 功能。但我需要从AXIOS调用中得到的数据。现在调试器中没有数据。我应该使用什么回调来利用网络请求返回的数据?谢谢您!

    1 回复  |  直到 6 年前
        1
  •  3
  •   Decade Moon    6 年前

    如果您的计算属性 current_user_has_team 取决于AXIOS调用后才可用的数据,然后您需要:

    1. 当前用户有团队 属性,如果数据不可用,则返回合理的默认值。
    2. 禁止访问 当前用户有团队 从模板(限制为 v-if )或者其他任何地方,直到AXIOS调用完成并且数据可用。

    这取决于您希望组件在“加载”情况下如何工作。