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

基于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">
View Code

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

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

 

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">
View Code

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

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
    
  ]
)
View Code

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 //将路由注入到跟实例中
)
View Code

问:什么是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>
View Code

 

三、编辑底部导航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>    
View Code
<script>
export default 
  methods: 
    goto (path) 
      this.$router.replace(path)
    
  

</script>
View Code

 

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

 提示:在首页中使用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>
View Code

编写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>
View Code

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

报错: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

 

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

相关内容

仿饿了么购物车下单效果

仿饿了么购物车下单效果

前一段由于新项目需要,开发一个类似饿了么购物车下单效果,电商类、外卖类、点餐类项目都可以用的上,废话不多说请看效果。 效果图如下:

主要的功能: 就是左侧展示分类,右侧展示分类下商品的,点击右侧分类下的商品,如果商品是套餐类型的话,点击可以看套餐详情,下单选择完商品后,可以在购物车里面添加或减少商品数量。

主要功能实现: 1:分类及商品及购物车里面商品数量的联动效果 2:底部购物车商品列表 3:选择左侧分类效果 4:添加商品是有0到1,减少商品有1到0动画效果 5:下单动画

1:分类及商品及购物车里面商品数量的联动效果

商品的分类和商品展示分别是两个列表,每一个商品的数量取商品的number、商品分类是取每一个分类下的商品list里面遍历商品取商品数量之和。然后商品列表的 GoodsAdapter里面有一个参数传的是商品分类的CatograyAdapter,当下单时减少或则添加商品时,都会调 MainActivityhandlerCarNum方法。 减少时type传0,添加时传1。通过一个SparseArray,key是商品的product_id,value为商品的下单个数。1:当添加商品都会传入一个商品对象goodsBean,通过product_id取到这个商品对象,如果商品对象为空时,给传入的对象下单数量goodsBean.setNum(1),否则goodsBean.setNum(++i)。2:当减少商品时,通过product_id取到这个商品对象,通过对象取对象的下单数量如果小与2的时候goodsBean.setNum(0),并将该对象从SparseArray中remove否goodsBean.setNum(--i)。最后调update方法更新GoodsAdapter,CatograyAdapter.

 1     public void handlerCarNum(int type, GoodsBean goodsBean, boolean refreshGoodList){
 2         if (type == 0) {
 3             GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
 4             if(temp!=null){
 5                 if(temp.getNum()<2){
 6                     goodsBean.setNum(0);
 7                     selectedList.remove(goodsBean.getProduct_id());
 8                 }else{
 9                     int i =  goodsBean.getNum();
10                     goodsBean.setNum(--i);
11                 }
12             }
13         } else if (type == 1) {
14             GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
15             if(temp==null){
16                 goodsBean.setNum(1);
17                 selectedList.append(goodsBean.getProduct_id(), goodsBean);
18             }else{
19                 int i= goodsBean.getNum();
20                 goodsBean.setNum(++i);
21             }
22         }
23         update(refreshGoodList);
24     }
 1  //刷新布局 总价、购买数量等
 2     private void update(boolean refreshGoodList){
 3         int size = selectedList.size();
 4         int count =0;
 5         for(int i=0;i<size;i++){
 6             GoodsBean item = selectedList.valueAt(i);
 7             count += item.getNum();
 8             totleMoney += item.getNum()*Double.parseDouble(item.getPrice());
 9         }
10         tv_totle_money.setText("¥"+String.valueOf(df.format(totleMoney)));
11         totleMoney = 0.00;
12         if(count<1){
13             bv_unm.setVisibility(View.GONE);
14         }else{
15             bv_unm.setVisibility(View.VISIBLE);
16         }
17 
18         bv_unm.setText(String.valueOf(count));
19 
20         if(productAdapter!=null){
21             productAdapter.notifyDataSetChanged();
22         }
23 
24         if(goodsAdapter!=null){
25             goodsAdapter.notifyDataSetChanged();
26         }
27 
28         if(catograyAdapter!=null){
29             catograyAdapter.notifyDataSetChanged();
30         }
31 
32         if(bottomSheetLayout.isSheetShowing() && selectedList.size()<1){
33             bottomSheetLayout.dismissSheet();
34         }
35     }

2:底部购物车商品列表

点击购物车查看购物车列表时,就是给含有下单商品的HashMap作为参数传入ProductAdapter中,在购物车中添加或减少商品数量同样是调 MainActivityhandlerCarNum方法。 除此之外,购物车列表弹起的效果可以用一个bottomsheet效果。as直接集成 compile'com.flipboard:bottomsheet-core:1.5.1'即可。

3:选择左侧分类效果

由于选择分类时,既要改变分类的背景色也要改变选择分类文字的颜色,就不能直接在xml里面设置了,需要在getview里面根据外面传进来选择的selection与position比较后设置。

