鸿蒙navgation的使用

主页面内容

import { homepage } from '../component/homepage'
import {listpage} from  '../component/listpage'
interface objName{
  age:number,
  name:string,
  id:number
}

@Entry
@Component
struct Index {
  //MytabIndex是我的切换索引
  @State MytabIndex:number=0
  //控制navgation切换
  pathStack:NavPathStack=new NavPathStack()
  tabController:TabsController=new TabsController()
  @State message: string = 'Hello World';
  @Builder
  pageMap(name:string){
    if (name === "home") {
      // pageOneTmp()
      homepage()
    } else if (name === "list") {
      // pageTwoTmp()
      listpage()
    } else if (name === "my") {
      // pageThreeTmp()
    }
  }
  build() {
    Navigation(this.pathStack) {
      Tabs({controller:this.tabController} //通过controller属性实现组件和一个变量的绑定
      //   然后我们就可以通过这个变量操作组件的一些行为
      ){
        TabContent(){
          Column(){
            Text("home")
            Button("跳转home")
              .onClick(()=>{
                const obj:objName={
                  id:1,
                  name:'k',
                  age:18
                }
                this.pathStack.pushPath({
                  // 与上边的pageMap里的name照应
                  name:'home',
                  // 传参
                  param:obj
                })
              })
          }


        }
        TabContent(){
          Text("list")
        }
        TabContent(){
          Text("user")
        }
      }
      .scrollable(false)
    }
    .title("我的页面")
    // .hideTitleBar(true)
    .toolbarConfiguration([
      {
        value:"home",
         icon:$rawfile("images/home.png"),
        activeIcon:$rawfile("images/home_sel.png"),
        action:()=>{
          this.tabController.changeIndex(0)
          this.MytabIndex=0
        },
        status:this.MytabIndex==0?ToolbarItemStatus.ACTIVE:ToolbarItemStatus.NORMAL


      },
      {
        value:"list",
        icon:$rawfile("images/goods.png"),
        activeIcon:$rawfile("images/goods_sel.png"),
        status:this.MytabIndex==1?ToolbarItemStatus.ACTIVE:ToolbarItemStatus.NORMAL,
        action:()=>{
          this.tabController.changeIndex(1)
          this.MytabIndex=1
        }
      },
      {
        value:"my",
        icon:$rawfile("images/user.png"),
        activeIcon:$rawfile("images/user_sel.png"),
        status:this.MytabIndex==2?ToolbarItemStatus.ACTIVE:ToolbarItemStatus.NORMAL,

        action:()=>{
          this.tabController.changeIndex(2)
          this.MytabIndex=2
        }
      }
      ])
    .navDestination(this.pageMap)
  }
}

list页面

@Component
export  struct listpage{
  pathStack:NavPathStack=new NavPathStack()
  build() {
    NavDestination(){
      Column(){
        Text("list")
        Button("回到主页")
          .onClick(()=>{
            // 详情回到首页
            this.pathStack.clear()//清空路由栈 (优先)
            // this.pathStack.popToIndex(-1)  //也可以实现
          })
      }
    }
    .onReady((context)=>{
      // 从上下文中获取pathStack参数,这个数据一定要和第一个的页面的数据对应上
      this.pathStack=context.pathStack
      this.pathStack.getParamByName("list") //可以根据name获取参数
    })

    .title("list")
  }
}

home页面

@Component
export  struct homepage{
  pathStack:NavPathStack=new NavPathStack()
  build() {
    NavDestination(){
      Column(){
        Text("home")
        Button("tolist")
          .onClick(()=>{
            this.pathStack.pushPath({
              name:"list"
            })
          })
      }
    }
    .onReady((context)=>{
      // 从上下文中获取pathStack参数,这个数据一定要和第一个的页面的数据对应上
      this.pathStack=context.pathStack
      this.pathStack.getParamByName("home") //可以根据name获取参数
    })

    .title("主要页面")
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值