vue从创建到完整的饿了么(5)v-for,v-bind与计算属性

news/2024/5/29 1:26:59

说明

1.上一章--Home.vue及vue-resource使用
2.cangdu大神的项目源码地址--Github地址
3.接口地址--Github地址
4.UI框架用的是--Mint UI
5.下一章--登录注册页面及密码的暗明文转换

开始

1.先看看Home.vue代码

<template><div><mt-header fixed><span slot="left">elm</span><mt-button slot="right">登录|</mt-button><mt-button slot="right">注册</mt-button></mt-header><div class="padtop40"><div class="after 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:{//注册组件},mounted:function(){//生命周期this.$http.get('http://cangdu.org:8001/v1/cities?type=group').then(response => {console.log(response);}, response => {console.log(response);});},computed:{//计算属性},methods:{//函数}
}
</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.赋值

首先我们要把城市列表的信息先赋值给一个变量,这样我们就可以调用了嘛。图片描述

3.v-for
城市列表我们有了,但是我们怎么把它显示到页面呢?难道要用for循环拼接字符串插到页里么?NONONO,那还是操作DOM节点,而VUE的好处之一就是操作数据来控制DOM。循环VUE用的是v-for,home.vue的html部分代码修改如下

<template><div><mt-header fixed><span slot="left">elm</span><mt-button slot="right">登录|</mt-button><mt-button slot="right">注册</mt-button></mt-header><div class="padtop40"><div class="after 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 v-for="(items,index) in citylist" class="mgtop10 bgfff"><mt-cell class="after" :title="index"></mt-cell><div class="itembox ovhid"><div v-for="item in items">{{item.name}}</div></div>        </div>  </div></div>
</template>

其实只是把全部城市列表的代码修改如下

 <div v-for="(items,index) in citylist" class="mgtop10 bgfff"><mt-cell class="after" :title="index"></mt-cell><div class="itembox ovhid"><div v-for="item in items">{{item.name}}</div></div>        </div>  

修改第一段代码v-for="(items,index) in citylist"
citylist是一个object对象(也可以是数组),itemscitylist的每一项键值,indexitems的键名(若为数组则是1,2,3...递增)。citylist有多少项,就会循环多少次,就会产生多少个元素(该元素为 v-for 写在的那个元素,其内的子元素也会自动生成)。然后就可以用item在元素中当做键值来使用。

修改第二段代码:title="index"
因为咱们获取的数据的键名就是A,B,C,D...所以咱们的城市列表直接用index来排序。而:title="index" v-bind:title="index"的缩写。vue的命令都用v-来开头。v-bind用来绑定DOM属性。

修改第三段代码<div v-for="item in items">{{item.name}}</div>
老铁们要注意,若items是A开头的城市数组集合,而itemitems的 每一项,即一个具体的城市(这只是举一个例子)

ok,代码咱们先写这么多,来看看页面变化。图片描述

全部出现!!老铁们可以看到,咱们只需要写一个模板,vue会帮我们自动生成所有的数据。但是还有几个问题需要处理一下
1.数据的顺序并不是从A挨个排到Z;
2.有的城市名字太长出现重叠。

4.排序
思路:重新创建一个object,键名是从A到Z,键值就是获取的数据的键值。
计算函数:js写在哪?一种方法是写在生命周期mounted的数据请求里,直接返回一个处理好的citylist,但咱们用另一种方法--计算属性computed。home.vue部分是代码修改如下

