【问题标题】:Cannot access variable in function, but I can get it outside无法访问函数中的变量,但我可以在外部获取它
【发布时间】:2022-10-25 17:06:29
【问题描述】:

我有一个变量

setup: {
  const items = ref<basicPokemon[]>([]);
}

但我似乎无法在我的功能中访问它。但是,我可以在“方法”部分而不是函数内部访问它。我究竟做错了什么?

methods: {

this.items //this works

 let response =  axios({
              url: 'https://stoplight.io/mocks/appwise-be/pokemon/57519009/pokemon',
              method: 'get',
              timeout: 8000,
              headers: {
                  'Content-Type': 'application/json',
              }})
              .then(data => data.data.forEach(function(pok: basicPokemon){
                  try{
                    items.value.push(pok); //here I cannot access the variable
                
                  }
                  catch(err){
                    //todo error handling
                        console.log(err)
                  }
                }))
}

【问题讨论】:

    标签: javascript typescript vuejs3


    【解决方案1】:

    您是否也尝试过在您的函数中引用this

    try{
        this.items.value.push(pok); //here I cannot access the variable
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2015-11-09
      相关资源
      最近更新 更多