vue中的Ajax之axios
首先是项目目录
图片.png
这里使用的接口是第三方的接口
http://api.komavideo.com/news/list
(当然也可以自己使用服务器来弄一个)
body是接口里面的数据,通过body.data将
数据传给context,然后呈现出来
mian.js
/* eslint-disable no-new */
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
new Vue({
el: '#app',
// eslint-disable-next-line no-undef
router,
// eslint-disable-next-line no-undef
components: { App },
template: '<App/>'
})
HelloWorld.vue
<template>
<div class="hello">
<pre> {{ context }} </pre>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data() {
return {
context:" "
};
},
mounted() {
this.axios.post("http://api.komavideo.com/news/list").then(body =>{
this.context =body.data;
});
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
网友评论