当前位置:   article > 正文

OpenHarmony JS——ToDoList应用实践_openharmony应用叠加js脚本

openharmony应用叠加js脚本

参考资料

技术胖-华为鸿蒙系统应用 OpenHarmony JS 前端开发 基础入门教程-完结 (jspang.com)

鸿蒙2.x系统应用开发 前端基础入门教程-12集全完结哔哩哔哩bilibili

1. 创建工程项目

2. 真机模拟预览

 3. 应用开发结构目录

目录结构中文件分类如下:

  • .hml 结尾的 HML 模板文件,这个文件用来描述当前页面的文件布局结构
  • .css 结尾的 CSS 样式文件,这个文件用于描述页面样式
  • .js 结尾的 JS 文件,这个文件用于处理页面和用户的交互

各个文件夹的作用:

  • app.js 文件用于全局 JavaScript 逻辑和应用生命周期管理。
  • pages 目录用于存放所有组件页面。
  • common 目录用于存放公共资源文件,比如:媒体资源和 JS 文件。
  • i18n 目录用于配置不同语言场景资源内容,比如:应用文本词条,图片路径等资源,注意 i18n 是开发保留文件夹,不可重命名。

 4.文件使用规则

(1)文件访问规则

应用资源可通过绝对路径或相对路径的方式进行访问,本开发框架中绝对路径以 "/" 开头,相对路径以 "./" 或 "../" ,具体访问规则如下:

  • 引用代码文件,需使用相对路径,比如:../common/utils.js。

  • 引用资源文件,推荐使用绝对路径。比如:/common/xxx.png。

  • 公共代码文件和资源文件推荐放在 common 下,通过以上两条规则进行访问。

  • CSS 样式文件中通过 url() 函数创建 数据类型,如:url(/common/xxx.png)。

  • 如果代码文件A和文件B位于同一目录,则代码文件B引用资源文件时可使用相对路径,也可使用绝对路径。

  • 如果代码文件A和文件B位于不同目录,则代码文件B引用资源文件时必须使用绝对路径。因为Webpack打包时,代码文件B的目录会发生变化。

5. 第三方JSON数据导入

注意使用相对路径

 6.具体代码

hml文件

  1. <div class="container">
  2. <text class="title">待办事项</text>
  3. <!-- 待办事项 -->
  4. <div class="item" for="{{todoList}}"> <!-- for 循环 -->
  5. <text class="todo">{{$item.info}}</text> <!-- {{$item.info}} 导入信息 -->
  6. <!-- {{$item.status}} 导入状态, 方法switchChange($idx) 改变状态-->
  7. <switch showtext="true" checked="{{$item.status}}"
  8. texton="完成" textoff="待办"
  9. class="switch"
  10. @change="switchChange($idx)"></switch>
  11. <button class="remove" onclick="remove($idx)">删除</button> <!-- .js 定义remove方法,传入索引值 -->
  12. </div>
  13. <!-- 剩余待办 -->
  14. <div class="info">
  15. <text class="info-text">您还有</text>
  16. <text class="info-num">{{todoCount}}</text><!-- todoCount方法计数-->
  17. <text class="info-text">件事情待办,加油!</text>
  18. </div>
  19. <!-- 添加待办 -->
  20. <div class="add-todo">
  21. <input class="plan-input" type="text"></input>
  22. <button class="plan-btn" onclick="addTodo" @click="addTodo">添加待办</button>
  23. </div>
  24. </div>

.js文件

  1. import todoList from "../../common/datas/todoList.js" // 导入外部JSON数据
  2. export default {
  3. data: {
  4. todoList,
  5. }, // 数据绑定:js 变量放在 data 中
  6. remove(index){
  7. console.log(index);
  8. this.todoList.splice(index, 1);
  9. },
  10. switchChange(index){
  11. this.todoList[index].status = !this.todoList[index].status;
  12. },
  13. addTodo(){
  14. this.todoList.push({
  15. info: 'IDE工具无法监听键盘输入',
  16. status: false
  17. })
  18. },
  19. computed:{
  20. todoCount(){
  21. let num = 0;
  22. this.todoList.forEach(element =>{
  23. if(!element.status){
  24. num++;
  25. }
  26. });
  27. return num;
  28. }
  29. }
  30. }

.css文件 

  1. .container {
  2. flex-direction: column;
  3. justify-content: flex-start;
  4. align-items: center;
  5. padding-bottom: 100px;
  6. }
  7. .title {
  8. font-size: 25px;
  9. margin-top: 20px;
  10. margin-bottom: 20px;
  11. color: #000000;
  12. opacity: 0.9;
  13. font-size: 28px;
  14. }
  15. .item{
  16. width: 325px;
  17. padding: 10px 0;
  18. flex-direction: row;
  19. align-items: center;
  20. justify-content: space-around;
  21. border-bottom: 1px solid #eee;
  22. }
  23. .todo{
  24. color: #000;
  25. width: 180px;
  26. font-size: 18px;
  27. }
  28. .switch{
  29. font-size: 12px;
  30. texton-color: green;
  31. textoff-color:red;
  32. text-padding: 5px;
  33. width: 100px;
  34. height: 24px;
  35. allow-scale: false;
  36. }
  37. .remove {
  38. font-size: 12px;
  39. margin-left: 10px;
  40. width: 50px;
  41. height: 22px;
  42. color: #fff;
  43. background-color: red;
  44. }
  45. .info{
  46. width: 100%;
  47. margin-top: 10px;
  48. justify-content: center;
  49. }
  50. .info-text {
  51. font-size: 18px;
  52. color: #AD7A1B;
  53. }
  54. .info-num{
  55. color: orangered;
  56. margin-left: 10px;
  57. margin-right: 10px;
  58. }
  59. .add-todo {
  60. position: fixed;
  61. left: 0;
  62. bottom: 0;
  63. width: 100%;
  64. height: 60px;
  65. flex-direction: row;
  66. justify-content: space-around;
  67. align-items: center;
  68. background-color: #ddd;
  69. }
  70. .plan-input {
  71. width: 240px;
  72. height: 40px;
  73. background-color: #fff;
  74. }
  75. .plan-btn {
  76. width: 90px;
  77. height: 35px;
  78. font-size: 15px;
  79. }

外部JSON数据 

  1. export default [
  2. {
  3. info: '给老王打个电话',
  4. status: true
  5. },
  6. {
  7. info: '输出工作计划',
  8. status: false
  9. },
  10. {
  11. info: '和小王对接需求',
  12. status: true
  13. },
  14. {
  15. info: '整理客户资料',
  16. status: false
  17. },
  18. {
  19. info: '和朋友一起聚餐',
  20. status: false
  21. }
  22. ]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/222683
推荐阅读
相关标签
  

闽ICP备14008679号