fetch是w3c的一種請求標准,不是第三方庫,與vue、jquery無關,且多數瀏覽器已內置
使用:
本地准備一份json文件作為數據源
請求,拿到的不是我們想要的數據
想要獲取響應數據,需在第一個then中將響應數據轉為json再返回給第二個then,在第二個then中去獲取值
也可以簡寫return
在場景中的應用
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<meta http-equiv='X-UA-Compatible' content='ie=edge'>
<script src='./vue.js'></script>
<title></title>
</head>
<body>
<div id='app'>
<button @click="handleClick()">點擊獲取影片信息</button>
<ul>
<li v-for=" data in datalist">
<h3>{{ data.name }}</h3>
<img :src="data.poster">
</li>
</ul>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
datalist: []
},
methods: {
handleClick() {
// 點擊觸發請求,獲取影片
fetch('./test.json').then(res => res.json()).then(res => {
console.log(res.data.films)
this.datalist = res.data.films
})
}
}
})
// fetch('./test.json').then(res => {
// return res.json()
// }).then(res=>{
// console.log(res)
// })
// fetch('./test.json').then(res => res.json()).then(res => {
// console.log(res)
// })
</script>
</body>
</html>