Vue-自带vue-resource插件实现http请求
安装
本地环境安装路由插件vue-resource: cnpm install vue-resource --save-dev
*没有安装淘宝镜像的可以将 cnpm 替换成 npm
想要安装的可以看这篇文章http://www.cnblogs.com/padding1015/p/7162024.html,(打开搜索 镜像 即可跳转到对应位置)
配置
1.引用
main.js中用import引入进来:
import VueResource from 'vue-resource'
* 注意这里VueResource是自定义的名字。下边注册的时候会用到
2.注册
同样,main.js中注册,同vue-router
Vue.use(VueResource)
3.配置
直接在对应页面的created钩子函数配置即可:
created() {
this.$http.get("http://jsonplaceholder.typicode.com/todos")
.then((data) => {
// console.log(data)
this.arrs = data.body;
})
链接是数据接口的地址,arrs是在本页面的data函数中定义的空对象,data.body是拿到的数据存放的地方。
4. 使用数据
根据拿到的数据结构和内容对应渲染
页面中直接渲染使用:
<li v-for="item in arrs" v-on:click="item.completed = ! item.completed">
<!-- {{item}} -->
<span class="id">{{item.userId}} </span>
<span class="title">{{item.title}}</span>
<span class="completed" v-show="item.completed">选中</span>
</li>
用到vue-resource的页面代码参考:
<template>
<div class="app-cont">
<div class="vue-resource">
<h3>获取其他地址里的json数据并进遍历</h3>
<ul>
<li v-for="item in arrs" v-on:click="item.completed = ! item.completed">
<!-- {{item}} -->
<span class="id">{{item.userId}} </span>
<span class="title">{{item.title}}</span>
<span class="completed" v-show="item.completed">选中</span>
</li>
</ul>
</div>
</div>
</template>
<script>
export default{
name: "app-cont",
data () {
return {
arrs:{}
}
},
methods: {
},
// 页面加载之前,用created钩子函数-获取网页数据
created() {
this.$http.get("http://jsonplaceholder.typicode.com/todos")
.then((data) => {
// console.log(data)
this.arrs = data.body;
})
}
}
</script>
<style scoped>
h2{
margin: 0;
padding: 10px 0;
background: #f5f5f5;
}
ul{
display: flex;
flex-wrap: wrap;
margin: 0 auto;
padding: 10px 0;
}
li{
position: relative;
flex-grow: 1;
flex-basis: 200px;
text-align: center;
padding: 30px;
margin: 5px;
border: 2px solid Lightgreen;
}
li:hover{
background: Lightgreen;
cursor: pointer;
}
span{
color: #333;
font-weight: bold;
}
.vue-resource{
margin: 20px 40px;
border: 1px solid Lightgreen;
background: #eee;
}
span{
display: block;
}
span.id{
width: 75%;
margin: 0 auto 20px;
padding: 10px 0;
border-bottom: 1px solid Lightgreen;
} span.completed{
position: absolute;
top: -6px;
right: 10px;
z-index: 999;
width: 20px;
height: 45px;
padding-top: 5px;
color: #90ee90;
background: #000;
}
</style>
声明:
请尊重博客园原创精神,转载或使用图片请注明:
博主:xing.org1^
出处:http://www.cnblogs.com/padding1015/
Vue-自带vue-resource插件实现http请求的更多相关文章
- Vue自带的过滤器
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 过滤器写法 {{ message | Filter}} 二 ...
- vue中自定义组件(插件)
vue中自定义组件(插件) 原创 2017年01月04日 22:46:43 标签: 插件 在vue项目中,可以自定义组件像vue-resource一样使用Vue.use()方法来使用,具体实现方法: ...
- 【Vue】---编写Vue插件流程---【巷子】
一.在Vue中编写插件流程 1.创建组件 components/message.vue <template> <div class="message" v-if= ...
- Vue图片懒加载插件
图片懒加载是一个很常用的功能,特别是一些电商平台,这对性能优化至关重要.今天就用vue来实现一个图片懒加载的插件. 这篇博客采用"三步走"战略--Vue.use().Vue.dir ...
- 基于 Vue BootStrap的迷你Chrome插件
代码地址如下:http://www.demodashi.com/demo/14306.html 安装 安装 Visual Studio Code 和Chrome, 自行FQ 详细安装这里略过 安装包管 ...
- vue点击返回顶部插件vue-totop
vue-totop vue 点击返回顶部插件,可以根据参数设定按钮大小,颜色,类型,返回顶部的时间等.github地址:https://github.com/1006008051/vue-totop, ...
- 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示
在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...
- vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证)
vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证): html: <el-form-item label="活动时间" required> & ...
- 10.vue router 带参数跳转
vue router 带参数跳转 发送:this.$router.push({path:'/news',query:{id:row.id}}) 接收:var id=this.$route.query. ...
- 实战:vue项目中导入swiper插件
版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4.常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: n ...
随机推荐
- XAMPP的配置与使用
XAMPP的配置与使用 一.起因 一开始,我在本地手动DIY安装了WAMPP环境,其中经历了很长时间的折腾,原因是独立安装Apache和MySQL,以及PHP的时候,屡次遇到版本兼容问题,我主要的时间 ...
- 在 CentOS7.0 上搭建 Chroot 的 Bind DNS 服务器
BIND(Berkeley internet Name Daemon)也叫做NAMED,是现今互联网上使用最为广泛的DNS 服务器程序.这篇文章将要讲述如何在 chroot 监牢中运行 BIND,这样 ...
- ps通道抠章
1. 打开图片 2. 使用椭圆形选框工具,选择章所在范围(ALT+SHITF+鼠标左键) 3.复制图层(CTRL+J)为图层1,隐藏背景 4.进入通道,选择对比度最大的通道,复制通道副本 5.反选(C ...
- windows FileZilla Server 开启FTP over TLS
FileZilla Server官方下载地址: https://filezilla-project.org/download.php?type=server FileZilla Server 开启FT ...
- xBIM WeXplorer xViewer的导航,相机、剖切、隐藏 等操作
目录 基础 xBIM WeXplorer 简要介绍 xBIM WeXplorer xViewer 基本应用 xBIM WeXplorer xViewer 浏览器检查 xBIM WeXplorer xV ...
- CENTOS6.6下mysql MMM架构搭建
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn MMM(Master-Master replication mana ...
- esxi 改变虚拟机磁盘格式为精简存储
最近在部署虚拟机,导入几个之前保存的ovf模板,发现存储已经被耗费的差不多了.检查了下磁盘存储格式 存储类型是 后置备延迟置零 占用空间 简单了解下 三种存储类型 1.厚置备延迟置零: 默认的创建格式 ...
- CSS单行、多行文本溢出显示省略号
如果实现单行文本的溢出显示省略号小伙伴们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; t ...
- nxlog4go 按天或按文件大小分割日志
Building a new rotate file writer: rfw := l4g.NewRotateFileWriter("_rfw.log").SetMaxSize(1 ...
- mysql window版本下载
最小的版本:https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.54-win32.msi