一、创建.json文件,存储需要用的图片、数据等。

二、轮播图,在界面的文件夹下新建swiper文件夹

.css文件

.cb-swiper-container {
    height: 240px;
}

swiper {
    width: 100%;
}

image {
    width: 100%;
    height: 100%;
    object-fit: fill;
}

.hml文件

<div class="cb-swiper-container">
    <swiper>
        <image for="{{list.slice(0,5)}}" src="{{$item.img}}">
            
        </image>
    </swiper>
</div>

.js文件

export default{
    props:{
        list:{
            type:Array
        }
    },
    onReady(){
    console.log(this.list)
    }
}

二、图标,新建hotcate文件夹

.css文件

.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}

.title {
    font-size: 40px;
    color: #000000;
    opacity: 0.9;
}

@media screen and (device-type: tablet) and (orientation: landscape) {
    .title {
        font-size: 100px;
    }
}

@media screen and (device-type: wearable) {
    .title {
        font-size: 28px;
        color: #FFFFFF;
    }
}

@media screen and (device-type: tv) {
    .container {
        background-image: url("/common/images/Wallpaper.png");
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
    }

    .title {
        font-size: 100px;
        color: #FFFFFF;
    }
}

@media screen and (device-type: phone) and (orientation: landscape) {
    .title {
        font-size: 60px;
    }
}
.cb-hc-container {
    flex-direction: column;
}

.cb-hc-title {
    border-bottom-width: 0.5px;
    border-bottom-color: #eee;
    height: 44px;
}

text {
    font-size: 12px;
    padding-left: 10px;
}

.cb-hc-list {
    flex-wrap: wrap;
    margin-top: 20px;
}

.cb-hc-item {
    width: 33.33333%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
}

image {
    object-fit: cover;
    width: 70px;
    height: 70px;
    border-radius: 8px;
}

text {
    font-size: 14px;
    line-height: 30px;
}

.hml文件

<div class="cb-hc-container">
    <div class="cb-hc-title">
        <text>
            热门分类
        </text>
    </div>
    <div class="cb-hc-list">
        <div class="cb-hc-item" for="{{list}}">
            <image src="{{$item.img}}"></image>
            <text>{{$item.title}}</text>
        </div>
    </div>
</div>

.js文件

// @ts-nocheck
import list from"../../../common/mock/cookbook-hotcat.json"
export default {
    data: {
        list:[]
    },
    onReady() {
        this.list = list
    }
}

三、列表,新建top10文件夹

.css文件

.cb-top-container {
    flex-direction: column;
    background-color: #f5f5f5;
}

.cb-title-title {
    height: 50px;
    padding-top: 5px;
    padding-left: 10px;
    color: #333;
}

text {
    font-size: 12px;
}

.cb-top-body {
    flex-wrap: wrap;
    padding-left: 10px;
    padding-right: 10px;
}

.cb-top-item {
    width: 50%;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: white;
    padding-bottom: 15px;
    margin-bottom: 10px;
    height: 160px;
}

image {
    width: 100%;
    flex: 1;
    object-fit: contain;
}

.cb-top-item-info {
    height: 50px;
    flex-direction: column;
    align-items: center;
}

.cb-top-item-title {
    font-size: 16px;
}

.cb-top-item-subtitle {
    font-size: 12px;
    color: #666;
    font-weight: 100;
}
.cb-top-title {
    border-bottom-width: 0.5px;
    border-bottom-color: #eee;
    height: 44px;
}

.hml文件

<div class="cb-top-container">
    <div class="cb-title-title">
        <text>
            精品好菜
        </text>
    </div>
    <div class="cb-top-body" >
         <div class="cb-top-item" for="{{list.slice(0,10)}}">
             <image src="{{$item.img}}"></image>
             <div class="cb-top-item-info">
                <text class="cb-top-item-subtitle" >
                    {{$item.all_click}}浏览{{$item.favorites}}收藏
                </text>
            </div>
         </div>
    </div>
</div>

.js文件

export default{
    props:{
        list:{
            type:Array
        }
    }
}

四、在主界面引用这些组件

<element name="cb-swiper" src="./swiper/swiper"></element>
<element name="cb-hotcate" src="./hotcate/hotcate"></element>
<element name="cb-top10" src="./top10/top10"></element>
<div class="cb-container">
        <div class="cb-title">
            <text>
                朝闻天下
            </text>
        </div>
        <div class="cb-body">
            <list>
                    <list-item class="cb-body-item">
                        <cb-swiper list = "{{list}}"></cb-swiper>
                    </list-item>
                    <list-item class="cb-body-item">
                        <cb-hotcate></cb-hotcate>
                    </list-item>
                <list-item class="cb-body-item">
                    <cb-top10 list="{{list}}"></cb-top10>
                </list-item>
            </list>
        </div>
</div>

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