computed:{//计算属性getlist:function(){var mycitylist={};for(var i=65;i<=90;i++){var num= String.fromCharCode(i);mycitylist[num]=this.citylist[num];}return mycitylist}},

fromCharCode()是把ascii码转成字符,所以num就是A,B,C,D...当我们调用getlist函数时,得到的是mycitylist(当citylist改变时,无需重新调用,mycitylist会随之改变)
函数写好了在哪调用呢?

    <div v-for="(items,index) in getlist" class="mgtop10 bgfff"><mt-cell class="after" :title="index"></mt-cell><div class="itembox ovhid"><div v-for="item in items">{{item.name}}</div></div>        </div>  

只是把<template></template>中的citylist替换成getlist即可。
看看页面结果图片描述

解决!城市已经按照A-Z排列,只剩下名字长度问题了,设置为单行不换行超出省略号即可,在src下的style下的mycss.css添加

.nowarp{white-space:nowrap;          /* 不换行 */overflow:hidden;               /* 内容超出宽度时隐藏超出部分的内容 */text-overflow:ellipsis;   /* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/
}

nowarp这个class加到城市名字的div上即可
图片描述

解决。这么看城市列表是没有问题了,那咱们接下来请求热门城市和当前城市。home.vue修改如下

<template><div><mt-header fixed><span slot="left">elm</span><mt-button slot="right">登录|</mt-button><mt-button slot="right">注册</mt-button></mt-header><div class="padtop40"><div class="after ih50 padlr10 box bgfff"><span  class="">当前选择城市</span><span  class="right fs0-8 col9f">定位不准时,请在城市列表选择</span></div><mt-cell  :title="nowcity.name" 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 v-for="item in hotcity">{{item.name}}</div></div>        </div>         <div v-for="(items,index) in getlist" class="mgtop10 bgfff"><mt-cell class="after" :title="index"></mt-cell><div class="itembox ovhid"><div class="nowarp" v-for="item in items">{{item.name}}</div></div>        </div>  </div></div>
</template><script>export default {data () {return {citylist:"",hotcity:"",nowcity:""}},component:{//注册组件},mounted:function(){//生命周期//城市列表this.$http.get('http://cangdu.org:8001/v1/cities?type=group').then(response => {console.log(response);this.citylist=response.body;}, response => {console.log(response);});//热门城市this.$http.get('http://cangdu.org:8001/v1/cities?type=hot').then(response => {console.log(response);this.hotcity=response.body;}, response => {console.log(response);});//定位城市this.$http.get('http://cangdu.org:8001/v1/cities?type=guess').then(response => {console.log(response);this.nowcity=response.body;}, response => {console.log(response);});},computed:{//计算属性getlist:function(){var mycitylist={};for(var i=65;i<=90;i++){var num= String.fromCharCode(i);mycitylist[num]=this.citylist[num];}return mycitylist}},methods:{//函数}
}
</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>

页面结果如下图片描述
搞定!home.vue大致写完了,剩下就是交互了


http://lihuaxi.xjx100.cn/news/239731.html

相关文章

Linux二进制导出配置文件,Go打包二进制文件的实现

背景众所周知&#xff0c;go语言可打包成目标平台二进制文件是其一大优势&#xff0c;如此go项目在服务器不需要配置go环境和依赖就可跑起来。操作需求&#xff1a;打包部署到centos7笔者打包环境&#xff1a;mac os方法&#xff1a;进入main.go所在目录&#xff0c;输入如下命…

服务器部署基础知识_我在生产部署期间学到的知识

服务器部署基础知识by Shruti Tanwar通过Shruti Tanwar 我在生产部署期间学到的知识 (What I learned during production deployment) Production deployment. The final stage of every project. When all the hard work you’ve put in over the course of time goes live t…

springbatch apache-activemq 整合(往mq中put数据,从mq中take数据)

简单测试如下&#xff1a;1&#xff1a;收下下载apache-activemq-5.14.4 解压apache-activemq-5.14.4\bin\win64&#xff0c;运行activemq.bat启动本地MQ服务器。通过浏览器可以查看本地MQ服务器的信息。http://127.0.0.1:8161/admin/index.jsp 2: 先往mq中put数据配置如下&…

linux 565显示格式,RGB565转BMP格式 C语言程序

#include#include#include#include"rgb2bmp.h"int RGB2BMP(char *,int ,int ,FILE *);int main(int argc,char *argv[]){double num_tmp 0;FILE *p;/*************** input data ***********filename :RGB数据文件名称nWidth :所生成文件的水平像素n…

javascript语法糖_语法糖和JavaScript糖尿病

javascript语法糖by Ryan Yurkanin瑞安尤卡宁(Ryan Yurkanin) 语法糖和JavaScript糖尿病 (Syntactic Sugar and JavaScript Diabetes) Syntactic sugar is shorthand for communicating a larger thought in a programming language.语法糖是用编程语言传达更大思想的简写。 …

为什么多 TCP 连接比单 TCP 连接传输快

转自&#xff1a; 我观察到&#xff0c;客户端机器从单一服务器使用 HTTP 下载一个文件&#xff1a;1. 单连接下载&#xff0c;速度没有达到客户端网络的最大带宽&#xff1b;2. 多连接同时下载&#xff0c;传输速度有极大的提高&#xff0c;带宽被占满。假设如下前提&#xff…

linux系统 插优盘安装xvidcap,linux下的视频录制软件xvidcap

1.xvidcap简介在linux如果我们想要进行视频录制&#xff0c;那么xvidcap是一个不错的选择。Xvidcap 是一个可将屏幕上的操作过程录制下来并保存为视频的小工具。对于需要制作产品演示和教学的朋友来说&#xff0c;这个屏幕录像机十分实用。Xvidcap 支持生成 avi、mpeg、asf、fl…

超越Android:Kotlin在后端的工作方式

by Adam Arold亚当阿罗德(Adam Arold) 超越Android&#xff1a;Kotlin在后端的工作方式 (Going Beyond Android: how Kotlin works on the Backend) This article is part of a series.本文是系列文章的一部分。 While most developers use Kotlin on Android, it is also a …