Vue.js v-on处理程序中出现错误:“0”;TypeError:无法读取属性';术语';“未定义”的定义;从iTunesAPI获取时
当我在vue.js应用程序中搜索歌曲时,我正在尝试获取iTunesAPI 但是我得到了这个错误: [Vue warn]:v-on处理程序中出错:“TypeError:无法读取未定义的属性'term'” 和Vue.js v-on处理程序中出现错误:“0”;TypeError:无法读取属性';术语';“未定义”的定义;从iTunesAPI获取时,vue.js,fetch,undefined,Vue.js,Fetch,Undefined,当我在vue.js应用程序中搜索歌曲时,我正在尝试获取iTunesAPI 但是我得到了这个错误: [Vue warn]:v-on处理程序中出错:“TypeError:无法读取未定义的属性'term'” 和 TypeError: Cannot read property 'term' of undefined at VueComponent.search (App.vue?234e:38) at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:18
TypeError: Cannot read property 'term' of undefined
at VueComponent.search (App.vue?234e:38)
at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
at HTMLButtonElement.invoker (vue.runtime.esm.js?2b0e:2179)
at HTMLButtonElement.original._wrapper (vue.runtime.esm.js?2b0e:6917)
很明显我的结果没有定义?但我不知道我该怎么解决这个问题有人能帮忙吗?谢谢
<template>
<div id="app">
<input v-model="term" type="search">
<button @click="search">Search</button>
<p/>
<div v-for="result in results" :key="result.trackName" class="result">
<img :src="result.artworkUrl100">
<b>Track:</b> {{result.trackName}}<br/>
<br clear="left">
</div>
<div v-if="noResults">
Sorry, but no results were found. I blame Apple.
</div>
<div v-if="searching">
<i>Searching...</i>
</div>
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
data: () => ({
term: "",
results: [],
noResults: false,
searching: false
}),
methods: {
search: () => {
this.results = [];
this.searching = true;
fetch(
`https://itunes.apple.com/search?term=${encodeURIComponent(this.term)}&limit=10&media=music`)
.then(res => res.json())
.then(res => {
this.searching = false;
this.results = res.results;
this.noResults = this.results.length === 0;
});
}
}
}
</script>
搜寻
曲目:{{result.trackName}}
抱歉,未找到任何结果。我责备苹果。
搜索。。。
导出默认值{
名称:“应用程序”,
组成部分:{
},
数据:()=>({
术语:“,
结果:[],
结果:错,
搜索:false
}),
方法:{
搜索:()=>{
这个结果=[];
这是真的;
取回(
`https://itunes.apple.com/search?term=${encodeURIComponent(this.term)}&limit=10&media=music`)
.then(res=>res.json())
。然后(res=>{
这是错误的;
this.results=res.results;
this.noResults=this.results.length==0;
});
}
}
}
您的搜索方法使用的是箭头函数。如果要保留上下文并使用this
,请将搜索声明更改为search(){…}
,这是我找到的一个this@maiorano84这成功了谢谢你。。。现在我在获取时遇到了CORS问题哈哈:从源“”获取“”的访问已被CORS策略阻止:“访问控制允许源”标头的值“”不等于提供的源。让服务器发送带有有效值的头,或者,如果不透明响应满足您的需要,则将请求的模式设置为“no cors”,以获取禁用cors的资源。