当前位置:网站首页>HarmonyOS应用开发培训第二次
HarmonyOS应用开发培训第二次
2022-06-22 20:01:00 【菜鸟不是】
父子组件通信功能实现
<element name="ctest" src="../../components/ctest/ctest.hml"></element>
<div class="container">
<ctest name="父传子内容"></ctest>
</div><!-- 务必需要注意的是:子组件的 hml、js、css 三个文件名必须保持一致 -->
<div class="ctest">
<text class="title">我是子组件</text>
<text>{
{ name }}</text>
</div>export default {
// props:[ "name" ],
props:{
name:{
default: '默认内容'
}
}
}Props 自定义组件可以通过 props 声明属性,父组件通过设置属性向子组件传递参数,camelCase(驼峰命名法)的 prop 名,在外部父组件传递参数时需要使用 kebab-case (短横线分隔命名)形式,即当属性compProp 在父组件引用时需要转换为 comp-prop。
添加默认值,子组件可以通过固定值 default 设置默认值,当父组件没有设置该属性时,将使用其默认值。此情况下 props 属性必须为对象形式,不能用数组形式。
数据单向性,父子组件之间数据的传递是单向的,只能从父组件传递给子组件,子组件不能直接修改父组件传递下来的值,可以将 props 传入的值用 data 接收后作为默认值,再对 data 的值进行修改。
- 子组件的定义
- 父组件的调用
路由功能实现
1.页面的定义
在 Pages 文件夹下面新建一个文件夹代表需要的路由,当然,我们也可以新建一个 Ablity 体验,这里演示 Pages.Name。
在 新建的文件夹下面务必需要注意新建三个文件
index.hml、index.js、index.css三个文件,该文件名必须使用 index 来进行命名,使用其他命名会造成文件依赖无法找到。快捷方式,可以选择到对应的 Ability Pages 文件夹下面,然后 new page 直接添加页面,并会自动注册好路由,这是比较方便的
在 config.json 文件中

2.路由的使用
- 导入路由模块
import router from '@system.router';- router.push(OBJECT),跳转到应用内的指定页面。
- router.replace(OBJECT),用应用内的某个页面替换当前页面,并销毁被替换的页面。
- router.back(OBJECT),返回上一页面或指定的页面。
// index页面,uri字段是页面路由,由配置文件中的pages列表指定。
router.push({
uri: 'pages/detail/detail',
});
// detail页面
router.push({
uri: 'pages/mall/mall',
});
// mall页面通过back,将返回detail页面
router.back();
// detail页面通过back,将返回index页面
router.back();
// 通过back,返回到detail页面
router.back({uri:'pages/detail/detail'});- router.clear(),清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。
- router.getLength(),获取当前在页面栈内的页面数量。
- router.getState(),获取当前页面的状态信息。
PS:页面路由需要在页面渲染完成之后才能调用,在onInit和onReady生命周期中页面还处于渲染阶段,禁止调用页面路由方法。
边栏推荐
- Objective-C不同数据类型占用字节大小
- 评估指标及代码实现(NDCG)
- csv新增一列
- ≥server2012R2系统,禁用系统自带的部分计划任务
- 长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
- Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022
- How to use feign to construct multi parameter requests
- 杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
- Arcgis中las点云数据抽稀
- Five uses of 87 with as
猜你喜欢
![[redis] cluster and common errors](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis] cluster and common errors
![[redis]redis的持久化操作](/img/83/9af9272bd485028062067ee2d7a158.png)
[redis]redis的持久化操作

【160. 相交链表】

基于C语言开发工资管理系统 课程论文+源码及可执行exe文件

杰理之列免晶振一拖八烧录升级【篇】

2022危险化学品经营单位主要负责人上岗证题库及模拟考试

2022化工自动化控制仪表考试练习题及在线模拟考试
![[513. find the value in the lower left corner of the tree]](/img/6d/b2ec8e3072a65c20c586941e6b2a85.png)
[513. find the value in the lower left corner of the tree]

2022 chemical automation control instrument examination exercises and online simulation examination

2022年起重机械指挥考试模拟100题及模拟考试
随机推荐
【206. 反转链表】
2022团体程序设计天梯赛L1
PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
[138. copy linked list with random pointer]
csv新增一列
Five uses of 87 with as
72 results and development suggestions of the latest on-site production system optimization
[20. valid brackets]
Apple corefoundation source code
Apple Objective-C source code
[206. reverse linked list]
[160. cross linked list]
[cm11 linked list splitting]
[142. circular linked list II]
字节跳动提出轻量级高效新型网络MoCoViT,在分类、检测等CV任务上性能优于GhostNet、MobileNetV3!...
NBA playoff match chart
访问成功但抛出异常:Could not find acceptable representation
85- have you learned any of these SQL tuning tips?
NBA季后赛对阵图
Moke 5. Service discovery -nacos