上手做一个华为鸿蒙手表应用 4 - 生命周期事件

1,661 阅读1分钟

接上一篇developer.huawei.com/consumer/cn… 本节内容: 生命周期事件 源码仓库地址gitee.com/zhaoquan/ha…   生命周期文档:

这里我们先从 JS API 的生命周期接口开始

这里就不画图了,直接用张荣超老师视频教程上的截图,没有哪帧图片没有字幕,抱歉了 这里生命周期跟,小程序很像

在这里插入图片描述

app.js

为了方便看生命周期,将 app.js 的 console.info("Application onCreate"); 改为:console.log("应用创建")'

console.info("Application onDestroy");改为:console.log("应用销毁");

//  app.js
// 修改后的代码如下
export default {
    onCreate() {
        console.log("应用创建");
    },
    onDestroy() {
        console.log("应用销毁");
    }
};

index.js

为了方便看生命周期,使用console.log打印 onInit(){ console.log("index 页面的 onInit() 被调用"); }, onReady(){ console.log("index 页面的 onReady() 被调用"); }, onShow(){ console.log("index 页面的 onShow() 被调用"); }, onDestroy(){ console.log("index 页面的 onDestroy() 被调用"); },

// index.js
// 在 index.js 添加生命周期函数,注意 data:{} 后面要添加英文逗号
import router from '@system.router'

export default {
    data: {
        title: 'World'
    },
    onInit(){
        console.log("index 页面的 onInit() 被调用");
    },
    onReady(){
        console.log("index 页面的 onReady() 被调用");
    },
    onShow(){
        console.log("index 页面的 onShow() 被调用");
    },
    onDestroy(){
        console.log("index 页面的 onDestroy() 被调用");
    },
    clickAction(){
//        console.log("我被点击了")
        router.replace({
            uri:'pages/xunlian/xunlian',
        });
    }
}

启动 Debug 看看调试信息

在这里插入图片描述


原文链接:developer.huawei.com/consumer/cn… 作者:chatterzhao