≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

使用vue2实现购物车和地址选配功能

人气:433 时间:2019-04-10

这篇文章主要为大家详细介绍了使用vue2实现购物车和地址选配功能,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

 

首先,vue基础js写法

 

代码如下:


new Vue({
  el:"#app",
  //模型
  data:{
  },
  filters:{
  },
  mounted:function(){
    this.$nextTick(function(){
    //初始化调用
    });
  },
  computed:{
    //实时计算
  },
  methods:{
  }
});

 

v-for

 

代码如下:


<li v-for="(item,index) in productList">
  <div class="item-name">{{item.productName}}</div>
</li>

 

v-model

 

(实时更新)

代码如下:


<input type="text" value="0" disabled v-model="item.productQuantity">
<div class="item-price-total">{{item.productQuantity}}</div>

 

v-bind

 

代码如下:


<a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}">
<!--可通过更改item.checked的值设置是否选中-->
<!--必须用v-bind 不可直接在class里面直接使用{{}}-->
<!--v-bind:class= 可简写为 :class= -->

 

filters过滤器的使用

 

 

1.html引用方式

 

代码如下:


<div class="item-price">{{item.productPrice | money('元')}}</div>

 

2.过滤器

 

代码如下:


filters:{
  formatMoney:function(value,type){
    return "¥"+value.toFixed(2)+ type;
  }
},

 

3.全局过滤器(写在new Vue的外面)

 

代码如下:


Vue.filter("money",function(value,type){
  return "¥"+value.toFixed(2) + type; //保留两位小数 结果eg:¥19.00元
});

调用methods中的方法:

代码如下:


@click="method(param)"
//或者
@click="delFlag=false"
@click="limitNum=addressList.length"

 

computed 实时计算

 

如下:默认显示三条数据,点击more 显示所有

代码如下:


<li v-for="(item,index) in filterAddress">
<div class="shipping-addr-more">
<a class="addr-more-btn up-down-btn" href="javascript:" @click="limitNum=addressList.length">
  more
  <i class="i-up-down">
   <i class="i-up-down-l"></i>
   <i class="i-up-down-r"></i>
  </i>
 </a>
</div>

data:{
    limitNum:3
  },
computed:{
  filterAddress:function(){
    return this.addressList.slice(0,this.limitNum);
  }
},

先提出一两个经典的实例

1.以下实现了对循环卡片的点击 选中

代码如下:


<li v-for="(item,index) in filterAddress" v-bind:class="{'check':index==currentIndex}" 
@click="currentIndex=index">
<!--其中currentIndex在js里需要定义-->

2.以下实现了对固定卡片的点击 选中

代码如下:


<ul>
  <li v-bind:class="{'check':shippingMethod==1}" @click="shippingMethod=1">
   <div class="name">标准配送</div>
   <div class="price">Free</div>
  </li >
  <li v-bind:class="{'check':shippingMethod==2}" @click="shippingMethod=2">
   <div class="name">高级配送</div>
   <div class="price">180</div>
  </li>
 </ul>
 <!--其中shippingMethod在js里需要定义-->

题外话:由于本人小白,学一点是一点,额外记录一下辅助弹出框 遮罩层的写法

代码如下:


<div class="md-overlay" v-if="delFlag"></div>

 

vue2的js语法 贴几个 方便查用

 

 

1.调用后端方法

 

代码如下:


var _this = this;
this.$http.get("data/address.json").then(function(response){
    _this.addressList = response; //这里不能直接用this 此this非彼this 所以只能声明_this
}); 
//以下为ES6写法,就可以直接用this了
let _this = this;  //没用,就放这看看~
this.$http.get("data/cartData.json",{"id":123}).then(res=>{
  this.productList = res.data.result.list;
});

 

2.forEach循环

 

代码如下:


this.productList.forEach(function(item,index){
  if(typeof item.checked == 'undefined'){ 
  //如果item中没有checked属性 在item对象中添加checked属性,值为true
    _this.$set(item,"checked",true);//局部注册
    Vue.set(item,"checked",true);//全局注册
  }
});

附上链接:码云地址vue2_study

 

总结

 

以上所述是小编给大家介绍的使用vue2实现购物车和地址选配功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对四海网网站的支持!

本文来自:http://www.q1010.com/184/5024-0.html

注:关于使用vue2实现购物车和地址选配功能的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • Vue项目中如何引入icon图标
  • Vue实现导出excel表格功能
  • Vue数据监听方法watch的使用
  • 分析VUE 对element-ui中的ElTableColumn扩展
  • Vue.js 表单控件操作小结
  • vue技术分享之你可能不知道的7个秘密
  • vue组件与复用分析
  • 分析Vue-cli webpack移动端自动化构建rem问题
  • vue组件分析之使用slot分发内容
  • vue写一个组件
上一篇:Vue 将后台传过来的带html字段的字符串转换为 HTML
下一篇:Vue实现导出excel表格功能
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。