axios请求本地json数据

  • Post author:
  • Post category:其他


利用axios请求获取本地json数据

运行项目导入axios框架 利用npm




npm install axios –save


App.vue
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <div class="teacherlist">
		<button @click="showtab = ! showtab">{{showtab ? "Hide" : "Show" }}</button>
		<table border="1" cellspacing="0" cellpadding="0" v-if="showtab">
			<tr>
				<th>索引</th>
				<th>姓名</th>
				<th>E-mail</th>	
				<th>电话</th>
			</tr>
			<tr v-for="(info,index) in infos ">
				<td>{{index}}</td>				
				<td>{{info.name}}</td>				
				<td>{{info.email}}</td>				
				<td>{{info.phone}}</td>
			</tr>
		</table>
	</div>
  </div>
</template>

<script>
export default {
  name: 'App',
  components: {},
  data(){
	  return{
		  infos:[],
		  index:'',
		  showtab:false
	  }
  },
  mounted(){	 
	   this.$axios
	   .get('/db.json')
	   .then(response => (
	       this.infos = response.data,
		   console.log(this.infos)
	  ))
	  .catch(function(error){
		  console.log(error)
	  })
	  	  
  }
}
</script>

<style>
#app {			
		text-align: center;
		color: #2c3e50;
		margin-top: 60px;
	}
	table{
		margin: auto;	
	}
	.teacherlist{
		width: 70%;
		margin: 0 auto;
		padding: 20px;
		border:soild 1px gray ;
	}
	.teacherlist td,th{
		padding: 10px;
	}

</style>
main.js
import Vue from 'vue'
import App from './App.vue'
import axios from "axios"
Vue.prototype.$axios=axios ;
Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')



版权声明:本文为m0_65048141原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。