当前需要使用的组件:
import data from './test.json' export default{ data(){ return{ userinform: '' } }, mounted(){ this.userinform = data } }
test.json就是普通json格式就可以了!然后完美解决!
后台服务器请求json方式:
this.$http.get('url').then(response => { //succces callback var data = response.body; }, response => { // error callback alert('连接失败!') });
附:VUE解析json展示列表页示例
<div> <div> <ol> <li v-for="site in sites"> {{ site.name }} </li> </ol> </div> </div> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> <script> function getJson(url,func){ $.ajax({ type:'get', url:url, dataType: 'json', success: function(data){ if (data.code==0){ var a = data.data.list;func(a); }else{ alert("接口调用失败"); } }, error: function(data){ alert(JSON.stringify(data)); } }); } $(function(){ var url = "http://www.test.com/index.php?s=https://www.jb51.net/shop/list/"; getJson(url,pushDom); }); function pushDom(data){ var vm = new Vue({ el: '#app', data: { sites: data } })} </script>