1.上一章--引入UI框架
2.苍渡大神Github源码地址--源码地址
3.UI框架用的是Mint UI,上一章已经引入
4.数据接口地址--Github
5.下一章--v-for,v-bind以及计算属性的简单使用css
1.先看一下UI图html
2.头部没有现成的组件,可是有一个固定定位的组件Header,稍加修改便可,home.vue
修改以下
3.npm run dev
运行页面以下
登陆与注册应该是链接,但我们先这么写,后面用到再改,Header
下的元素应该有个margin-top,这时发现这个css样式应该不少页面都要用,那应该写在全局的css里,怎么写才是全局的css?。我们在main.js
引入Mint-ui
的时候,写了一段代码vue
import 'mint-ui/lib/style.css'
这是引入Mint-ui
的css,但我们在home.vue组件并无引入能够直接使用,那我们的全局CSS是否是也是这么引入的呢?
4.在src文件夹下新建文件夹style,在style内新建文件mycss.css
,代码以下
在main.js
引入node
import './style/mycss.css'
在home.vue修改将全局css加入到元素上git
<template> <div> <mt-header fixed> <span slot="left">elm</span> <mt-button slot="right">登陆|</mt-button> <mt-button slot="right">注册</mt-button> </mt-header> <h1 class='mgtop40'>这里是home页面</h1> </div> </template>
页面效果以下es6
若是谷歌手机模式浏览字变小在index.html
的head
加入如下代码github
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0" />
能够看到全局样式已经应用,接下来写页面web
1.home.vue
页面代码修改以下ajax
<template> <div> <div class="fixed bgfff"> <mt-header> <span slot="left">elm</span> <mt-button slot="right">登陆|</mt-button> <mt-button slot="right">注册</mt-button> </mt-header> </div> <div class="padtop40 bgf5"> <div class="ih50 padlr10 box bgfff"> <span class="">当前选择城市</span> <span class="right fs0-8 col9f">定位不许时,请在城市列表选择</span> </div> <mt-cell title="天津" class="col after" to="" is-link value=""></mt-cell> <div class="mgtop10 bgfff"> <mt-cell class="after" title="热门城市"></mt-cell> <div class="itembox ovhid col"> <div>天津</div><div>天津</div><div>天津</div><div>天津</div> <div>天津</div><div>天津</div><div>天津</div><div>天津</div> <div>天津</div><div>天津</div><div>天津</div><div>天津</div> </div> </div> <div class="mgtop10 bgfff"> <mt-cell class="after" title="A"></mt-cell> <div class="itembox ovhid"> <div>天津</div><div>天津</div><div>天津</div><div>天津</div> <div>天津</div><div>天津</div><div>天津</div><div>天津</div> <div>天津</div><div>天津</div> </div> </div> </div> </div> </template> <script> export default { data () { return { } }, component:{ } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .itembox>div{ width:25%; float:left; text-align:center; height:40px; line-height:40px; box-sizing: border-box; border-right:1px solid #e4e4e4; border-bottom:1px solid #e4e4e4; } .itembox>div:nth-child(4n){ border-right:0px; } </style>
2.mycss.css
修改以下(body
默认有个margin:8px
,是由于Mint-ui
么?)npm
body{ margin: 0px; height: 100vh; background-color: #f5f5f5; } .fixed{ position: fixed; top: 0px; width: 100%; z-index: 5; } .ih40{ height: 40px; line-height: 40px; } .ih50{ height: 50px; line-height: 50px; } .bgcol{ background-color:#26a2ff; } .bgfff{ background-color: #fff; } .bgf5{ background-color:#F5F5F5; } .fs0-8{ font-size: 0.8rem; } .fs1-2{ font-size: 1.2rem; } .col9f{ color: #9F9F9F; } .col{ color: #26a2ff; } .box{ box-sizing: border-box; } .padlr10{ padding:0px 10px 0px 10px; } .padtop10{ padding-top:10px; } .padtop40{ padding-top:40px; } .mgtop40{ margin-top: 40px; } .mgtop10{ margin-top: 10px; } .mgbot10{ margin-bottom: 10px; } .ovhid{ overflow: hidden; } .right{ float: right; } .clear{ clear: both; } /*一像素分割线*/ .after{ position: relative; } .after::after{ content: " "; position: absolute; left: 0; bottom: 0; width: 100%; height: 1px; background-color: #e4e4e4; -webkit-transform-origin: left bottom; transform-origin: left bottom; } /* 2倍屏 */ @media only screen and (-webkit-min-device-pixel-ratio: 2.0) { .after::after { -webkit-transform: scaleY(0.5); transform: scaleY(0.5); } } /* 3倍屏 */ @media only screen and (-webkit-min-device-pixel-ratio: 3.0) { .after::after { -webkit-transform: scaleY(0.33); transform: scaleY(0.33); } }
页面效果
须要注意的是,我写了一个after
的class,这是用来利用css伪类::after
来实现元素下边框一像素的class(手机上若是直接用border-bottom
设置1px
的话页面显示并非一像素而是2像素或者3像素,虽然大部分用户并不能感受出有什么区别,可是设计师会很不开心--这种代码网上有不少)。
3.ok,页面写好了,下面是请求数据。接口地址。之前发送请求获取数据,我是用ajax
,可是vue本身封装了一个数据请求插件vue-resource,我们先来试试。
1.安装vue-resource。我们在第一章建立的时候已经安装了 。未安装的能够进入vue项目后
npm install --save vue-resource
安装成功后,能够打开项目的package.json
文件查看全部的配置文件,其中就有vue-resource
的版本信息
2.引入。
使用首先要引入,打开main.js
,输入
import Resource from 'vue-resource' Vue.use(Resource)
ok,这就引入成功,就能够在组件中写代码使用。
3.使用
打开home.vue
文件,在<script></script>
中输入
export default { data () { return { } }, component:{ //注册组件 }, mounted:function(){ //生命周期 this.$http.get('http://cangdu.org:8001/v1/cities?type=group').then(response => { console.log(response); }, response => { console.log(response); }); }, computed:{ //计算属性 }, methods:{ //函数 } }
return
中用来写须要用到的变量。component
中用来注册本组件须要引用的其余外部组件(用到的时候再说)。mounted
是vue的生命周期,你们都知道页面是有加载顺序的,不是说一会儿页面就能够出来。而vue从建立到使用到结束分为了十个周期,称为生命周期钩子,而mounted
是把vue数据加载的html的时候调用(这是我目前的理解)。computed
是计算属性,其中写一个个函数,这些函数用来计算属性,当属性改变时,函数的结果的也会随之改变,而咱们使用时只须要调用一次函数便可(用到再说)。methods
中用来写函数,调用一次执行一次。
4.请求
我们把数据请求放到 mounted
中,vue-resource
代码以下
this.$http.get('http://cangdu.org:8001/v1/cities?type=group').then(response => { console.log(response); }, response => { console.log(response); });
this
指向vue。数据接口中请求城市列表是GET
请求,因此咱们使用this.$http.get()
方法。then
中有两个函数,第一个是请求成功的函数,第二个是请求失败的函数。上面的代码使用了es6
的箭头函数
(我也只是看得懂,并无用过)
城市的接口地址
数据请求代码这就写完了,我们先运行试试,能够在控制台看到ok,数据请求成功,下面就是怎样把数据放到页面里呢?