vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用

发布时间:2019-08-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

说明

1.上一章--引入UI框架
2.苍渡大神GIThub码地址--源码地址
3.ui框架用的是Mint UI,上一章已经引入
4.数据接口地址--Github
5.下一章--v-for,v-bind以及计算属性的简单使用

头部

1.先看一下UI图

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用

2.头部没有现成的组件,但是有一个固定定位的组件Header,稍加修改即可,home.vue修改如下

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用


3.npm run dev 运行页面如下

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用


登录与注册应该是连接,但咱们先这么写,后面用到再改,Header下的元素应该有个margin-top,这时发现这个css样式应该很多页面都要用,那应该写在全局的css里,怎么写才是全局的css?。咱们在main.js引入Mint-ui的时候,写了一段代码

@H_304_44@
import 'mint-ui/lib/style.css'

这是引入Mint-ui的css,但咱们在home.vue组件并没有引入可以直接使用,那咱们的全局CSS是不是也是这么引入的呢?
4.在src文件夹下新建文件夹style,在style内新建文件mycss.css,代码如下

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用


main.js引入

import './style/mycss.css'

在home.vue修改将全局css加入到元素上

<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>

页面效果如下

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用

如果谷歌手机模式浏览字变小在index.htMLhead加入以下代码

<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0" />

可以看到全局样式已经应用,接下来写页面

home.vue主体

1.home.vue页面代码修改如下

" title="" data-original-title="复制">
<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么?)

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);
    }
}

页面效果

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用

需要注意的是,我写了一个after的class,这是用来利用css伪类::after来实现元素下边框一像素的class(手机上如果直接用border-bottom设置1px的话页面显示并不是一像素而是2像素或者3像素,虽然大部分用户并不能感觉出有什么区别,但是设计师会很不开心--这种代码网上有很多)。

3.ok,页面写好了,下面是请求数据。接口地址。以前发送请求获取数据,我是用ajax,但是vue自己封装了一个数据请求插件vue-resource,咱们先来试试。

vue-resource

1.安装vue-resource。咱们在第一章创建的时候已经安装了 。未安装的可以进入vue项目后

 npm install --save vue-resource

安装成功后,可以打开项目的package.json文件查看所有的配置文件,其中就有vue-resource的版本信息

vue从创建到完整的饿了么(4)Home.vue布局及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箭头函数(我也只是看得懂,并没有用过)
城市的接口地址

数据请求代码这就写完了,咱们先运行试试,可以在控制台看到

vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用


ok,数据请求成功,下面就是怎样把数据放到页面里呢?

脚本宝典总结

以上是脚本宝典为你收集整理的vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用全部内容,希望文章能够帮你解决vue从创建到完整的饿了么(4)Home.vue布局及vue-resource使用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:allallAPIAPIbreakbreakbuttonbuttonClassCSSCSSdefaultdefaultdivdoes6exportexportexportfloatfloatforforformformfunctionfunctionfunctionhiddenhiddenImportininintjavalinknamenamenodepackagepackagepost-format-galleryreturnreturnreturnselectstaticstaticstatictexttextthistoptopVuevuejs生命周期钩子箭头函数箭头函数
猜你在找的ES2015(es6)相关文章
其他相关热搜词更多
phpjavapython程序员loadpost-format-gallerydivString参数开发ListcapMapClass工具安装SQL数据库资源this
全站导航更多
最新ES2015(es6)教程
热门ES2015(es6)教程