简体   繁体   English

无法读取 Vue3 中未定义(正在读取)的属性

[英]Cannot read properties of undefined (reading) in Vue3

I am getting this call from the famous ApiPokemon: https://pokeapi.co/api/v2/pokemon?limit=151我从著名的 ApiPokemon 接到这个电话: https ://pokeapi.co/api/v2/pokemon?limit=151

Once I have that data I want to pass it through another method ( _getPokemonData ) to map the complete data for each Pokémon.一旦我有了这些数据,我想通过另一个方法( _getPokemonData )将它传递给每个神奇宝贝的完整数据。

However when I create this VueJS method I get this error:但是,当我创建此 VueJS 方法时,我收到此错误:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_getPokemonData') Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_getPokemonData')

Why can't I call the _getPokemonData method inside the forEach?为什么我不能在 forEach 中调用 _getPokemonData 方法? This is my code:这是我的代码:

mounted() {
    this.$http
      .get("https://pokeapi.co/api/v2/pokemon?limit=151")
      .then((response) => {
        //this.pokemons = response.data.results;
        this.pokemons = response.data.results.forEach(function(pokemon){
          this._getPokemonData(pokemon); 
        });
      });

    this.$http
      .get("https://pokeapi.co/api/v2/type/")
      .then((response) => {
        this.types = response.data.results;
      });
  },

  methods: {
    _getPokemonData(pokemon) {
      console.log(pokemon);
    },

switch the function in forEach for a arrow function.将 forEach 中的函数切换为箭头函数。 by using 'function' you create a new scope which hijacks the 'this' reference.通过使用“函数”,您可以创建一个劫持“this”引用的新范围。

see this看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“发出”)VueQuill Vue3 - Uncaught TypeError: Cannot read properties of undefined (reading 'emit') VueQuill Vue3 TypeError:无法在渲染 function 中读取未定义的属性(读取“iconType”)-vue 3 - TypeError: Cannot read properties of undefined (reading 'iconType') in render function - vue 3 未捕获的类型错误:无法读取未定义的属性(读取“mixin”)Vue 3 - Uncaught TypeError: Cannot read properties of undefined (reading 'mixin') Vue 3 Vue CLI - TypeError:无法读取未定义的属性(读取“1”) - Vue CLI - TypeError: Cannot read properties of undefined (reading '1') 如何修复 typeError: cannot read properties of undefined (reading 'upload') in vue - how to fix typeError: cannot read properties of undefined (reading 'upload') in vue 类型错误:无法读取未定义的属性(读取“输入”),Vue - TypeError: Cannot read properties of undefined (reading 'input'), Vue Vue.js:“TypeError:无法读取未定义的属性(读取'$refs)'” - Vue.js: "TypeError: Cannot read properties of undefined (reading '$refs)'" 无法读取未定义的属性(读取 *) - Cannot read properties of undefined (reading *) 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM