无法读取VUE3中未定义(阅读)的属性
我从著名的apipokemon接到这个电话: https://pokeapi.co.co/api/api/api/v2/pokemon?limit=151
一旦有了这些数据,我想通过另一种方法( _GetPokeMondata )将其传递给每个神奇宝贝的完整数据。
但是,当我创建此vuejs方法时,我会得到此错误:
uck offult(在Promise)typeError:无法读取未定义的属性(读取'_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);
},
I am getting this call from the famous 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.
However when I create this VueJS method I get this error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_getPokemonData')
Why can't I call the _getPokemonData method inside the forEach?
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);
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将功能切换为foreach以获取箭头功能。通过使用“函数”,您可以创建一个劫持“此”参考的新范围。
参见 this
switch the function in forEach for a arrow function. by using 'function' you create a new scope which hijacks the 'this' reference.
see this