我正在使用 Vue 3、Composition API 和 Pinia 构建 Pokemon 过滤搜索应用程序。我正在尝试设置应用程序,以便将从 Pokemon API 获取的响应传递到 fetchPokemon() 函数内的商店(使用 Pinia 设置)。
const fetchPokemon = () => {
axios.get("https://pokeapi.co/api/v2/pokemon?offset=0")
.then((response) => {
store.addPokemon(response.data.results)
})
}
Run Code Online (Sandbox Code Playgroud)
将响应传递到商店后, updatePokemon() 函数使用 filter 和 include 方法过滤出商店中的 Pokemon 并将其与用户输入文本字段(“state.text”)中的 Pokemon 进行匹配:
const updatePokemon = () => {
if(!state.text) {
return []
}
return store.getState.pokemons.filter((pokemon) =>
pokemon.name.includes(state.text)
)
}
Run Code Online (Sandbox Code Playgroud)
执行应用程序时,我在 updatePokemon() 函数中收到以下错误:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'includes')
Run Code Online (Sandbox Code Playgroud)
我假设这意味着用于搜索/过滤的 .includes() 方法不能用于此搜索。我应该如何处理过滤器并包含将商店中的神奇宝贝与用户输入的神奇宝贝进行匹配的方法?
这是代码:
皮尼亚店
import { defineStore } …Run Code Online (Sandbox Code Playgroud)