≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

Vue2.0 http请求以及loading展示实例

人气:504 时间:2019-04-08

这篇文章主要为大家详细介绍了Vue2.0 http请求以及loading展示实例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

我们需要额外两个依赖vuex 和 axios:(还是接着上一个项目MyFirstProject写)

代码如下:


npm i vuex axios -D 

 

首先简单的阐述下http请求

 

 

1、main.js 中引入axios

 

代码如下:


import axios from 'axios' 
Vue.prototype.$http = axios; 

 

2、focus.vue中写个函数获取数据

 

代码如下:


<template>
	<div id="focus">
		<ul >
			<li v-for="(item,index) in focusList">
				<div class="fportraits">
					<img :src="'./src/'+item.portrait" :alt="item.name">
				</div>
				<div class="details">
					<div class="ftitle"><strong> {{ item.name }} </strong></div>
					<p> {{ item.production }} </p>
				</div>
				<div class="isfocused">
					<p>取消关注</p>
				</div>
				<div class="clearfix"></div>
			</li>
		</ul>
	</div>
</template>
<script>
	export default{
		data(){
			return {
				focusList:[] //存储请求返回的数据
			}
		},
		mounted(){
			this.getFocusList()
		},
		methods:{
			getFocusList(){   //http get请求data.json 的数据
				var vm = this;
				this.$http.get('src/assets/data/data.json')
					.then(function(res){
						vm.focusList = res.data;
					})
					.catch(function(err){
						console.log(err)
					})
			}
		}
	}
</script>
<style scoped>
#focus{text-align:left;}
#focus ul{margin:0 auto;width:50rem;border-bottom:none;}
#focus p{margin:0;}
#focus li{width:46rem;display:block;border-bottom:1px solid #ddd;padding:0.5rem 2rem;cursor:default;}
#focus img{height:4rem;margin-left:-1rem;}
.fportraits{float:left;width:4rem;height:4rem;border-radius:50%;overflow:hidden;}
.details{float:left;margin-left:1rem;}
.isfocused{float:right;font-size:0.8rem;height:0.8rem;line-height:0.8rem;margin:0;}
.clearfix{clear:both;}
</style>

 

获取成功后展示效果如图:

 

【q1010.com温馨提示:图片暂缺】

我的两个男神羡慕羡慕有没有很帅

到此请求数据就结束了,是不是很简单,然额接下来涉及到store就有点复杂了,欲知后事如何,且听下回分解~

本文来自:http://www.q1010.com/184/4422-0.html

注:关于Vue2.0 http请求以及loading展示实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue.js中$set与数组更新方法
  • 解决vue处理axios post请求传参的问题
  • 基于vue 添加axios组件,解决post传参数为null的问题
  • vue中本地静态图片路径写法
  • vue2.0 computed 计算list循环后累加值的实例
  • vue.js 嵌套循环、if判断、动态删除的实例
  • axios发送post请求springMVC接收不到参数的解决方法
  • vue axios 在页面切换时中断请求方法 ajax
  • 写一个移动端惯性滑动&回弹Vue导航栏组件 ly-tab
  • 浅析Vue中method与computed的区别
上一篇:vue2.0 路由不显示router-view的解决方法
下一篇:浅析Vue中method与computed的区别
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。