鸿蒙应用ets之仿黑马智慧商城分类页

黑马vue2项目的智慧商城,自己仿写一个鸿蒙版的


前言

黑马的接口调用基础地址更换了,之前的接口需要更换

需要用到黑马智慧商城接口。地址:wiki - 智慧商城-实战项目 (apifox.com)


一、分析布局

总体为纵向布局,分为头部和内容区

内容区左边框框为一个可以滚动的菜单栏

内容区右边框框为一个展示主菜单选项中的子选项内容(可以使用Flex或者Grid布局展示)

二、编写UI界面

1.头部块(红色框内容)

 //头部文字
        Row() {
          Text('全部分类')
            .width('100%')
            .fontSize(25)
            .fontWeight(800)
            .fontColor(Color.Black)
            .textAlign(TextAlign.Center)
        }.width('100%')
      //顶部搜索栏
        Row() {
          Search({ placeholder: '搜索商品' })
            .width('90%')
        }.width('100%').justifyContent(FlexAlign.Center)

2.主菜单块(黄色框内容)

//左边主菜单选项
        Column(){
          List(){
            //渲染主菜单名字
            ForEach(this.allGoodsData,(item:string,index:number)=>{
              ListItem(){
                Text(item)
                  .width('100%').height(50)
                  .fontColor(this.backColor[index === this.currentIndex ? 1 : 0])
                  .fontSize(20).fontWeight(700).textAlign(TextAlign.Center)
              }.padding({top:10})
              .onClick(()=>{
                //当用户点击其他主菜单选项的时候将索引赋值
                this.currentIndex = index
                //这里可以判断后台获取的数据是否包含子选项,我这里偷懒直接定义静态的了
                this.i = index
                if(this.i>7){
                  //索引大于7的主菜单选项没有子选项内容,那么就在子菜单内容展示无内容
                  this.flag = true
                }else{
                  this.flag = false
                }
                //清空子菜单数组内容,便于后面重新赋值
                this.goodsInfo = []
                //更新子菜单内容
                this.updateInfo()
              })
            })
          }.backgroundColor("#ffedefef")
        }.width('30%').height('100%')

3.子选项块(蓝色框内容)

//右边子选项
        Column(){
          //判断用户点击的主菜单获取的数据,子选项是否有数据
          if(!this.flag){//有数据,则渲染数据
            Flex({wrap:FlexWrap.Wrap,justifyContent:FlexAlign.Center}){
              ForEach(this.goodsInfo,(item:any,index:number)=>{
                Column(){
                  Image(item.url)
                    .width('30%').height(100).objectFit(ImageFit.Contain)
                  Text(item.name).fontSize(12).fontWeight(600)
                }
              })
            }.padding('2%')
          }else {//没有数据,展示无数据提示
            Text('暂时没有数据').fontSize(30).fontWeight(800)
          }
        }.width('70%').height('100%')

三 、发送请求获取数据

1.页面出现之前的时候执行

获取默认内容

 //获取分类页的后台数据
  async getGoodsClass(){
    //发送请求
    const data = await axios.get('http://smart-shop.itheima.net/index.php?s=/api/category/list')
    //左边
    const len1 = data.data.data.list.length//获取数据的长度,便于下面渲染
    const category = data.data.data.list//简化
    //循环,将后台数据中的主菜单内容添加到数组中
    for (let i = 0; i < len1; i++) {
      this.allGoodsData.push(category[i].name)
    }
    //右边
    const len2 = category[this.i].children.length//获取每一个主菜单对应的子选项的长度
    //循环将对应的主菜单的子选项的内容添加到数组中
    for (let i = 0; i < len2; i++) {
      var item = {
        name:category[this.i].children[i].name,
        url:category[this.i].children[i].image.preview_url
      }
      this.goodsInfo.push(item)
    }
  }

2.更新主菜单对应的子选项

当用户点击不同的主菜单的时候,需要刷新子选项

//更新对应的商品信息
  async updateInfo(){
    const data = await axios.get('http://smart-shop.itheima.net/index.php?s=/api/category/list')
    const category = data.data.data.list
    const len2 = category[this.i].children.length
      for (let i = 0; i < len2; i++) {
        var item = {
          name:category[this.i].children[i].name,
          url:category[this.i].children[i].image.preview_url
        }
        this.goodsInfo.push(item)
      }
  }

总结

该页面主要实现主菜单的滑动,点击然后展示不同类别的子选项内容

完整代码如下:

import axios from '@ohos/axios'
@Component
export struct More{

  //存放商品总列表的数组
  @State allGoodsData:string[]=[]
  //存放分类商品的信息数组
  @State goodsInfo:any[]=[]
  //默认查询分类商品组
  @State i:number = 0
  //背景颜色数组
  @State backColor:string[]=["#ff868585", "#ffff0000"]
  //用户点击现在的索引
  @State currentIndex:number = 0
  //判断是否有商品信息的标志
  @State flag:boolean = false

 aboutToAppear(){
    this.getGoodsClass()
  }

