学习小程序开发定制计划第56天,Vue实例(购物车实现)

今天学习了下vue,跟着教程做了一个实例, 分享给大家一起学习。南宁才聚软件:www.zkelm.com
讲解一下思路:
1.所有的数据都存在一个数组中,

services:[
						{
							name:'汉堡包',
							price:300,
							active:false
						},
						{
							name:'薯片',
							price:200,
							active:false
						},
						{
							name:'可乐鸡肉块',
							price:800,
							active:false
						},
						{
							name:'肉卷小甜甜',
							price:500,
							active:false
						}
						
					]

2.使用v-for=“services in services” 循环生成一个 li标签,其中标签之中分别输出 services.name ,services.price,services.active 等值

这个时候就会得到如图
才聚软件:www.zkelm.com
3.制作点击变色

  • 中加入一个class 和 @click事件,做切换active状态
  • <li :class="{ifactive:services.active}" >
    

    然后设置ifactive{background:#e35885} //设置背景颜色
    此时点击li的时候 就会产生变色, 但是这个时候 下方的总价是没有变化的。
    南宁才聚软件:zkelm.com
    4.总价的计算方法:
    使用vue的computer属性来计算total值,代码如下

    total(){
    						var sum=0;
    						this.services.forEach(function(e){
    							if(e.active){
    								sum+=e.price
    							}
    							
    						})
    						return sum;
    					}
    

    这段代码的意思就是:使用forEach()循环找出数组中active==true的数据,然后使用
    sum+=e.price 求出总和

    5.在li元素中加入切换services.active的methods事件

    <li @click="toggleactive(services)"></li>
    

    vue中methods函数如下

    new Vue({
       el:"#main",
       data:{},
       methods:{
           toggleactive(e){
                  e.active=!e.active
             }
      }
    })
    

    这样子就达到了取反的效果, 点一下是选择,在点一下就是不选中。 然后触动computer事件计算出结果
    南宁才聚软件科技有限公司:www.zkelm.com

    这里贴出完整的代码:

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>购物车实例2</title>
    		<script src="https://unpkg.com/vue/dist/vue.js"></script>
    		<style type="text/css">
    		   
    			.liactive{
    				background-color: #e35885 !important;
    			}			
    			#main{
    				width: 700px;
    				border:1px solid #3399EE;
    				position: absolute;
    				background-color: #61a1bc;
    				
    			}
    			#main ul{
    				padding: 0;
    			}
    			#main h1{
    				margin: 10px auto;
    				width: 100%;
    				text-align: center;
    				font-size: 60px;
    				font-weight:800;
    				color: #fff;
    			}
    			#main ul li{
    				list-style-type: none;
    				margin:5px 10px;
    				padding:15px 30px;
    				font-size: 26px;
    				font-weight:900;
    				color: #fff;
    				position: relative;
    				width: 400px;
    				margin: 10px auto;
    				background-color: #8ec16d;
    				border-radius: 5px;
    				
    			}
    			#main ul li span{
    				float: right;
    				width: 100px;	
    				text-align: right;
    				margin-left: 100px;
    				
    			}
    			#main ul:after{
    				content: '';
    				border-bottom: 1px solid #fff;
    				width: 450px;
    				display: block;
    				
    				margin: 30px auto;
    			}
    			.total{
    				display: block;
    				font-size: 30px;
    				font-weight: 900;
    				color:#fff;
    				text-align: left;
    				margin-left: 130px;
    				margin-bottom: 100px;
    			}
    			.total span{
    			
    				float: right;
    				margin-right: 150px;
    			}
    		</style>
    	</head>
    	<body>
    		<div id="app">
    			<form id="main">
    				<h1>Service</h1>
    				<ul>
    					<li :class="{liactive:service.active}" v-for="service in services" @click=toggleActive(service)>
    						{{service.name}}<span>{{service.price | currency}}</span>
    					</li>
    				</ul>
    				<div class="total">
    					Total:<span>{{total() | currency}}</span>
    				</div>
    			</form>
    		</div>
    		<script type="text/javascript">
    			new Vue({
    				el:"#main",
    				data:{
    					services:[
    						{
    							name:'汉堡包',
    							price:300,
    							active:false
    						},
    						{
    							name:'薯片',
    							price:200,
    							active:false
    						},
    						{
    							name:'可乐鸡肉块',
    							price:800,
    							active:false
    						},
    						{
    							name:'肉卷小甜甜',
    							price:500,
    							active:false
    						}
    						
    					]
    				},
    				methods:{
    					toggleActive(item){
    						item.active=!item.active
    					},
    					total(){
    						var sum=0;
    						this.services.forEach(function(e){
    							if(e.active){
    								sum+=e.price
    							}
    							
    						})
    						return sum;
    					}
    				},
    				filters:{
    						currency:function(value){
    							return '$'+value.toFixed(2);
    						}
    					}
    				
    			})
    		</script>
    	</body>
    </html>
    
    

    南宁才聚软件专业做小程序开发定制,欢迎一起学习交流:www.zkelm.com

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的利用kbone实现小程序与Web端同构的代码实例: 首先,安装kbone和相关依赖: ``` npm install kbone kbone-runtime vue vue-router vuex ``` 然后,在Vue3应用中添加kbone的配置: ```js // main.js import Vue from 'vue' import VueRouter from 'vue-router' import Vuex from 'vuex' import App from './App.vue' import routes from './router' import store from './store' Vue.use(VueRouter) Vue.use(Vuex) // 创建 router 和 store 实例 const router = new VueRouter({ mode: 'history', routes }) const store = new Vuex.Store(store) // 将 Vue 实例挂载到 DOM 上 new Vue({ router, store, render: h => h(App) }).$mount('#app') // kbone 配置 export { router, store } ``` 接下来,创建一个kbone的配置文件,将Vue3应用转换为小程序可用的代码: ```js // kbone.config.js module.exports = { origin: 'https://test.miniprogram.com', entry: '/', router: { home: [ '/(home|index)?', '/test/(home|index)', ], list: [ '/list/:id', '/test/list/:id', ], detail: [ '/detail/:id', '/test/detail/:id', ], }, redirect: { notFound: 'home', accessDenied: 'home', }, generate: { app: 'noemit', appWxss: 'default', tabBar: 'custom', }, app: { backgroundTextStyle: 'dark', navigationBarTextStyle: 'white', navigationBarTitleText: 'kbone', }, appExtraConfig: { sitemapLocation: 'sitemap.json', }, global: { rem: true, share: true, windowScroll: false, backgroundColor: '#F7F7F7', }, pages: { home: { extra: { navigationBarTitleText: '首页', }, }, list: { extra: { navigationBarTitleText: '列表页', }, }, detail: { extra: { navigationBarTitleText: '详情页', }, }, }, } ``` 最后,使用kbone命令将Vue3应用转换为小程序可用的代码: ``` npx kbone-cli build --type wx --watch ``` 这样,就可以实现一个简单的利用kbone实现小程序与Web端同构的应用。在实践中,还需要根据具体的需求进行更加复杂的配置和开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值