HarmonyOS NEXT開發(fā)利器:BasicLibrary三方庫推薦

2024-12-03 15:11 更新

大家好,我是 V 哥。你在學(xué)習(xí)HarmonyOS NEXT 開發(fā)嗎,今天 V 哥給你推薦一款超好用的三方庫BasicLibrary,BasicLibrary 是一個基于 API 11 封裝的基本庫,旨在提升鴻蒙開發(fā)效率。它包含了一些常用的 UI 組件和實用工具類,未來計劃將其打造成一個通用的 UI 組件和基本工具組件庫。

輸入圖片說明

安裝

要安裝 BasicLibrary,可以使用以下命令:

  1. ohpm install @peakmain/library

BasicLibrary 功能有哪些

先來看一下BasicLibrary都提供了哪些功能,一目了然。

輸入圖片說明

案例演示

List 列表

先來看一個 List 列表,支持下拉刷新和加載更多。

輸入圖片說明

1. 導(dǎo)入依賴

  1. import { PkList } from '@peakmain/library/Index'

2. 參數(shù)

輸入圖片說明

3. 看一個例子

  1. import { NavBar, PkList } from '@peakmain/library/Index'
  2. @Entry
  3. @Component
  4. struct ListPage{
  5. @State
  6. arr: String[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
  7. @State
  8. page: number = 1 // 第幾頁
  9. pageSize: number = 2 //共幾頁
  10. async getNewData(isRefresh:boolean){
  11. console.log("執(zhí)行了getNewData..." + isRefresh)
  12. const tmp = await this.getData(isRefresh)
  13. if (isRefresh) {
  14. this.arr = tmp
  15. } else {
  16. this.arr.push(...tmp)
  17. }
  18. }
  19. getData(isRefresh:boolean){
  20. console.log("執(zhí)行了getData..." + isRefresh)
  21. return new Promise<String[]>((resolve) => {
  22. let tmp: String[]
  23. setTimeout(() => {
  24. if (!isRefresh) {
  25. this.page++
  26. tmp = ['new_0', 'new_1', 'new_2', 'new_3', 'new_4', 'new_5']
  27. } else {
  28. this.page = 1
  29. tmp = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
  30. }
  31. console.log("當(dāng)前頁數(shù):" + this.page)
  32. resolve(tmp); // 執(zhí)行完成后調(diào)用 resolve
  33. }, 2000);
  34. });
  35. }
  36. @Builder
  37. renderItem(item: object){
  38. Column(){
  39. Text('' + item)
  40. .width('100%')
  41. .height(100)
  42. .fontSize(16)
  43. .textAlign(TextAlign.Center)
  44. .borderRadius(10)
  45. .backgroundColor(Color.White)
  46. }
  47. .
  48. margin({
  49. bottom: 10,
  50. left: 10, right: 10
  51. })
  52. .border({
  53. width: 0.5,
  54. color: Color.Red
  55. })
  56. .borderRadius(20)
  57. }
  58. build(){
  59. Column()
  60. {
  61. NavBar({
  62. title: "下拉刷新與加載更多"
  63. })
  64. PkList({
  65. dataSource: this.arr,
  66. finished: this.page >= this.pageSize,
  67. onRefresh: async () => {
  68. await this.getNewData(true)
  69. },
  70. renderItem: (item) => {
  71. this.renderItem(item)
  72. },
  73. onLoad: async () => {
  74. await this.getNewData(false)
  75. }
  76. }).margin({
  77. bottom: 20
  78. })
  79. }
  80. }
  81. }

Skeleton骨架屏

用于在內(nèi)容加載過程中展示一組占位圖形。

輸入圖片說明

導(dǎo)入依賴

  1. import { PkSkeleton } from '@peakmain/library';

參數(shù)

輸入圖片說明

示例代碼

  1. PkSkeleton({
  2. count: 3,
  3. showAvatar: this.showAvatar
  4. })

權(quán)限工具類

輸入圖片說明

導(dǎo)入依賴

  1. import PermissionUtils from '@peakmain/library/src/main/ets/utils/PermissionUtils';

創(chuàng)建request對象

  1. request: PermissionUtils = new PermissionUtils()

檢查是否有權(quán)限 方法如下:

  1. this.request.hasPermissions(權(quán)限數(shù)組)

示例如下

  1. async checkPermission(){
  2. let result =
  3. await this.request.checkPermissions(['ohos.permission.LOCATION', "ohos.permission.APPROXIMATELY_LOCATION"])
  4. if (result) {
  5. promptAction.showToast({ message: "已授予位置權(quán)限" })
  6. }
  7. return result
  8. }

請求權(quán)限

  1. this.request.requestPermission(權(quán)限數(shù)組)

示例如下

  1. result = await this.request.requestPermission(['ohos.permission.LOCATION', "ohos.permission.APPROXIMATELY_LOCATION"])
  2. if (result) {
  3. this.sLocation = true
  4. promptAction.showToast({ message: "已授予位置權(quán)限" })
  5. } else {
  6. this.sLocation = false
  7. promptAction.showToast({ message: "已拒絕位置權(quán)限" })
  8. }

打開應(yīng)用權(quán)限設(shè)置頁面

  1. this.request.openPermissionsInSystemSettings()

以上簡單給大家做了個演示,BasicLibrary 的更多功能,可以詳細(xì)參考文檔哦。

gitee 上獲取 BasicLibrary

輸入圖片說明

在gitee 上搜索 peakmain/BasicLibrary,即可獲取該組件的的全部內(nèi)容。關(guān)注威哥愛編程,一起學(xué)鴻蒙開發(fā)呀。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號