vue-axios的使用以及axios請(qǐng)求賦值為空的問題
vue-axios的使用及axios請(qǐng)求賦值為空問題
注意:
如果沒有使用腳手架
需要命令導(dǎo)入axios
npm install axios
注意:
如果沒有axios智能代碼提示
vscode 引入插件 axios Snippets
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="userList">
<ul>
<li v-for="(item, index) in users" >
編號(hào):{{item.id}} 姓名:{{item.name}} 號(hào)碼:{{item.number}}
</li>
</ul>
</div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script src="./node_modules/axios/dist/axios.js"></script>
<script>
new Vue({
el:'#userList',
data: {
users:[]
},
//mounted 鉤子函數(shù),可在vue初始化之前加載
mounted() {
this.getUserList();
},
methods: {
getUserList(){
//axios 請(qǐng)求
axios.get('http://127.0.0.1:8081/userList', {
// params:{
// ID:123451
// }
})
//為什么請(qǐng)求要在這里處理呢?翻看我前面的文章ES6新特
.then(res => {
console.log(res.data)
this.users=res.data
})
.catch(err => {
console.error(err);
})
}
},
})
</script>
</html>注意:
.then 如果使用箭頭函數(shù)
我們可以通過this.users 把獲取到的值 賦予進(jìn)去,否則只能如下:
let vm = new vue({})
vm.users = res.data
只能這樣哈~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在Vue中使localStorage具有響應(yīng)式(思想實(shí)驗(yàn))
這篇文章主要介紹了如何在Vue中使localStorage具有響應(yīng)式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Vue使用JsonView進(jìn)行JSON數(shù)據(jù)展示
Vue-JSON-Viewer 是一個(gè)用于在Vue項(xiàng)目中展示JSON數(shù)據(jù)的組件,它解決了在項(xiàng)目開發(fā)中面臨的展示JSON數(shù)據(jù)的需求,下面就跟隨小編一起來了解下它的具體使用吧2025-03-03
Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定詳細(xì)講解
關(guān)于Vue中img動(dòng)態(tài)拼接src圖片地址的問題
Vue.JS項(xiàng)目中5個(gè)經(jīng)典Vuex插件

