2024鸿蒙样题需要掌握的知识点
目录
一、读取json格式文件为对象或数组,显示相应字段
1、创建json文件的参数一致的类
2、导入类、导入json文件
3、循环渲染
二、推送普通文本类型通知。
三、打开和关闭子窗口;鼠标拖拽子窗口;在子窗口页面中显示轮播图;点击轮播图,跳转到某图书详细信息页面。
打开和关闭窗口
四、使用视频组件显示视频文件。
五、用户首选项或关系型数据库进行数据持久化存储。
六、在页面跳转时进行传值
1、字符串类型
2、对象类型
七、使用华为云,创建云函数;并根据云函数返回的接口地址,使用http进行网络数据获取。
1、创建云函数
2、调用云函数
八、打开手机拨号界面。
一、读取json格式文件为对象或数组,显示相应字段
-
1、创建json文件的参数一致的类
-
2、导入类、导入json文件
-
3、循环渲染
import router from '@ohos.router'
//导入即对象
import books from 'resources/rawfile/book1.json'
import { Book } from '../model/Book'@Entry
@Component
struct Detail {build() {Column() {//展示所有的图书-->读取book文件ForEach(books.book, (item: Book) => {Text(item.bookName)})}.height('100%').width('100%')}
}
二、推送普通文本类型通知。
//基础文本通知
import { notificationManager } from '@kit.NotificationKit';
// 描述通知的请求
let notificationRequest: notificationManager.NotificationRequest = {//id为通知的唯一标识,用于通讯的通知与取消id: 100,content: {//notificationContentType定义通知的类型notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,//normal定义普通文本类型通知的内容normal: {//title定义通知内容标题title: `通知内容标题`,//title定义通知内容详情text: '通知内容详情'}}
}
// 发送通知
notificationManager.publish(notificationRequest).then(()=>{console.info('publish success')
}).catch((err: Error) => {console.error(`publish failed,message is ${err}`);
});
简化版
notificationManager.publish({id: 1,content: {notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '图书馆的通知',text: '这里是图书馆的通知'}}})
三、打开和关闭子窗口;鼠标拖拽子窗口;在子窗口页面中显示轮播图;点击轮播图,跳转到某图书详细信息页面。
-
打开和关闭窗口
1、首先定义一个类
import { window } from '@kit.ArkUI'class MyWindows{windowStage:window.WindowStage|null=nullSubWindow:window.Window|null=null//打开async ShowSubWindows(){if (this.windowStage == null) {console.error('testTag','cuowu')}else {this.SubWindow=await this.windowStage.createSubWindow('kongzi')//设置窗口的位置this.SubWindow.moveWindowTo(440,550)//设置窗口的大小(宽、高)this.SubWindow.resize(800,1500)}//窗口里面的内容this.SubWindow?.setUIContent("pages/zi",()=>{//在这个页面展示出来(this.SubWindow as window.Window).showWindow()})}//关闭 destroySubWindow(){this.SubWindow?.destroyWindow((err)=>{if (err.code) {console.error('testTag','Failed to destroy the window. Cause: ' + JSON.stringify(err));return;}console.info('Succeeded in destroying the window.')})}
}
export default new MyWindows()
2、在EntryAbility中初始化
export default class EntryAbility extends UIAbility {async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {windowStage.loadContent('pages/Index', (err) => {……}); //小窗let windowsClass = await windowStage.getMainWindow()let name: Array<"status" | "navigation"> = []windowsClass.setWindowSystemBarEnable(name)MyWindows.WindowStage = windowStage}
3、在需要打开小窗的地方调用
//注意:需要导入MyWindows的ets文件 Button('点击弹出小窗').onClick(() => {MyWindows.ShowSubWindows() })
4、在需要关闭小窗的地方调用
//注意:需要导入MyWindows的ets文件 Button('点击关闭窗口').onClick(()=>{MyWindows.destroySubWindow()})
四、使用视频组件显示视频文件。
//视频控制器 controller: VideoController = new VideoController()Video({ src: this.book.videoPath, controller: this.controller }).width('85%').height(180).autoPlay(true)
五、用户首选项或关系型数据库进行数据持久化存储。
六、在页面跳转时进行传值
-
1、字符串类型
//传值
router.pushUrl({url:'pages/Detail',params:{libraryName:'图书馆'} })
//接收(注意类型)
const Params: string = router.getParams()as string const libraryName:String=Params['libraryName']
-
2、对象类型
//传值
router.pushUrl({url: 'pages/Detail',params: {name : '',book : ''} })
//接收
//Entry外定义类
class library {name: String = ''book: String = '' }//build前接收值
const Params: library = router.getParams()as library const name:String=Params.name const book:String=Params.book
七、使用华为云,创建云函数;并根据云函数返回的接口地址,使用http进行网络数据获取。
1、创建云函数
然后点击刚刚创建的触发器,点击修改,按需修改(Path等),点击修改成功
然后找到刚刚的API,在总览中复制API URL
2、调用云函数
//注意:需要导入http包(import http from '@ohos.net.http') let data=await http.createHttp().request('刚刚复制的API URL') console.log("testTag","调用华为云的云函数,返回的值是",`${data.result}`)
八、打开手机拨号界面。
//调用查询能力接口 let isSupport = call.hasVoiceCapability() //如果有能力拨打电话 if (isSupport) {//向10086拨打电话call.makeCall("10086",(err)=>{if (err) {//拨打电话的时候发生错误,打印日志console.log('testTag',"拨打电话失败")}else {//拨打电话成功,打印日志console.log('testTag',"拨打电话成功")}}) }
简化版
call.makeCall("10086")