Vue2.5从零开发猫眼系列以更新完毕

Vue2.5从零开发猫眼①——前言 

Vue2.5从零开发猫眼②——项目开始前准备

Vue2.5从零开发猫眼③——启动页开发

Vue2.5从零开发猫眼④——Home页开发

Vue2.5从零开发猫眼⑤——影院页开发

Vue2.5从零开发猫眼⑥——个人中心组件开发

Vue2.5从零开发猫眼⑦——City组件开发

Vue2.5从零开发猫眼⑧——引入store、LocalStorage和优化上线

CommonHeader组件开发

在src目录创建pages\home\Home.vue

<template><div><common-header :title="title" :back="true" @goBack="goBack"></common-header></div>
</template><script>
import CommonHeader from '../../components/header/Header'
export default {name: 'Home',components: {CommonHeader},data () {return {title: '首页'}},methods: {goBack() {this.$router.back()}}
}
</script>
<style>
</style>

修改router\index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/pages/home/Home'
import Start from '@/pages/start/Start'Vue.use(Router)
export default new Router({routes: [{path: '/',name: 'start',component: Start},{path: '/home',name: 'home',component: Home}]
})

这里我们引入了CommonHeader组件,所以我们在src\components\header下创建Header.vue, 因为CommonHeader属于其他页面公共的组件,所以我们把公共组件都放到components目录下,方便引用

<template><div class="header"><div class="header-left" v-if="back" @click="goBack"><span class="iconfont icon-zuojiantou"></span></div><h1 class="title" v-text="title"></h1></div>
</template><script>
export default {name: 'Header',props: {title: {type: String,default: ''},back: {type: Boolean,default: false}},methods: {goBack() {this.$emit('goBack')}}
}
</script><style lang="stylus" scoped>@import '~stylus/variable.styl'.headerheight: 50pxcolor: #fffbackground: $bgColorborder-bottom: 1px solid #e54847position: relative.header-leftposition: absoluteleft: 10pxtop: 16px.icon-zuojiantoufont-size:20px.titlefont-size: 18pxfont-weight: 400text-align: centerline-height: 50pxcolor: #fff
</style>

CommonHeader 只做页面显示,不处理逻辑,如果其他组件需要引用它时候需要传参数,和监听对应的goBack函数。

NavMenu组件开发

因为NavMenu只是home页的组件,所以在home组件页下创建components\nav-menu\NavMenu组件

<template><div class="nav-menu scale-1px"><router-link to='/city'><div class="left"><span>北京</span><span class="iconfont icon-xiala"></span></div></router-link><div class="center"><span class="menu" :class="[menuActive == menu.id ? 'active' : '']" @click="changeMenu(menu.id)" v-for="menu of menuList" :key="menu.id">{{ menu.title }}</span></div><router-link to='/search'><div class="right"><span class="iconfont icon-sousuo"></span>  </div></router-link></div>
</template><script>
export default {name: 'NavMenu',props: {menuActive: {type: Number,default: 0}},data() {return {menuList: [{id: 0,title: '正在热映'},{id: 1,title: '即将上映'}]}},methods: {changeMenu(menuId) {this.$emit('changeMenu', menuId);}}
}
</script><style lang="stylus" scoped>@import '~stylus/variable.styl'.nav-menuheight: 44pxdisplay: flex.leftwidth: 80pxfloat: leftline-height:44pxcolor: #666text-align: center.icon-xialafont-size:14px.centerflex: 1text-align: centerline-height:44pxpadding: 0 20pxposition: relative.menudisplay:inline-blockcolor: #666font-weight: 700width: 50%;float: left;box-sizing: border-box;&.activecolor: $bgColor&:aftercontent: ""display: blockposition: absolutebottom: -1pxwidth: calc(50% - 20px);height: 2pxbackground-color: $bgColor.rightfloat: rightwidth: 50pxcolor: $bgColorline-height:44pxtext-align: centerfont-size:16pxfont-weight: bold
</style>

这个组件需要注意的是使用了选中的menu下划线使用了calc的函数,具体calc使用可以去网上搜索

然后再Home.vue里引用该组件

1. import NavMenu from './components/nav-menu/NavMenu'
2. components: {CommonHeader,NavMenu},
3. <nav-menu :menu-active="menuActive" @changeMenu="changeMenu"></nav-menu>

内容组件开发

  • 先获取数据接口 再static\创建mock\home.json

    {"code": 1,
    "movieList": [{"id": 249342,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/c106904da68edd848afd4a320976d051346321.jpg","version": "v3d imax","nm": "海王","preShow": false,"sc": 9.6,"globalReleased": true,"wish": 250996,"star": "杰森·莫玛,艾梅柏·希尔德,妮可·基德曼","rt": "2018-12-07","showInfo": "今天101家影院放映2598场","showst": 3,"wishst": 0},{"id": 1220969,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/3020fcde26f323551243819629c251a8376809.jpg","version": "","nm": "惊涛飓浪","preShow": false,"sc": 0,"globalReleased": true,"wish": 36139,"star": "谢琳·伍德蕾,山姆·克拉弗林,伊丽莎白·霍桑","rt": "2018-12-07","showInfo": "今天101家影院放映777场","showst": 3,"wishst": 0},{"id": 1208282,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/3e7696319c840d4890e947b926259d532809641.jpg","version": "","nm": "无名之辈","preShow": false,"sc": 9.1,"globalReleased": true,"wish": 15666,"star": "陈建斌,任素汐,潘斌龙","rt": "2018-11-16","showInfo": "今天96家影院放映554场","showst": 3,"wishst": 0},{"id": 78480,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/feb4cd6eb054232b4851a97bbc2a66d51198036.jpg","version": "","nm": "狗十三","preShow": false,"sc": 8.4,"globalReleased": true,"wish": 18506,"star": "张雪迎,果靖霖,智一桐","rt": "2018-12-07","showInfo": "今天94家影院放映318场","showst": 3,"wishst": 0},{"id": 123,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/c304c687e287c7c2f9e22cf78257872d277201.jpg","version": "","nm": "龙猫","preShow": false,"sc": 0,"globalReleased": false,"wish": 105627,"star": "秦岚,糸井重里,岛本须美","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0},{"id": 1215919,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/2205b5298f91edc2b072f1dbc8d4e46e5518392.jpg","version": "","nm": "印度合伙人","preShow": false,"sc": 0,"globalReleased": false,"wish": 18745,"star": "阿克谢·库玛尔,索娜姆·卡普尔,雷.雷富","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0},{"id": 42964,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/363e3a7e614d29b2847ff4e62afcd3f42168651.jpg","version": "v3d imax","nm": "毒液:致命守护者","preShow": false,"sc": 9.3,"globalReleased": true,"wish": 393571,"star": "汤姆·哈迪,米歇尔·威廉姆斯,里兹·阿迈德","rt": "2018-11-09","showInfo": "今天52家影院放映151场","showst": 3,"wishst": 0},{"id": 341213,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/600beaeaa9929649f38181d41f2c8578328129.jpg","version": "v3d imax","nm": "无敌破坏王2:大闹互联网","preShow": false,"sc": 8.9,"globalReleased": true,"wish": 102436,"star": "约翰·C·赖利,萨拉·西尔弗曼,克里斯汀·贝尔","rt": "2018-11-23","showInfo": "今天50家影院放映111场","showst": 3,"wishst": 0},{"id": 344649,"haspromotionTag": false,"img": "https://p1.meituan.net/128.180/movie/19efcfbf1a98c02cb5efd69a2edf766e2421917.jpg","version": "v2d imax","nm": "天气预爆","preShow": false,"sc": 0,"globalReleased": false,"wish": 23097,"star": "肖央,杜鹃,常远","rt": "2018-12-21","showInfo": "2018-12-21上映","showst": 4,"wishst": 0},{"id": 1207271,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/677dce9488e154c11afa8fb2fef0725a831453.png","version": "","nm": "憨豆特工3","preShow": false,"sc": 8.6,"globalReleased": true,"wish": 94378,"star": "罗温·艾金森,本·米勒,欧嘉·柯瑞兰寇","rt": "2018-11-23","showInfo": "今天28家影院放映62场","showst": 3,"wishst": 0},{"id": 346465,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/3e073bbe2abbdad57f6768270acbb6d85873182.jpg","version": "v3d imax","nm": "绿毛怪格林奇","preShow": false,"sc": 0,"globalReleased": false,"wish": 8396,"star": "本尼迪克特·康伯巴奇,卡梅伦·丝蕾,拉什达·琼斯","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0},{"id": 341753,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/30aff57a6e035d8a147806b4e207b1c67586143.jpg","version": "","nm": "三只小猪2","preShow": false,"sc": 8.3,"globalReleased": false,"wish": 16481,"star": "王晓彤,李晔,洪海天","rt": "2018-12-08","showInfo": "2018-12-08 本周六重映","showst": 4,"wishst": 0}
    ],
    "comingList": [{"id": 341753,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/30aff57a6e035d8a147806b4e207b1c67586143.jpg","version": "","nm": "三只小猪2","preShow": false,"sc": 8.3,"globalReleased": false,"wish": 16481,"star": "王晓彤,李晔,洪海天","rt": "2018-12-08","showInfo": "2018-12-08 本周六重映","showst": 4,"wishst": 0,"comingTitle": "12月8日 周六"},{"id": 1227795,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/a8cc227734ef0449f457341cfb93aabe187083.jpg","version": "","nm": "云上日出","preShow": false,"sc": 0,"globalReleased": false,"wish": 692,"star": "荣梓杉,关亚,丁宁","rt": "2018-12-11","showst": 1,"wishst": 0,"comingTitle": "12月11日 周二"},{"id": 1238951,"haspromotionTag": false,"img": "https://p1.meituan.net/128.180/movie/cf6230938a60b584e1fe1618ca9b3a2a8575606.jpg","version": "","nm": "谁是坏孩子","preShow": false,"sc": 0,"globalReleased": false,"wish": 313,"star": "安智辉,方从伊,沙学周","rt": "2018-12-11","showst": 1,"wishst": 0,"comingTitle": "12月11日 周二"},{"id": 1228750,"haspromotionTag": false,"img": "https://p1.meituan.net/128.180/movie/03b64c266a863c74d547feaef1859eab3294680.jpg","version": "","nm": "照相师","preShow": false,"sc": 0,"globalReleased": false,"wish": 855,"star": "谢钢,刘牧,康磊","rt": "2018-12-12","showst": 1,"wishst": 0,"comingTitle": "12月12日 周三"},{"id": 1238952,"haspromotionTag": false,"img": "https://p1.meituan.net/128.180/movie/a0f9704bb208c24359df82e88a16315c91983.jpg","version": "","nm": "缘·梦","preShow": false,"sc": 0,"globalReleased": false,"wish": 144,"star": "高蕊,刘滨,张津赫","rt": "2018-12-13","showst": 1,"wishst": 0,"comingTitle": "12月13日 周四"},{"id": 123,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/c304c687e287c7c2f9e22cf78257872d277201.jpg","version": "","nm": "龙猫","preShow": false,"sc": 0,"globalReleased": false,"wish": 106812,"star": "秦岚,糸井重里,岛本须美","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0,"comingTitle": "12月14日 周五"},{"id": 1215919,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/2205b5298f91edc2b072f1dbc8d4e46e5518392.jpg","version": "","nm": "印度合伙人","preShow": false,"sc": 0,"globalReleased": false,"wish": 18902,"star": "阿克谢·库玛尔,索娜姆·卡普尔,雷.雷富","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0,"comingTitle": "12月14日 周五"},{"id": 346465,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/3e073bbe2abbdad57f6768270acbb6d85873182.jpg","version": "v3d imax","nm": "绿毛怪格林奇","preShow": false,"sc": 0,"globalReleased": false,"wish": 8530,"star": "本尼迪克特·康伯巴奇,卡梅伦·丝蕾,拉什达·琼斯","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0,"comingTitle": "12月14日 周五"},{"id": 1219776,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/da4eb7b46debc0ee54ec7b5ff371198c944440.jpg","version": "","nm": "网络谜踪","preShow": true,"sc": 0,"globalReleased": false,"wish": 5858,"star": "约翰·赵,黛博拉·梅辛,米切尔·拉","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0,"comingTitle": "12月14日 周五"},{"id": 1229950,"haspromotionTag": false,"img": "https://p0.meituan.net/128.180/movie/5581cf675d3d623685328facc05abd933568286.jpg","version": "","nm": "午夜整容室","preShow": false,"sc": 0,"globalReleased": false,"wish": 4402,"star": "赖晶晶,王李丹妮,陈垚","rt": "2018-12-14","showInfo": "2018-12-14 下周五上映","showst": 4,"wishst": 0,"comingTitle": "12月14日 周五"}
    ]
    }

    修改config\index.js

    proxyTable: {
    '/api': {target: 'http://localhost:8081',pathRewrite: {'^/api' : '/static/mock'}
    }
    } 

    这里使用的是代理请求,访问api目录的时候能代理到/static/mock,端口号改成自己的端口,重新启动webpack则可以在浏览器访问http://localhost:8081/api/home.json

  • 获取异步数据

    npm install -D axios
    npm run dev

在src目录创建api\home.js

import axios from 'axios'export function getMoiveList() {return axios.get('/api/home.json', {}).then((res) => {return Promise.resolve(res.data)})
}
  • 因为我们这里是使用滚动条,所以要引入better-scroll组件

npm install -D better-scroll

在src\components\srcoll编写scroll组件

<template><div ref="wrapper"><slot></slot></div>
</template><script type="text/ecmascript-6">import BScroll from 'better-scroll'export default {props: {probeType: {type: Number,default: 1},click: {type: Boolean,default: true},data: {type: Array|Object,default: null},refreshDelay: {type: Number,default: 20}},mounted() {setTimeout(() => {this._initScroll()}, 20)},methods: {_initScroll() {if (!this.$refs.wrapper) {return}this.scroll = new BScroll(this.$refs.wrapper, {probeType: this.probeType,click: this.click})},refresh() {this.scroll && this.scroll.refresh()}},watch: {data() {setTimeout(() => {this.refresh()}, this.refreshDelay)}}}
</script><style scoped lang="stylus">
</style>
  • 在pages\home\component\content\创建Content组件 ```html

    ?> 这个组件没有什么逻辑性内容,只需要home组件传递menuActive 显示对应的内容即可。- 在home组件引入content组件
    ```js
    import MovieContent from './components/content/Content'
    components: {CommonHeader,NavMenu,MovieContent,
    },

    还需要向Content组件传递menuActive,默认让第几个内如区域显示。

    CommonFooter组件开发

    在src目录创建pages\footer\Footer.vue

    <template><div class="bottom"><div class="btn-item" v-for="(item,index) of itemList" :key="index"><router-link :to="item.href" :class="[bottomActive == index ? 'active' : '']"><span class="iconfont" :class="item.icon"></span><p>{{ item.title }}</p></router-link></div></div>
    </template>
    <script>
    export default {name: 'Footer',props: {bottomActive: {type: Number,default: 0}   },data() {return {itemList: [{id: 0,title: '电影',icon: 'icon-dianying',href: '/home'},{id: 1,title: '影院',icon: 'icon-yingyuan',href: '/cinema'},{id: 2,title: '我的',icon: 'icon-home',href: '/my'}]}}
    }
    </script><style lang="stylus" scoped>@import '~stylus/variable.styl'.bottomposition: fixed;bottom:0;width: 100%;height: 50px;display: flex;border-top: 1px solid #d6d6d6;background-color: rgba(246,246,246,0.95);.btn-itemtext-decoration: none;flex: 1;font-size:11px;display: flex;flex-direction: column;align-items: center;justify-content: center;atext-decoration: none;color: #999;&.activecolor: $bgColor;.iconfontfont-size: 22px;pmargin-top: 4px;
    </style>
    • 在home组件引入CommonFooter组件

      import CommonFooter from '../../components/footer/Footer'
      components: {
      CommonHeader,
      NavMenu,
      MovieContent,
      CommonFooter
      },

      还需要像Footer组件传递bottomActive,默认让第几个菜单激活。

    至此,home组件已经完成,效果如下: 

Vue2.5从零开发猫眼④——Home页开发相关推荐

  1. (一)移动App开发——Native App-原生开发Web App-网页开发Hybrid App-混合开发网页打包成App四方式-Cordova-APPCan-DCloud-API Cloud

    移动 App 开发 Native App-原生开发 开发技术 原生的 Android 平台 原生的 iOS 平台 JavaScript bridge 用于原生应用中的 Web 和原生平台进行交互. h ...

  2. vue2+vue3小白零基础教程—vue2篇,全网2021最详细教程

    vue教程 提示:Vue3系列请参考Vue2+Vue3小白零基础教程-vue3篇文章,本文为vue2篇. 1. Vue核心 1.1 Vue简介 1.1.1 Vue是什么 一套用于构建用户界面的渐进式J ...

  3. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  4. 构建Koa2 + Vue2.0 前后端分离的多页应用

    换了家公司,技术栈由之前的 React 变成了现在的 Vue.之前也没怎么接触过 Vue,但现在需要搭建一个基于 Vue 的多页面应用,于是就网上搜了一番,顺便记录一下过程. client 配置 Vu ...

  5. .NET 6新特性试用 | 无需配置开发人员异常页

    前言 在.NET 6之前,我们需要在"Startup.cs"文件中手工配置开发人员异常页: if (env.IsDevelopment()) {app.UseDeveloperEx ...

  6. python语言程序设计难不难_零基础学Python编程开发难度大吗?从哪学起?

    转行零基础学Python编程开发难度大吗?从哪学起? 近期很多小伙伴问我,如果自己转行学习Python,完全0基础能否学会呢?Python的难度到底有多大? 今天,小编就来为大家详细解读一下这个问题. ...

  7. 如何学好python基础_零基础如何学好Python开发?

    作为一个零基础小白想学好Python开发应该先确定明确目标.做好学习Python系统规划.选择适合的开发工具.进阶提升学习规划.多练多看加深对Python程序的理解,想入门一门编程语言就需要不断的进行 ...

  8. 零基础转行Web前端开发自学靠谱吗?菜鸟学习前端的方法分享

    Web 前端开发行业长期处于供不应求的状态,薪资待遇也随之水涨船高,因此吸引了不少零基础转业者.那么,可能会有人问了:零基础转行Web 前端开发自学靠谱吗?这个问题没有确切的答案,毕竟每个人的情况都不 ...

  9. 前端开发~uni-app ·[项目-仿糗事百科] 学习笔记 ·009【搜索页开发】

    注:前言.目录见 https://god-excious.blog.csdn.net/article/details/105312456 文章目录 [035]搜索页开发(上) 开发内容 具体方法 核心 ...

最新文章

  1. 【Java源码分析】ArrayList源码分析
  2. 团队作业1——团队展示选题
  3. OVS搭建虚拟机全过程(四十五)
  4. [Music]A Place Nearby
  5. 聚能聊每周精选 第二十三期
  6. mysql配置以及性能优化(转)
  7. clickhouse建库_专访ClickHouse创始人:数据库竞争依旧火热,技术整合势在必行
  8. 咖啡种类、做法和历史来源
  9. Python自动化填写问卷星问卷
  10. 最简单人工智能python_如何从零学会人工智能(AI)Python?
  11. 【Nand2tetris】Project02
  12. android 高仿拼多多,Android 仿京东、拼多多商品分类页
  13. 自学网络结构(一):Neural Architecture Search With Reinforcement Learning
  14. AD domain 环境下VBS自动生成邮件签名
  15. iphone下拉菜单卡住了_为什么苹果手机把主菜单往下拉时就会卡死
  16. 基于X11协议的Linux快捷键管理
  17. Linux-终端命令行
  18. c语言程序设计植树,c语言程序设计报告
  19. 背后的力量 | 推动智慧校园建设 华云数据帮助昆明卫生职业学院重塑IT架构
  20. Simulink模块库介绍(三)——非线性模块

热门文章

  1. 【模块电路】继电器驱动应用
  2. 创建Deployment时指定hostnetwork是干啥的?
  3. 【Linux】wget命令的使用
  4. LASSO近端梯度下降法Proximal Gradient Descent公式推导及代码
  5. Linux运维基本功(肆)---Linux文件管理
  6. HttpClient和HtmlUnit的比较总结以及使用技巧(一)
  7. Android IBinder的linkToDeath介绍
  8. vr属于计算机什么领域,vr技术是什么
  9. VR+文旅景区结合VR主题乐园VR设备厂家
  10. 阿里云ClickHouse海量数据分析