繁体   English   中英

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

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

我从著名的 ApiPokemon 接到这个电话: https ://pokeapi.co/api/v2/pokemon?limit=151

一旦我有了这些数据,我想通过另一个方法( _getPokemonData )将它传递给每个神奇宝贝的完整数据。

但是,当我创建此 VueJS 方法时,我收到此错误:

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

为什么我不能在 forEach 中调用 _getPokemonData 方法? 这是我的代码:

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);
    },

将 forEach 中的函数切换为箭头函数。 通过使用“函数”,您可以创建一个劫持“this”引用的新范围。

看到这个

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM