Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 无法在全局变量中获取响应_Vue.js_Vuejs2_Axios - Fatal编程技术网

Vue.js 无法在全局变量中获取响应

Vue.js 无法在全局变量中获取响应,vue.js,vuejs2,axios,Vue.js,Vuejs2,Axios,您好,我是vuejs和axios的新手。我想获取我的axios并将响应存储在变量中,然后将该变量设置为全局变量,以便在代码之外访问它。然而,每当我输入console.log时,它总是给我未定义的信息。有人能帮我吗?多谢各位 代码如下: async created() { let result; this.$store .dispatch('getAllProduct') .then((responseOne) => { // con

您好,我是vuejs和axios的新手。我想获取我的axios并将响应存储在变量中,然后将该变量设置为全局变量,以便在代码之外访问它。然而,每当我输入console.log时,它总是给我未定义的信息。有人能帮我吗?多谢各位

代码如下:

async created() {
    let result;

    this.$store
      .dispatch('getAllProduct')
      .then((responseOne) => {
        // console.log("PRODUCT", responseOne.data.data)
        result = responseOne.data.data
      })
      .catch((error) => {
        console.log('ERROR USER RESPONSE ', error)
      })

    console.log('t', result)
  },

由于您已将
async
添加到创建中,因此您应该
等待响应:

async created() {
    let result;
 try {
 const responseOne = await   this.$store.dispatch('getAllProduct')    
   result = responseOne.data.data
   }
   catch(error) {
        console.log('ERROR USER RESPONSE ', error)
   }
    console.log('t', result)
  },
如果不想使用
async/await
,则应在数据选项中定义
结果
,并在
中更新,然后
回调:

data(){
  return{
    result:null
 }
},
watch:{
   result(newVal){ // watch the result property
     console.log(newVal)
  }
},
created() {
   

    this.$store
      .dispatch('getAllProduct')
      .then((responseOne) => {
        // console.log("PRODUCT", responseOne.data.data)
        this.result = responseOne.data.data
      })
      .catch((error) => {
        console.log('ERROR USER RESPONSE ', error)
      })

  },

由于您已将
async
添加到创建中,因此您应该
等待响应:

async created() {
    let result;
 try {
 const responseOne = await   this.$store.dispatch('getAllProduct')    
   result = responseOne.data.data
   }
   catch(error) {
        console.log('ERROR USER RESPONSE ', error)
   }
    console.log('t', result)
  },
如果不想使用
async/await
,则应在数据选项中定义
结果
,并在
中更新,然后
回调:

data(){
  return{
    result:null
 }
},
watch:{
   result(newVal){ // watch the result property
     console.log(newVal)
  }
},
created() {
   

    this.$store
      .dispatch('getAllProduct')
      .then((responseOne) => {
        // console.log("PRODUCT", responseOne.data.data)
        this.result = responseOne.data.data
      })
      .catch((error) => {
        console.log('ERROR USER RESPONSE ', error)
      })

  },

您需要在代码中添加
wait

async created(){
让结果;
试一试{
让output=等待此操作。$store.dispatch('getAllProduct'))
结果=output.data.data
}
捕获(错误){
console.log('ERROR USER RESPONSE',ERROR)
}
console.log('t',result)

},
您需要在代码中添加
wait

async created(){
让结果;
试一试{
让output=等待此操作。$store.dispatch('getAllProduct'))
结果=output.data.data
}
捕获(错误){
console.log('ERROR USER RESPONSE',ERROR)
}
console.log('t',result)

},
请同时发布相关的门店操作。至少提供一个最少且可重复的示例。请同时发布相关的门店操作。至少提供一个最小且可重复的示例。不客气,之所以出现这种行为是因为
console.log('t',result)
之前执行,然后执行
回调asynchronously@TechDev是吗?嗯,奇怪的事。因为除了linting错误(这无论如何都不会影响工作流)之外,您还需要在
async
函数中等待
stuff。我想知道这是什么原因。不客气,您之所以会有这种行为,是因为
console.log('t',result)
之前执行,然后执行
回调asynchronously@TechDev是吗?嗯,奇怪的事。因为除了linting错误(这无论如何都不会影响工作流)之外,您还需要在
async
函数中等待
stuff。我不知道这是什么原因。