鸿蒙js开发8 鸿蒙的商品购物车及参数传递 原创

发布于 2021-1-28 01:59
浏览
0收藏

本文初步的实现了商品添加到购物车和跳转购物车页面时参数的传递

效果图:

鸿蒙js开发8 鸿蒙的商品购物车及参数传递-开源基础软件社区

视图及样式:

<div class="container">
    <list class="listdiv">
        <block for="{{productlists}}">
            <list-item class="list-item">
                <text>{{$item.pname}}</text>
                <text>{{$item.price}}</text>
                <image class="imgdiv" src="/common/add.png" onclick="addproduct({{$idx}})"></image>
            </list-item>
        </block>
    </list>

    <div class="shopcardiv">
        <div class="shopcar" onclick="showshopcar">
            <image class="shopcarimg" src="/common/shopcar0.png"></image>
        </div>
        <!--初始不出现-->
        <div if="{{count>0?true:false}}" class="countdiv">
            <text class="t1">{{count}}</text>
        </div>
    </div>
</div>
.container {
    width: 100%;
    height: 1300px;
    background-color: snow;
    display: flex;
    flex-direction: column;
}
.listdiv{
    width: 100%;
    height: 90%;
}
.list-item{
    width: 100%;
    height: 20%;
    border-bottom: 1px solid silver;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}
.imgdiv{
    width: 60px;
    height: 60px;
}
.shopcardiv{
    width: 100%;
    height: 9%;
    border-top: 1px solid darkolivegreen;
    position: fixed;
    bottom: 0px;
    left: 0px;
}
.shopcar{
    width: 80px;
    height: 80px;
    background-color: green;
    border-radius: 100px;
    position: absolute;
    left: 300px;
    bottom: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.shopcarimg{
    width: 60px;
    height: 60px;
}
.countdiv{
    position: absolute;
    left: 350px;
    bottom: 110px;
    background-color: red;
    padding: 10px 20px;
    border-radius: 100px;
}
.t1{
    color: white;
    font-weight: bold;
}

业务逻辑层:

import prompt from '@system.prompt';
import router from '@system.router';
export default {
    data: {
        productlists:[{"pname":"P40","price":"¥4500"},
                      {"pname":"手环","price":"¥199"},
                      {"pname":"手表","price":"¥1388"},
                      {"pname":"电脑","price":"¥9999"},
                      {"pname":"耳机","price":"¥1099"},
                      {"pname":"Pad","price":"¥4999"},
                      {"pname":"智慧屏","price":"¥3299"}],
        count:0,

        //购物车商品
        carproduct:[]

    },
    addproduct(index){
        ++this.count;

        //商品记录到购物车中
        this.carproduct.push(this.productlists[index]);
    },

    showshopcar(){

        //
        prompt.showToast({
           message:this.carproduct
        });

        //鸿蒙带参数跳转页面
        router.push({
            uri:'pages/productcar/productcar',
            params:{
                carlists:this.carproduct
            }
        });
    }
}

选择商品后跳转到购物车页面:

鸿蒙js开发8 鸿蒙的商品购物车及参数传递-开源基础软件社区

鸿蒙页面跳转接收参数的方式:定义数据模型名称时和参数的名字一致即可

export default {
    data: {
        title:"购物车列表",
        //接收页面跳转时带的参数方式:定义名称和参数的名字一致即可
        carlists:[]
    }
}

视图及样式:

<div class="container">
    <div class="topdiv">
        <text>{{title}}</text>
    </div>
    <list class="listdiv">
        <block for="{{carlists}}">
            <list-item class="list-item">
                <text>{{$item.pname}}</text>
            </list-item>
        </block>
    </list>
</div>
.container {
    width: 100%;
    height: 1300px;
    display: flex;
    flex-direction: column;
}
.topdiv{
    width: 100%;
    height: 10%;
    border-bottom: 1px solid black;
    display: flex;
    justify-content: center;
    align-items: center;
}
.listdiv{
    width: 100%;
    height: 90%;
}
.list-item{
    width: 100%;
    height: 15%;
    border-bottom: 1px solid silver;
    display: flex;
    justify-content: center;
    align-items: center;
}

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
1
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