基于vue来开发一个仿饿了么的外卖商城(一)

一、准备工作

1.大前提:已安装好node、 npm、 vue、 vue-cli、stylus(此项目使用stylus来编译)

2.开发软件:Google Chrome(建议安装插件vue-devtools,方便调试),webstorm / sublime Text / VS Code (推荐使用webstorm,sublime 和 VS Code需要安装相应的插件)

3.项目结构

4.项目结构分析

5. 图标准备

推荐在阿里巴巴矢量图库查找需要的图标,官网地址:https://www.iconfont.cn

使用步骤:

a.注册用户

b.按照项目需求将相应的图标添加到库(购物车)

c.点击购物车,将图标添加到项目中

d.选中Font-class,此时会出现相应的代码连接,将链接复制到项目的index.html中,如下所示:

<link href="//at.alicdn.com/t/font_955721_h2wm4c3aixr.css" rel="stylesheet">

此时在编辑vue文件时可以直接使用相应的class=“iconfont icon-xxx”来获取相应的图标。示例:

<span>
    <i class="iconfont icon-shangcheng"></i>
</span>

6.编译基础样式

reset.css

二、移动端配置与基础路由配置

1.使编辑页面自适应手机屏幕(解决双击缩放)

<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<link rel="stylesheet" href="./static/css/reset.css">

2.解决点击响应的延时0.3s问题

300ms延迟问题:移动端的双击缩放

解决方法:使用fastclick,具体查看链接:http://www.cnblogs.com/lyyguniang/p/9284968.html

在index.html中编辑

<script type=‘application/javascript‘ src=‘https://as.alipayobjects.com/g/component/fastclick/1.0.6/fastclick.js‘></script>
<script>
      if(‘addEventListener‘ in document) {
        document.addEventListener(‘DOMContentLoaded‘,function(){
          FastClick.attach(document.body);
        },false);
      }
    </script>

3.使用vue-router编辑基础路由

参考链接:http://www.cnblogs.com/SamWeb/p/6610733.html

首先下载vue-router

npm install vue-router --save

index.js

// 导入模块
import Vue from ‘vue‘
import Router from ‘vue-router‘

// 引入组件
import Msite from ‘../pages/Msite/Msite.vue‘
import Search from ‘../pages/Search/Search.vue‘
import Order from ‘../pages/Order/Order.vue‘
import Profile from ‘../pages/Profile/Profile.vue‘

// 告诉vue使用vueRouter
Vue.use(Router)

// routes是路由的数组,有两部分组成:path和 component,最后由router来管理路由
export default new Router({
  routes: [
    {
      path: ‘/‘,
      redirect: ‘/msite‘
    },
    {
      path: ‘/msite‘,
      component: Msite
    },
    {
      path: ‘/search‘,
      component: Search
    },
    {
      path: ‘/order‘,
      component: Order
    },
    {
      path: ‘/profile‘,
      component: Profile
    }
  ]
})

main.js

import Vue from ‘vue‘
import App from ‘./App‘
// 引用路由
import router from ‘./router‘

/* eslint-disable no-new */
new Vue({
  el: ‘#app‘,
  render: h => h(App),
  router //将路由注入到跟实例中
})

问:什么是render: h=> h(App)

查看链接:https://segmentfault.com/q/1010000007130348

vue-router包含router-link和router-view,前者负责点击跳转,后者负责页面渲染。在饿了么app中,是点击底部导航来跳转到相应的路由页面的,此时在app.vue中导入底部导航组件,当点击相应的图标时,跳转路由

App.vue

<template>
    <div id="app">
        <router-view/>
        <FooterGuide></FooterGuide>
    </div>
</template>

<script>
import FooterGuide from ‘./components/FooterGuide/FooterGuide.vue‘
export default {
  components: {
    FooterGuide
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
    .app
        width 100%
        height 100%
        background #f5f5f5
</style>

三、编辑底部导航FooterGuide组件

技术点:用v-on:click,$route实现点击相应图标跳转路由

$route.path和class实现点击相应的图标的时候,图标样式动态改变

问:$router和$route的区别

查看链接:https://www.jianshu.com/p/fa0b5d919615

示例:

       <div class="guide_item" :class="{on: ‘/msite‘===$route.path}" @click="goto(‘/msite‘)">
            <span class="item_icon">
                <i class="iconfont icon-changyonglogo40"></i>
            </span>
            <span>首页</span>
        </div>    

<script>
export default {
  methods: {
    goto (path) {
      this.$router.replace(path)
    }
  }
}
</script>

四、各路由静态页面的编辑

提示:在首页中使用swiper来实现图片轮播

查看官方文档:https://www.swiper.com.cn/

首先npm install swiper --save

接着编写html,大体格式如下:

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
</div>

编写JavaScript

<script>
import Swiper from ‘swiper‘
import ‘swiper/dist/css/swiper.min.css‘
export default {
  mounted () {
    /* eslint-disable no-new */
    new Swiper(‘.swiper-container‘, {
      pagination: {
        el: ‘.swiper-pagination‘
      },
      loop: true
    })
  }
}
</script>

注意:这个时候运行会有一个报错

报错:Do not use ‘new‘ for side effects

解决方法:在new上加上注释 /* eslint-disable no-new */

查看链接:https://www.jianshu.com/p/3a7982110656

五、最后运行npm run dev

六、各种注意点归纳

1.vue运行的时候报错: Parsing error:x-invalid-end-tag

解决方法:https://blog.csdn.net/zy13608089849/article/details/79545738

2.报错:Newline required at end of file but not found

原因:编辑style时底部要空多一行

解决方法:https://www.cnblogs.com/qingqingzou-143/p/7067604.html

3.报错:Component template should contain exactly one root element.

解决方法:https://segmentfault.com/q/1010000008361637

4.在编写stylus样式的时候,切记按着格式编写,如果运行的时候没有报错,但是样式没有显示,估计就是格式编写错误,注意空格

最后附上项目源码:

https://github.com/xinhua6/gshop.git

之后会逐步完善该项目,敬请期待。

原文地址:https://www.cnblogs.com/lanhuo666/p/10095462.html

时间: 2024-07-31 17:47:37

基于vue来开发一个仿饿了么的外卖商城(一)的相关文章

基于vue来开发一个仿饿了么的外卖商城(二)

一.抽出头部作为一个组件,在底部导航的时候可以相应的显示不同的标题 技术点:使用slot进行组件间的通信:父组件给子组件传值(子组件里面通过props接收父组件传过来的数据) 查看链接:https://blog.csdn.net/sinat_17775997/article/details/52484072 //components/HeaderTop/HeaderTop.vue <template> <header class="header"> <sl

基于Vue框架开发的仿饿了么前端小应用

主要使用vue框架进行开发.使用最新的框架版本,修正了vue1.0到vue2.0过度过程出现的几处bug. 视频教程则是黄轶老师的<vuejs高仿饿了么APP>. 源代码地址:https://github.com/waihoyu/sell 原文地址:https://www.cnblogs.com/waihoyu/p/9350175.html

基于Vue 2.0高仿 &lt;今日头条&gt; 单页应用。

这是用 vue.js 2.0 高仿 今日头条 的移动端项目,结合了原生app的部分功能以及网页版. 技术栈 vue.js 2.0全家桶(vue.vuex.vue-router) axios.jsonp element-ui.iview vue-lazyload.animate.css.moment.flexible.js 在线地址 线上地址(预览地址) GitHub源码地址 说明 项目内定死 账号: admin, 密码: admin. 因为数据原因,首页请求的数据接口来自网页版今日头条,修改了一

使用vue来开发一个下拉菜单组件(1)

一.新建demo工程 vue init webpack-simple demo 添加src/mixins/emitter.js文件(见前几篇博文) 安装font-awesome字体库: cnpm install font-awesome --save 配置webpack.config.js,引入字体文件: { test: /\.(otf|eot|ttf|woff|woff2)$/, loader: 'file-loader' } 在src/main.js中引入font-awesome: impo

【Vue.js】高仿饿了么外卖App(一)

1.架构从传统的MVC向REST API+前端MV*迁移 参考链接: http://blog.csdn.net/broadview2006/article/details/8615055 http://blog.csdn.net/u013628152/article/details/42709033 MV*包括:MVC.MVP.MVVM vue.js是MVVM框架 2.Iconmoon制作图标字体 2.1 点击IconMoon App 2.2点击import icons,导入svg图片 2.3选

Yii 2.0开发一个仿京东商城平台

课程目录及下载地址: 第1章 课程简介介绍了课程内容.背景和案例展示.第2章 项目的准备工作介绍了如何使用PHP依赖管理工具Composer安装Yii2框架,模拟配置真实企业开发项目运行环境和编辑器.第3章 项目前台页面的搭建介绍了如何使用Yii2框架的页面布局管理完成前台首页.商品分类.商品详情.购物车.收银台.订单中心.用户注册登录页面的搭建和渲染.第4章 项目后台页面的搭建介绍了如何使用Yii2框架的脚手架Gii模块创建后台功能模块,完成后台页面的搭建及前后台页面的调优处理.第5章 管理员

基于vue脚手架开发

1.安装node 2.安装淘宝镜像 //国内镜像npm下载速度会比较快npm install cnpm -g --registry=https://registry.npm.taobao.org安装完成: cnpm -v3.安装vue-cli 4.vue init webpack5.npm install //此步骤尽量不要用cnpm 原文地址:https://www.cnblogs.com/blog-note/p/12132149.html

用vue + leancloud开发一个免费的博客

项目地址 https://github.com/Fee-ing/Fe... 在线预览 在线预览地址: 搭建免费博客 原文地址:https://www.cnblogs.com/baimeishaoxia/p/12210615.html

VBox 一款基于vue开发的音乐盒 序章

之前自己基于vue写了一个 Mplayer, github地址:https://github.com/xiangwenhu/MPlaer, 演示地址:http://babydairy2017.cloudapp.net:3000/index#/index 只有简单的搜索播放随心听功能,样式适配上也很差, 路由就两个,数据都走vuex,数据转发是用nodejs自己转发,而且只是PC能比较好的展现,之后不了了之,偶尔也自己上来听听歌曲. 有一天,抬头看,有一片云,特别的蓝,风一吹,一阵花香飘来,美好,