当前位置: 首页 > news >正文

山东住房和建设庭网站青岛谁做网站多少钱

山东住房和建设庭网站,青岛谁做网站多少钱,高德地图怎么看实况街景,中国品牌网站建设前言 上篇文章#xff0c;我们使用NodeJs脚本完成了HarmonyOS项目的组件化运行#xff0c;但是由于脚本是基于4.0.0.400版本的DevEco Studio开发的#xff0c;可能在配置文件的修改上有些许差距#xff0c;那么遇到这种情况怎么办#xff0c;一种是再写一套针对性的脚本文…前言 上篇文章我们使用NodeJs脚本完成了HarmonyOS项目的组件化运行但是由于脚本是基于4.0.0.400版本的DevEco Studio开发的可能在配置文件的修改上有些许差距那么遇到这种情况怎么办一种是再写一套针对性的脚本文件或者在原有的脚本中增加配置版本参数第二种就是自己搞一个俗话说授人以鱼不如授人以渔索性这篇文章就把上篇的脚本是如何实现的给大家阐述一下这样大家就可以自己操作了。 分析需求 需求的总体概括就非常的简单让动态共享包的模块在运行包和动态共享包之间可以动态的切换省去人工配置的步骤由上篇文章我们已经得知动态共享包和运行包之间的区别主要来源于三处分别是hvigorfile.ts文件、module.json5文件和缺少入口ability。 首先肯定需要一个可以控制的开关利用这个开关判断是否要进行模块的动态切换如果需要切换那么就执行动态共享包切换运行包否则就还原大致流程如下 梳理模板 无论是由动态共享包切换为运行包还是由运行包切换为动态共享包我们改变的都是配置文件也就是上述中存在差异的那三个文件文件的内容如何来回的更改呢当然了可以设置统一的内容只更改区别之处但是为了直观方便的查看和修改无疑使用模版是比较简单的。 首先准备好两份文件一份是动态共享包一份是运行包切换的时候直接选择不同的模版即可。 动态共享包模版 动态共享包需要提供两个模版即可分别是hvigorfile.ts文件和module.json5文件。 1、hvigorfile.ts import { hspTasks } from ohos/hvigor-ohos-plugin;export default {system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ } 2、module.json5 {module: {name: mine,type: shared,description: $string:shared_desc,deviceTypes: [phone,tablet],deliveryWithInstall: true,pages: $profile:main_pages} } 运行包模版 运行包除了两个配置文件不同还必须有Ability作为主入口这是必不可少的。 1、hvigorfile.ts import { hapTasks } from ohos/hvigor-ohos-plugin;export default {system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ } 2、module.json5 {module: {name: entry,type: entry,description: $string:module_desc,mainElement: EntryAbility,deviceTypes: [phone,tablet],deliveryWithInstall: true,installationFree: false,pages: $profile:main_pages,abilities: [{name: EntryAbility,srcEntry: ./ets/entryability/EntryAbility.ts,description: $string:EntryAbility_desc,icon: $media:icon,label: $string:EntryAbility_label,startWindowIcon: $media:icon,startWindowBackground: $color:start_window_background,exported: true,skills: [{entities: [entity.system.home],actions: [action.system.home]}]}]} } 3、Ability import AbilityConstant from ohos.app.ability.AbilityConstant; import hilog from ohos.hilog; import UIAbility from ohos.app.ability.UIAbility; import Want from ohos.app.ability.Want; import window from ohos.window;export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {hilog.info(0x0000, testTag, %{public}s, Ability onCreate);}onDestroy() {hilog.info(0x0000, testTag, %{public}s, Ability onDestroy);}onWindowStageCreate(windowStage: window.WindowStage) {// Main window is created, set main page for this abilityhilog.info(0x0000, testTag, %{public}s, Ability onWindowStageCreate);windowStage.loadContent(pages/Index, (err, data) {if (err.code) {hilog.error(0x0000, testTag, Failed to load the content. Cause: %{public}s, JSON.stringify(err) ?? );return;}hilog.info(0x0000, testTag, Succeeded in loading the content. Data: %{public}s, JSON.stringify(data) ?? );});}onWindowStageDestroy() {// Main window is destroyed, release UI related resourceshilog.info(0x0000, testTag, %{public}s, Ability onWindowStageDestroy);}onForeground() {// Ability has brought to foregroundhilog.info(0x0000, testTag, %{public}s, Ability onForeground);}onBackground() {// Ability has back to backgroundhilog.info(0x0000, testTag, %{public}s, Ability onBackground);} }技术实现 1、创建配置文件 第一步书写开关也就是配置文件当然了配置文件具体如何定义看自己安排无论何种形式展现该有的参数一定要有比如是否要开启组件化以及开启组件化的模块名字至于其他的参数可以根据需要进行添加目前我定义的配置文件如下具体的解释都有注释上篇文章中也做了一系列的解读。 #组件化配置文件 #是否开启组件化 startModulefalse #开启的组件名字开启后当前的组件可以独立运行 startModuleName #上述组件开启后其他非必要组件是否改为动态包模式,默认不改变 startOtherSharedfalse #过滤组件名字永远不会独立运行以应为逗号作为分割 filterModuleName #当前脚本默认加载的页面默认不填是Index.ets loadPage 配置文件这里我自定义了后缀具体是什么文件都所谓主要的是文件里的内容。 有了配置文件之后我们就可以根据配置文件来一层一层的实现相关逻辑。 2、初始化项目 nodeJs环境在安装DevEco Studio的时候就已经配置完成检验是否安装可以在命令行中执行如下命令 node -v 如果正常能显示版本号则安装成功。 在需要创建脚本的目录执行初始化操作 npm init 具体的步骤解释 package name 包名也就是工程名默认是括号中的内容 version:版本号默认是括号中的内容 description:描述信息 entry point入口文件名默认是括号中的内容 test command:测试命令 git repository:git仓库地址 keywords: 密码 author: 作者名字 license: (ISC)许可证 一路按照相关提示执行下一步即可其实就是生产了一个json配置文件大家也可以自己手动创建。 执行完毕之后就会在当前目录创建一个json文件。 json文件内容 3、创建执行js文件 这个js文件就是我们所有逻辑的书写地方为了能够让js文件可以正常运行我们需要在package.json文件里进行配置如下 以后执行脚本的时候直接在命令行中执行npm run module即可我们先简单的输出一个“Hello world”首先在module.js里进行打印如下所示 执行命令结果 3、完成最后的逻辑 由于需要对文件进行操作这里使用到了Node.js中的核心模块fs一句话介绍fs模块提供了丰富的函数和方法可以进行文件的读取、写入、复制、删除等操作同时也支持目录的创建、遍历和修改等操作如果你想要更详细的了解可以查看我的往期文章或者在网上搜索也行有大量的资料存在。 1)、读取配置文件 所有的功能实现都是基于配置文件所以配置文件里的参数至关重要也是程序的第一步读取配置文件逐项拿到设置的相关参数并记录下来。 //读取文件信息let path require(path);let dirName path.join(__dirname); //获取跟目录try {//读取配置文件查找对应的配置信息let data fs.readFileSync(dirName /module.harmony, utf-8);var startModule;var startModuleName;var filterModuleName;var startOtherShared;var loadContentPage;data.split(/\r?\n/).forEach((line, position) {if (position 2) {let open line.split()[1];startModule open.toString();}if (position 4) {let moduleName line.split()[1];startModuleName moduleName.toString();}if (position 6) {let otherName line.split()[1];startOtherShared otherName.toString();}if (position 8) {let filterName line.split()[1];filterModuleName filterName.toString();}if (position 10) {//load的页面信息let loadPage line.split()[1];loadContentPage loadPage.toString();}});//开启组件化之后单独的模块可以独立运行//不开启组件化那么entry可以独立运行其他均不可,需要一一更改配置文件traverseFolder(dirName, startModule.indexOf(true) ! -1,startModuleName, startOtherShared, filterModuleName, loadContentPage);} catch (e) {console.log(发生了错误请检查配置文件是否存在或反馈至AbnerMing);} 2)、动态修改配置文件信息 根据配置文件信息进行组件化运行也就是动态共享包切换为运行包如何切换拿到差异性文件然后读取模版信息进行写入即可。 需要注意的是动态共享包切换为运行包需要动态创建ability除此之外关于组件的名字ability名字尽量和组件保持一致。 运行包切换为动态共享包也是读取配置文件然后进行写入即可。 function traverseFolder(folderPath, isModule, startModuleName,startOtherShared, filterModuleName, loadContentPage) {const items fs.readdirSync(folderPath);items.forEach(item {let dir folderPath / item;const stats fs.statSync(dir);if (stats.isDirectory()) {let hvigorFilePath dir /hvigorfile.ts;fs.readFile(hvigorFilePath, utf8, (err, dataStr) {if (err) {return;}if (isModule) {//开启组件化//把当前的组件改为运行状态if (item startModuleName) {let moduleName item.substring(0, 1).toUpperCase() item.substring(1, item.length)//修改为可运行状态let entryHvigorFile getEntryHvigorFile();//读取string.json文件增加labellet jsonName dir /src/main/resources/base/element/string.json;fs.readFile(jsonName, utf8, (err, dataStr) {if (err) {return;}let obj JSON.parse(dataStr);let array obj[string];let label { name: shared_label, value: item };let isSharedLabel false;for (var i 0; i array.length; i) {let name array[i][name];if (name shared_label) {isSharedLabel true;break;}}if (!isSharedLabel) {array.push(label);}writeContent(jsonName, JSON.stringify(obj));//进一步更改json5文件let json5 dir /src/main/module.json5;writeContent(json5, getEntryModuleJson5(item, moduleName));});if (loadContentPage null || loadContentPage ) {//为空的时候才去创建//创建Index.ets文件let indexPath dir /src/main/ets/pages;const indexItem fs.readdirSync(indexPath);let isHaveIndex false;indexItem.forEach(item {if (item Index.ets) {//证明存在isHaveIndex true;}});if (!isHaveIndex) {//不存在就要去创建writeContent(indexPath /Index.ets, getIndex());}}//创建Ability文件let etsPath dir /src/main/ets/ item ability/ moduleName Ability.ts;fs.mkdir(dir /src/main/ets/ item ability, function (err) {if (err) {writeContent(etsPath, getAbility(moduleName, loadContentPage));return;}//写入文件writeContent(etsPath, getAbility(moduleName, loadContentPage));});} else {//非当前的组件需要改为动态包模式吗根据配置文件来改变有两种是永远不能改变的if (item ! entry filterModuleName.indexOf(item) -1 startOtherShared) {//把其他的模块都改成动态包不能运行let moduleJson5 getSharedModuleJson5(item);let hvigorFile getSharedHvigorFile();writeContent(hvigorFilePath, hvigorFile);writeContent(dir /src/main/module.json5, moduleJson5);}}} else {//主模块和需要过滤的模块不进行动态包设置if (item ! entry filterModuleName.indexOf(item) -1) {//把其他的模块都改成动态包不能运行let moduleJson5 getSharedModuleJson5(item);let hvigorFile getSharedHvigorFile();writeContent(hvigorFilePath, hvigorFile);writeContent(dir /src/main/module.json5, moduleJson5);}}});}}); } 相关总结 由于逻辑比较简单完整的逻辑大家可以查看源码 https://gitee.com/abnercode/harmony-os-module 由于开发环境的不同配置文件信息也有所不同无非就是更改的模版不一样只需要在脚本中换成你的环境下的配置文件内容即可。
http://www.dnsts.com.cn/news/137132.html

