鸿蒙模拟当当网底部导航栏的功能【华为根技术】

举报
tea_year 发表于 2025/07/30 17:22:37 2025/07/30
【摘要】 首页Index.etsimport { BottomNav } from '../common/BottomNav'import { Buy } from './Buy'import { Cart } from './Cart'import { Category } from './Category'import { Detail } from './Detail'import { My }...

首页Index.ets

import { BottomNav } from '../common/BottomNav'
import { Buy } from './Buy'
import { Cart } from './Cart'
import { Category } from './Category'
import { Detail } from './Detail'
import { My } from './My'

@Entry
@Component
struct Index {
  // @State message: string = 'Hello World';
  @Provide("pageInfos") pageInfos: NavPathStack = new NavPathStack()

  @Builder
  PageMap(name: string) {
    if (name === 'category') {
      Category()
    } else if (name === 'buy') {
      Buy()
    } else if (name === "cart") {
      Cart()
    } else if (name === 'my') {
      My()
    } else if (name === 'detail') {
      Detail()
    }
  }

  @Styles bk(){
    .border({width: 1, color: '#f00'})
  }
  aboutToAppear(): void {
    //关闭导航转场的效果
    this.pageInfos.disableAnimation(true)
  }
  build() {
    Navigation(this.pageInfos){
      //1. 内容区
      Scroll(){
        Column(){
          Text("首页的内容区")
        }
        .height('130%')
        .width('80%')
        .bk()
      }
      .layoutWeight(1)

      //2. 底部导航
      BottomNav({activeIndex: 0})
    }
    .navDestination(this.PageMap)
    .hideTitleBar(true)
    .hideToolBar(true)
  }
}

底部导航数据BottomNavModel.ets

ets > model > BottomNavModel.ets
export interface NavItemModel {
  unselectedImg: string,
  selectedImg: string,
  title: string,
  name: string
}

export const BottomNavData: NavItemModel[] = [
  {
    unselectedImg: 'home',
    selectedImg: 'activeHome',
    title: '首页',
    name: ''
  },
  {
    unselectedImg: 'star',
    selectedImg: 'activeStar',
    title: '分类',
    name: 'category'
  },

  {
    unselectedImg: 'message',
    selectedImg: 'activeMessage',
    title: '值得买',
    name: 'buy'
  },
  {
    unselectedImg: 'people',
    selectedImg: 'activePeople',
    title: '购物车',
    name: 'cart'
  },
  {
    unselectedImg: 'star',
    selectedImg: 'activeStar',
    title: '个人中心',
    name: 'my'
  }
]

底部导航组件BottomNav.ets

ets > common > BottomNav.ets
import { BottomNavData, NavItemModel } from "../model/BottomNavModel"

@Component
export struct BottomNav {
  @Consume("pageInfos") pageInfos: NavPathStack
  @State navList: NavItemModel[] = []
  @Prop activeIndex: number = 0

  aboutToAppear(): void {
    this.navList = BottomNavData
  }
  build() {
    Row() {
      ForEach(this.navList,(item: NavItemModel,index: number)=>{
        Column() {
          if (this.activeIndex === index){
            Image($r(`app.media.${item.selectedImg}`))
              .width(20)
              .height(20)
          }else{
            Image($r(`app.media.${item.unselectedImg}`))
              .width(20)
              .height(20)
          }
          Text(item.title)
        }
        .onClick(()=>{
           if (index === 0){
             this.pageInfos.clear()
           }else{
             this.pageInfos.pushPath({name: item.name})
           }
        })
      }, (item:NavItemModel)=>item.title)

    }
    .justifyContent(FlexAlign.SpaceAround)
    .width('100%')
    .height(50)
    .border({width: 1})
  }
}

分类页面Category.ets

import { BottomNav } from "../common/BottomNav"

@Component
export struct Category {
  build() {
    NavDestination(){
      Column(){
        Text("分类页面")
      }
      .layoutWeight(1)
      BottomNav({activeIndex: 1})
    }
  }
}

值得买页面Buy.ets

import { BottomNav } from "../common/BottomNav"

@Component
export struct Buy{
  build() {
    NavDestination(){
      Column(){
        Text("值得买页面")
      }
      .layoutWeight(1)
      BottomNav({activeIndex: 2})
    }
  }
}

购物车页面Cart.ets

import { BottomNav } from "../common/BottomNav"

@Component
export struct Cart{
  build() {
    NavDestination(){
      Column(){
        Text("购物车页面")
      }
      .layoutWeight(1)
      BottomNav({activeIndex: 3})
    }
  }
}

个人中心页面My.ets

import { BottomNav } from "../common/BottomNav"

@Component
export struct My{
  build() {
    NavDestination(){
      Column(){
        Text("个人中心页面")
      }
      .layoutWeight(1)
      BottomNav({activeIndex: 4})
    }
  }
}

商品详情页面Detail.ets

@Component
export struct Detail{
  build() {
    NavDestination(){
      Column(){
        Text("个人中心页面")
      }
    }
  }
}
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。