vue2.0 仿手机新闻站(四)axios
1.axios的配置
main.js
import Vue from 'vue'
import App from './App.vue'
// 引入 路由
import VueRouter from 'vue-router'
// 引入 路由配置文件
import routes from './router.config'
// 引入 vuex入口文件
import store from './store/index'
// 引入 axios
import axios from 'axios'
// 引入 loading 组件
import Loading from './components/loading' Vue.use(VueRouter);
Vue.use(Loading); // 关于axios配置
axios.interceptors.request.use(function(config){
// 发送请求
store.dispatch('showLoading');
return config;
},function(error){
return Promise.reject(error);
}); axios.interceptors.response.use(function(response){
// 请求回来
store.dispatch('hideLoading');
return response;
},function(error){
return Promise.reject(error);
}); // 配置请求的根路径
// axios.default.baseURL = 'http://localhost:8080'; // 设置默认头部信息 post
// axios.default.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; // 把axios对象挂到Vue原型上
Vue.prototype.$http = axios; // 创建 路由
const router = new VueRouter({
mode:'history', // 删除 url 中的'#'号,切换路径模式
scrollBehavior:() => ({y:0}), // 滚动条滚动的行为,不加这个默认就会记忆原来滚动条的位置
routes // routes 等价于 routes:routes
}); require('./assets/css/base.css'); // 全局引入 new Vue({
el: '#app',
router,
store,
render: h => h(App)
})
2. 组件中的使用
Home.vue
<!-- 首页 -->
<template>
<div id="home">
<div class="content mt30">
<div class="newsList">
<ul>
<li v-for="(item,index) in arrList">
<a href="conText.html">
<h2>{{index}}.{{item.title}}</h2>
<p>{{item.detail}}</p>
</a>
</li>
</ul>
</div>
</div>
</div>
</template> <script>
export default{
data(){
return {
arrList:[]
}
},
mounted(){
// 获取数据
this.fetchData();
},
methods:{
fetchData(){
var _this = this;
// this 为 vue的实例
this.$http.get('src/data/index.data').then(function(res){
_this.arrList = res.data;
}).catch(function(err){
console.log('Home',err);
});
}
}
}
</script> <style scoped>
.mt30{
margin-top: 30px;
}
</style>
3.静态数据

4.效果图

vue2.0 仿手机新闻站(四)axios的更多相关文章
- vue2.0 仿手机新闻站(一)项目开发流程
vue仿手机新闻站: 1.拿到静态页面,直接用vue边布局,边写 2.假数据 没有用任何UI组件,切图完成 做项目基本流程: 1.规划组件结构 Nav.vue Header.vue Home.vue ...
- vue2.0 仿手机新闻站(七)过滤器、动画效果
1.全局过滤器 (1)normalTime.js 自定义 将 时间戳 转换成 日期格式 过滤器 /** * 将 时间戳 转换成 日期格式 */ export const normalTime = ( ...
- vue2.0 仿手机新闻站(二)项目结构搭建 及 路由配置
1.项目结构 $ vue init webpack-simple news $ npm install vuex vue-router axios style-loader css-loader -D ...
- vue2.0 仿手机新闻站(六)详情页制作
1.结构 2.配置详情页路由 router.config.js /** * 配置 路由 */ // 导入组件 import Home from './components/Home.vue' impo ...
- vue2.0 仿手机新闻站(五)全局的 loading 组件
1.组件结构 index.js const LoadingComponent = require('./Loading.vue') const loading = { install: functio ...
- vue2.0 仿手机新闻站(三)通过 vuex 进行状态管理
1.创建 store 结构 2.main.js 引入 vuex 3. App.vue 组件使用 vuex <template> <div id="app"&g ...
- 项目vue2.0仿外卖APP(四)
组件拆分 先把项目搭建时生成的代码给清了吧 现在static目录下引入reset.css 接着在index.html引入,并且设置<meta> 有时候呢,为了让代码符合我们平时的编码习惯, ...
- Vue2.0仿饿了么webapp单页面应用
Vue2.0仿饿了么webapp单页面应用 声明: 代码源于 黄轶老师在慕课网上的教学视频,我自己用vue2.0重写了该项目,喜欢的同学可以去支持老师的课程:http://coding.imooc.c ...
- 项目vue2.0仿外卖APP(六)
goods 商品列表页开发 布局编写 除了商品之外还有购物车,还有个详情页,挺复杂的. 两栏布局:左侧固定宽度,右侧自适应,还是用flex. 因为内容可能会超过手机高度,超过就隐藏.左右两侧的内容是可 ...
随机推荐
- Mybatis通过接口的方式实现增删改查
导入jar包 [mybatis] [oracle] 生成数据库 1.添加Mybatis的配置文件mybatis-config.xml 在src目录下创建一个mybatis-config.xml文件,如 ...
- CH Round #59 - OrzCC杯NOIP模拟赛day1
第一题:队爷的新书 题意简述:给定n个闭区间,求出一个数p使它与包含它的区间数的积最大,输出这个积. 分析:使用一个差分数组g,每个区间[l,r],l位置加1,r+1的位置减1,从前往后统计,得到对于 ...
- Codeforces #990E Post Lamp
题目大意 今欲用若干条长为 $k$($1\le k\le m, k\in \mathbb{Z}$) 的线段覆盖数轴上 $[0,n]$ 这一段.线段的起点(左端点)必须为 $[0, n-1]$ 中的某个 ...
- BZOJ 4460 [Jsoi2013]广告计划 ——Bitset 后缀自动机
发现n比较小,直接枚举答案,然后发现连续的一段是确定的,然后我们只需要判断每个位置是否有这个连续的一段就好了 发现起点不同,最后的位置可能会有差距,所以DP一下就好了 然后用0表示未折返,1表示从最下 ...
- 【04】【转】Nodejs学习笔记(四)--- 与MySQL交互(felixge/node-mysql)
目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...
- 洛谷P2168 荷马史诗 [NOI2015]
题目描述 追逐影子的人,自己就是影子 ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛&g ...
- CF126B password&&HDU 4763 Theme Section
http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...
- C#中的继承与覆盖
原文发布时间为:2009-03-03 -- 来源于本人的百度文章 [由搬家工具导入] //using System;//using System.Collections.Generic;using S ...
- CSS 五种方式实现 Footer 置底
CSS 五种方式实现 Footer 置底 页脚置底(Sticky footer) 就是让网页的footer部分始终在浏览器窗口的底部. 当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推 ...
- vim 搜尋取代功能
VI 的搜尋取代語法格式大致如下 :[範圍]s/[比對字串]/[取代字串]/[g,c,i] 範圍部分: 範圍表示法為開頭 , 結束 假如我要從第 1 行到第 150 行,可以這樣下: 1, 150 假 ...