相关文章:

  • 临沂建设局网站官网做网站的书籍
  • 网站栏目的分类文章标签wordpress
  • 广州网站seo招聘南宁专业网络推广公司
  • 网站中的公司地址怎么做wordpress站点优化
  • 建设英文网站赚钱的36个方法个人建设网站难吗
  • 金融 网站建设国内外优秀设计网站
  • 提供做pc端网站企业公司网站制作
  • 虚拟机可以做两个网站西安网站设计费用
  • asp网站开发实例书籍网站怎么做收入
  • wordpress旅游网站主题wordpress如何修改电子邮箱
  • 可信赖的昆明网站建设网站被k多久可以恢复
  • 我想学制作网站吗jsp网站服务建设是什么
  • 三亚兼职网站做网站首选九零后网络
  • 网站表单制作推广公司哪家好
  • 58同城网站官网thinkphp5做网站
  • 广州手机网站制作咨询wordpress 树
  • 泰安中商网络做的网站怎么进入专业的外贸行业网站设计
  • 建设书局 网站网站前期推广
  • 四平网站建设怎么选做咨询类网站风险评估
  • 主题网站建设哪些行业没有做网站
  • 商城网站建设的优势服装加工厂怎么找客户
  • 桂林网站制作培训学校传奇网站一般怎么做的
  • 天津网站快速排名提升泰国一家做男模的网站
  • 温州网站设计力推亿企帮购物网站搜索功能怎么做
  • 青岛网站建设团队邯郸房产信息网查询系统
  • 找一个免费的网站网站开发心得体会
  • 网站维护优化黔南州住房和城乡建设局网站
  • 公众号推文制作网站怎么把网站开发成crx
  • 如何查看一个网站流量智效云seo
  • 重庆建网站多少钱wordpress如何备份