1  public void setSelection(int selection) {
2         this.selection = selection;
3     }
1  if (position == selection) {
2             viewholder.tv_catogray.setBackgroundResource(R.drawable.rec_red_left_stroke);
3             viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.black));
4         } else {
5             viewholder.tv_catogray.setBackgroundResource(R.drawable.empty);
6             viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.gray));
7         }

4:添加商品是有0到1,减少商品有1到0动画效果

其实动画效果做起来也不是特别复杂,首先要搞清android的几种动画效果,分为在Android3.0(即API Level11)以前,Android仅支持2种动画:分别是Frame Animation(逐帧动画)和Tween Animation(补间动画),在3.0之后Android支持了一种新的动画系统,称为:Property Animation(属性动画)。 最常用的补间动画最常用的4中效果如下: 渐变透明度动画效果 渐变尺寸伸缩动画效果 画面转换位置移动动画效果 画面转移旋转动画效果

添加商品是有0到1,减少商品有1到0动画效果其实就是利用了translate、rotate水平位移和旋转动画效果。

 1  //显示减号的动画
 2     private Animation getShowAnimation(){
 3         AnimationSet set = new AnimationSet(true);
 4         RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
 5         set.addAnimation(rotate);
 6         TranslateAnimation translate = new TranslateAnimation(
 7                 TranslateAnimation.RELATIVE_TO_SELF,2f
 8                 ,TranslateAnimation.RELATIVE_TO_SELF,0
 9                 ,TranslateAnimation.RELATIVE_TO_SELF,0
10                 ,TranslateAnimation.RELATIVE_TO_SELF,0);
11         set.addAnimation(translate);
12         AlphaAnimation alpha = new AlphaAnimation(0,1);
13         set.addAnimation(alpha);
14         set.setDuration(500);
15         return set;
16     }
17     //隐藏减号的动画
18     private Animation getHiddenAnimation(){
19         AnimationSet set = new AnimationSet(true);
20         RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
21         set.addAnimation(rotate);
22         TranslateAnimation translate = new TranslateAnimation(
23                 TranslateAnimation.RELATIVE_TO_SELF,0
24                 ,TranslateAnimation.RELATIVE_TO_SELF,2f
25                 ,TranslateAnimation.RELATIVE_TO_SELF,0
26                 ,TranslateAnimation.RELATIVE_TO_SELF,0);
27         set.addAnimation(translate);
28         AlphaAnimation alpha = new AlphaAnimation(1,0);
29         set.addAnimation(alpha);
30         set.setDuration(500);
31         return set;
32     }

5:下单动画

下单动画首先要获取添加商品时的初始位置坐标,通过getLocationInWindow方法获取初始位置的心x,y坐标,然后获取添加商品要消失的地方的坐标,一般都选择购物车logo的坐标。然后计算x,y的位移值后,通过TranslateAnimation转换移动效果,x,y同时移动就实现了抛物线的效果。最后不要忘记设置当动画结束的时候隐藏抛过来的小图标。

 1 public void setAnim(final View v, int[] startLocation) {
 2         anim_mask_layout = null;
 3         anim_mask_layout = createAnimLayout();
 4         anim_mask_layout.addView(v);//把动画小球添加到动画层
 5         final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation);
 6         int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标
 7         tv_car.getLocationInWindow(endLocation);
 8         // 计算位移
 9         int endX = 0 - startLocation[0] + 40;// 动画位移的X坐标
10         int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
11 
12         TranslateAnimation translateAnimationX = new TranslateAnimation(0,endX, 0, 0);
13         translateAnimationX.setInterpolator(new LinearInterpolator());
14         translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
15         translateAnimationX.setFillAfter(true);
16 
17         TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
18         translateAnimationY.setInterpolator(new AccelerateInterpolator());
19         translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
20         translateAnimationY.setFillAfter(true);
21 
22         AnimationSet set = new AnimationSet(false);
23         set.setFillAfter(false);
24         set.addAnimation(translateAnimationY);
25         set.addAnimation(translateAnimationX);
26         set.setDuration(800);// 动画的执行时间
27         view.startAnimation(set);
28         // 动画监听事件
29         set.setAnimationListener(new Animation.AnimationListener() {
30             // 动画的开始
31             @Override
32             public void onAnimationStart(Animation animation) {
33                 v.setVisibility(View.VISIBLE);
34             }
35 
36             @Override
37             public void onAnimationRepeat(Animation animation) {
38                 // TODO Auto-generated method stub
39             }
40 
41             // 动画的结束
42             @Override
43             public void onAnimationEnd(Animation animation) {
44                 v.setVisibility(View.GONE);
45             }
46         });
47 
48     }

最后肯定少不了源码下载地址:shopcar

赞(1)

文章来源于网络,原文链接请点击 这里
文章版权归作者所有,如作者不同意请直接联系小编删除。
作者:小小小华