ArkTS-一次开发,多端部署

展示在这里插入图片描述在这里插入图片描述

在这里插入图片描述

官方代码适配解读

官方代码:一次开发,多端部署-视频应用

解读

  1. 适配多端:根据屏幕大小来判断不同客户端,BreakpointSystem.ets中引入官方API获取 @ohos.mediaquery
  2. CommonConstants.ets定义好不同屏幕范围大小,供需要判断处使用
  3. BreakpointSystem.ets中根据不同的屏幕大小,设置不同的监听器,为监听器绑定注册和销毁回调事件
  4. 将当前屏幕应设置的栅格设置保存在AppStorage中的currentBreakpoint,供所需判断处使用
  5. MainPage.ets主页面中引入BreakpointSystem,在页面开启时aboutToAppear注册,在页面关闭时aboutToDisappear销毁
  6. MainPage.ets主页面通过.showSideBar(this.currentBreakpoint === Const.LG)判断是否显示侧边栏,通过 if (this.currentBreakpoint !== Const.LG) { BottomTabs({ bottomTabIndex: $bottomTabIndex }) }判断是否显示底边导航,实现导航的底部或者侧边显示,以实现多端适配

BreakpointSystem.ets代码

import mediaQuery from '@ohos.mediaquery';
import { CommonConstants as Const } from '../constants/CommonConstants';

export class BreakpointSystem {
  private currentBreakpoint: string = Const.MD;
  private smListener: mediaQuery.MediaQueryListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_1);
  private mdListener: mediaQuery.MediaQueryListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_2);
  private lgListener: mediaQuery.MediaQueryListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_3);

  private updateCurrentBreakpoint(breakpoint: string) {
    if (this.currentBreakpoint !== breakpoint) {
      this.currentBreakpoint = breakpoint;
      AppStorage.Set<string>('currentBreakpoint', this.currentBreakpoint);
    }
  }

  private isBreakpointSM = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(Const.SM);
    }
  }

  private isBreakpointMD = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(Const.MD);
    }
  }

  private isBreakpointLG = (mediaQueryResult: mediaQuery.MediaQueryResult) => {
    if (mediaQueryResult.matches) {
      this.updateCurrentBreakpoint(Const.LG);
    }
  }

  public register() {
    this.smListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_1);
    this.smListener.on('change', this.isBreakpointSM);
    this.mdListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_2);
    this.mdListener.on('change', this.isBreakpointMD);
    this.lgListener = mediaQuery.matchMediaSync(Const.BREAKPOINTS_SCOPE_3);
    this.lgListener.on('change', this.isBreakpointLG);
  }

  public unregister() {
    this.smListener.off('change', this.isBreakpointSM);
    this.mdListener.off('change', this.isBreakpointMD);
    this.lgListener.off('change', this.isBreakpointLG);
  }
}

CommonConstants.ets关键代码片段

 /**
   * Breakpoints that represent small device types.
   */
  static readonly SM: string = 'sm';

  /**
   * Breakpoints that represent middle device types.
   */
  static readonly MD: string = 'md';

  /**
   * Breakpoints that represent large device types.
   */
  static readonly LG: string = 'lg';
  /**
   * Range of the small device width.
   */
  static readonly BREAKPOINTS_SCOPE_1: string = '(320vp<=width<520vp)';

  /**
   * Range of the middle device width.
   */
  static readonly BREAKPOINTS_SCOPE_2: string = '(520vp<=width<840vp)';

  /**
   * Range of the large device width.
   */
  static readonly BREAKPOINTS_SCOPE_3: string = '(840vp<=width)';

MainPage.ets代码

import { BreakpointSystem, CommonConstants as Const } from '@ohos/common';
import { BottomTabs } from '../view/BottomTabsComponent';
import { LeftTabs } from '../view/LeftTabsComponent';
import { HomeTabs } from '../view/HomeTabsComponent';
import { FindTabs } from '../view/FindTabsComponent';
import { DriveTabs } from '../view/DriveTabsComponent';
import { MineTabs } from '../view/MineTabsComponent';