  //获取分类页的后台数据
  async getGoodsClass(){
    //发送请求
    const data = await axios.get('http://smart-shop.itheima.net/index.php?s=/api/category/list')
    //左边
    const len1 = data.data.data.list.length//获取数据的长度,便于下面渲染
    const category = data.data.data.list//简化
    //循环,将后台数据中的主菜单内容添加到数组中
    for (let i = 0; i < len1; i++) {
      this.allGoodsData.push(category[i].name)
    }
    //右边
    const len2 = category[this.i].children.length//获取每一个主菜单对应的子选项的长度
    //循环将对应的主菜单的子选项的内容添加到数组中
    for (let i = 0; i < len2; i++) {
      var item = {
        name:category[this.i].children[i].name,
        url:category[this.i].children[i].image.preview_url
      }
      this.goodsInfo.push(item)
    }
  }

  //更新对应的商品信息
  async updateInfo(){
    const data = await axios.get('http://smart-shop.itheima.net/index.php?s=/api/category/list')
    const category = data.data.data.list
    const len2 = category[this.i].children.length
      for (let i = 0; i < len2; i++) {
        var item = {
          name:category[this.i].children[i].name,
          url:category[this.i].children[i].image.preview_url
        }
        this.goodsInfo.push(item)
      }
  }


  build(){
    //总体纵向布局
    Column(){
      //头部文字
        Row() {
          Text('全部分类')
            .width('100%')
            .fontSize(25)
            .fontWeight(800)
            .fontColor(Color.Black)
            .textAlign(TextAlign.Center)
        }.width('100%')
      //顶部搜索栏
        Row() {
          Search({ placeholder: '搜索商品' })
            .width('90%')
        }.width('100%').justifyContent(FlexAlign.Center)

      Row(){
        //左边主菜单选项
        Column(){
          List(){
            //渲染主菜单名字
            ForEach(this.allGoodsData,(item:string,index:number)=>{
              ListItem(){
                Text(item)
                  .width('100%').height(50)
                  .fontColor(this.backColor[index === this.currentIndex ? 1 : 0])
                  .fontSize(20).fontWeight(700).textAlign(TextAlign.Center)
              }.padding({top:10})
              .onClick(()=>{
                //当用户点击其他主菜单选项的时候将索引赋值
                this.currentIndex = index
                //这里可以判断后台获取的数据是否包含子选项,我这里偷懒直接定义静态的了
                this.i = index
                if(this.i>7){
                  //索引大于7的主菜单选项没有子选项内容,那么就在子菜单内容展示无内容
                  this.flag = true
                }else{
                  this.flag = false
                }
                //清空子菜单数组内容,便于后面重新赋值
                this.goodsInfo = []
                //更新子菜单内容
                this.updateInfo()
              })
            })
          }.backgroundColor("#ffedefef")
        }.width('30%').height('100%')
        //右边子选项
        Column(){
          //判断用户点击的主菜单获取的数据,子选项是否有数据
          if(!this.flag){//有数据,则渲染数据
            Flex({wrap:FlexWrap.Wrap,justifyContent:FlexAlign.Center}){
              ForEach(this.goodsInfo,(item:any,index:number)=>{
                Column(){
                  Image(item.url)
                    .width('30%').height(100).objectFit(ImageFit.Contain)
                  Text(item.name).fontSize(12).fontWeight(600)
                }
              })
            }.padding('2%')
          }else {//没有数据,展示无数据提示
            Text('暂时没有数据').fontSize(30).fontWeight(800)
          }
        }.width('70%').height('100%')
      }.width('100%').layoutWeight(1)
    }
    .width('100%').height('100%')
  }
}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ETS(Easy Template System)是CSDN开发的一套模板引擎,用于前端面开发。在鸿蒙系统中,ETS也是可以使用的,以下是一个简单的例子,演示如何使用ETS实现面跳转。 1. 在项目中创建一个ETS模板文件,例如"index.ets",并在其中添加一个面跳转的按钮: ``` <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>Welcome to my website</h1> <button onclick="jumpToPage()">Jump to new page</button> <script type="text/javascript"> function jumpToPage() { // 面跳转逻辑 } </script> </body> </html> ``` 2. 在鸿蒙应用的JS文件中导入ETS模板引擎,并使用它来渲染面: ``` import { createElement } from '@vue/composition-api'; import Ets from '@system.ets'; export default { data: { ets: null, }, onInit() { this.ets = new Ets({ baseUrl: '/common/', // ETS模板所在的目录 }); // 渲染面 this.ets.render('index.ets', {}, (html) => { const container = createElement('div'); container.setInnerHTML(html); this.$refs.page.setContent(container); }); }, }; ``` 3. 在面跳转函数中,使用鸿蒙提供的路由API来进行面跳转: ``` <script type="text/javascript"> import router from '@system.router'; function jumpToPage() { router.push({ uri: '/pages/new-page/new-page', }); } </script> ``` 在上面的例子中,我们通过ETS模板引擎和鸿蒙提供的路由API,实现了一个简单的面跳转功能。当用户点击面上的按钮时,应用会跳转到名为"new-page"的面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值