2024年鸿蒙最新HarmonyOS3,HarmonyOS鸿蒙系统工程师面试宝典

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

2 路由的传参,需要使用class

使用router进行页面跳转传递参数时,原来直接设置params参数即可,现在需要定义params为对象类型,构建routerParams对象进行传参。

如下,注释掉的代码为之前原有代码,在HarmonyOS Next4.1上无法使用。

2.1 在第一个页面中设置传递的参数

import router from '@ohos.router';
import promptAction from '@ohos.promptAction';


export class routerParams {
  msg: string

  constructor(msg: string) {
    this.msg = msg
  }
}

@Entry
@Component
struct LoginPage {
  @State message: string = 'Login Page'
  @State userName: string = ''
  @State password: string = ''
  build() {
    Row() {
      Column() {

        Button('跳转1')
          .width(100)
          .margin({ top: 10 })
          .onClick(() => {
            // router.pushUrl({ url: 'pages/HomePage', params: { msg: 'hello world,我是上一个页面传递过来的' } },
            //   router.RouterMode.Standard, (err) => {
            //     if (err) {
            //       promptAction.showToast({ message: `跳转失败:code is ${err.code}, message is ${err.message}` })
            //       return;
            //     } else {
            //       promptAction.showToast({ message: `跳转成功` })
            //     }
            //
            //   }
            // )

            router.pushUrl({ url: 'pages/HomePage', params: new routerParams("hello world,我是上一个页面传递过来的") },
              router.RouterMode.Standard, (err) => {
                if (err) {
                  promptAction.showToast({ message: `跳转失败:code is ${err.code}, message is ${err.message}` })
                  return;
                } else {
                  promptAction.showToast({ message: `跳转成功` })
                }

              }
            )
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

2.2 在第二个页面中接收传递的参数

接收上一个页面传递过来的参数时,需要注意判空,避免出现空指针问题。

import router from '@ohos.router';
import promptAction from '@ohos.promptAction';
import {routerParams} from './LoginPage';

@Entry
@Component
struct HomePage {
  @State message: string = 'HomePage'
  @State msg: string = '';

  onPageShow() {

    // const params: routerParams = router.getParams();
    // if (params != null && this.msg != null) {
    //   // 获取info属性的值
    //   this.msg = params['msg'];
    // } else {
    //   this.msg = '没有参数传递过来'
    // }
    
    // 获取传递过来的参数对象
    const params = router.getParams() as routerParams;
    if (params != null && this.msg != null) {
      // 获取info属性的值
      this.msg = params.msg;
    } else {
      this.msg = '没有参数传递过来'
    }
    
  }

  build() {
    Row() {
      Column() {

        Text(this.msg)
          .fontSize(20)

      }
      .width('100%')
    }
    .height('100%')
  }
}

3 应用文件访问代码修改

FileUtil.ets中

  • context通过getContext(this);进行获取,不能再使用getContext(this) as common.UIAbilityContext; 进行获取。
  • 调用fs.listFileSync获取文件列表时,需要传入ListFileOption对象,不能传入定义的options。
import fs,{ Filter }  from '@ohos.file.fs';
import Logger from '../utils/Logger';

const TAG = '[FileUtil]';
const context = getContext(this);

/**
 * 创建文件,写入文件,并返回文件内容
 * @returns
 */
export function createFile(): string {
  // 获取应用文件路径
  // let context = getContext(this) as common.UIAbilityContext;
  // filesDir为 /data/storage/el2/base/haps/entry/files
  let filesDir = context.filesDir;
  let filePath = filesDir + '/test.txt'
  // 新建并打开文件
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  // 写入一段内容至文件
  let writeLen = fs.writeSync(file.fd, "Try to write str.");
  console.info("The length of str is: " + writeLen);
  Logger.info(TAG, "The length of str is: " + writeLen);

  // // 从文件读取一段内容,方式一
  // let buf = new ArrayBuffer(1024);
  // let readLen = fs.readSync(file.fd, buf, { offset: 0 });
  // let content = String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)));
  // Logger.info(TAG, "readSync the content of file: " + content);

  // 从文件读取内容,方式二
  let content = fs.readTextSync(filePath);
  Logger.info(TAG, "readTextSync the content of file: " + content);

  // 关闭文件
  fs.closeSync(file);
  return content;
}

export function readWriteFile() {
  // 获取应用文件路径
  // let context = getContext(this) as common.UIAbilityContext;
  let filesDir = context.filesDir;

  // 打开文件
  let srcFile = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE);
  let destFile = fs.openSync(filesDir + '/destFile.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  // 读取源文件内容并写入至目的文件
  let bufSize = 4096;
  let readSize = 0;
  let buf = new ArrayBuffer(bufSize);
  let readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
  while (readLen > 0) {
    readSize += readLen;
    fs.writeSync(destFile.fd, buf);
    readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
  }
  // 关闭文件
  fs.closeSync(srcFile);
  fs.closeSync(destFile);
  Logger.info(TAG, "readWriteFile success");

}


export async function readWriteFileWithStream() {
  // 获取应用文件路径
  // let context = getContext(this) as common.UIAbilityContext;
  let filesDir = context.filesDir;

  // 打开文件流
  let inputStream = fs.createStreamSync(filesDir + '/test.txt', 'r+');
  let outputStream = fs.createStreamSync(filesDir + '/destFile.txt', "w+");
  // 以流的形式读取源文件内容并写入目的文件
  let bufSize = 4096;
  let readSize = 0;
  let buf = new ArrayBuffer(bufSize);
  let readLen = await inputStream.read(buf, { offset: readSize });
  readSize += readLen;
  while (readLen > 0) {
    await outputStream.write(buf);
    readLen = await inputStream.read(buf, { offset: readSize });
    readSize += readLen;
  }
  // 关闭文件流
  inputStream.closeSync();
  outputStream.closeSync();
}

/**
 * 查看文件列表
 * @returns 文件名[]
 */
export function getFileList(): string[] {
  // 获取应用文件路径


![img](https://img-blog.csdnimg.cn/img_convert/e9cf4ce220f8892ceed8898b3a143a61.png)
![img](https://img-blog.csdnimg.cn/img_convert/cc02f7f5e5723fb09fcfe37432df10f0.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

-1715741445977)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值