利用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')

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