@Entry
@Component
struct MainPage {
  @State @Watch('onIndexChange') bottomTabIndex: number = 0;
  @StorageProp('currentBreakpoint') currentBreakpoint: string = Const.MD;
  private breakpointSystem: BreakpointSystem = new BreakpointSystem();
  private controller: TabsController = new TabsController();

  aboutToAppear() {
    this.breakpointSystem.register();
  }

  aboutToDisappear() {
    this.breakpointSystem.unregister();
  }

  onIndexChange() {
    this.controller.changeIndex(this.bottomTabIndex);
  }

  build() {
    SideBarContainer(SideBarContainerType.Embed) {
      LeftTabs({ bottomTabIndex: $bottomTabIndex });

      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.End, justifyContent: FlexAlign.End }) {
        Tabs({ barPosition: BarPosition.End, index: 0, controller: this.controller }) {
          TabContent() {
            HomeTabs({ currentBreakpoint: $currentBreakpoint })
          }
          .padding({
            left: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding1') : (this.currentBreakpoint === Const.MD ? $r('app.float.main_page_padding3') : 0),
            right: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding1') : $r('app.float.main_page_padding3')
          })

          TabContent() {
            FindTabs()
          }
          .padding({
            left: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding2') : (this.currentBreakpoint === Const.MD ? $r('app.float.main_page_padding3') : 0),
            right: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding2') : $r('app.float.main_page_padding3')
          })

          TabContent() {
            DriveTabs()
          }
          .padding({
            left: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding1') : (this.currentBreakpoint === Const.MD ? $r('app.float.main_page_padding3') : 0),
            right: this.currentBreakpoint === Const.SM ? $r('app.float.main_page_padding1') : $r('app.float.main_page_padding3')
          })

          TabContent() {
            MineTabs()
          }
        }
        .onChange((index: number) => {
          this.bottomTabIndex = index;
        })
        .width(Const.FULL_SIZE)
        .vertical(false)
        .barHeight(0)

        if (this.currentBreakpoint !== Const.LG) {
          BottomTabs({ bottomTabIndex: $bottomTabIndex })
        }
      }
      .width(Const.FULL_SIZE)
      .backgroundColor($r('app.color.background_color'))
    }
    .showSideBar(this.currentBreakpoint === Const.LG)
    .showControlButton(false)
    .sideBarWidth(Const.SIDEBAR_WIDTH)
    .maxSideBarWidth(Const.SIDEBAR_WIDTH_MAX)
    .minSideBarWidth(Const.SIDEBAR_WIDTH_MIN)
  }
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
object literals cannot be used as type declarations (arkts-no-obj-literals-a) 是指在 TypeScript 中,不能将对象字面量用作类型声明。 在 TypeScript 中,我们可以使用类型声明来定义变量、参数和函数的类型。类型声明允许我们指定变量的类型,以便在编译时进行类型检查,并提供更好的代码提示和静态分析。 然而,当我们尝试使用对象字面量作为类型声明时,TypeScript 会出现错误消息 "object literals cannot be used as type declarations (arkts-no-obj-literals-a)"。这是因为对象字面量通常用于创建对象而不是定义类型。 例如,下面的代码尝试使用对象字面量作为类型声明: ```typescript const user: { name: string, age: number } = { name: "Alice", age: 25 }; ``` 上述代码会导致错误,因为对象字面量不能被用作类型声明。为了解决这个问题,我们可以使用 interface 或 type 关键字来定义类型: ```typescript interface User { name: string, age: number } const user: User = { name: "Alice", age: 25 }; ``` 在上述代码中,我们使用 interface 关键字定义了一个名为 User 的接口,它包含了 name 和 age 两个属性。接着,我们使用 User 接口作为类型声明,来定义 user 变量的类型。 总结来说,"object literals cannot be used as type declarations (arkts-no-obj-literals-a)" 是 TypeScript 中的错误消息,意味着不能直接使用对象字面量作为类型声明,而应该使用接口或类型声明来定义对象的类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值