DEV Community

Cover image for HarmonyOS Development: the node model HvigorNode in the hvigor plug-in
程序员一鸣
程序员一鸣

Posted on

HarmonyOS Development: the node model HvigorNode in the hvigor plug-in

Foreword

this article API>= 13, based on DevEco Studio 5.1.0 Release, version number 5.1.0.828

in the previous article, we introduced two ways to develop an hvigor plug-in, but only outlined the basic development process and did not involve specific code development. In the next few articles, we will focus on understanding the API usage in the hvigor plug-in to facilitate actual plug-in development.

Like some common requirements, such as code checking before construction, basic configuration checking, etc., we often need to obtain the current project path, each module under the project, and even each file under the project. Then, how do we obtain these functions? At present, there are many ways for everyone to choose from in the government. Let's summarize them one by one.

The first is HvigorNode it is the node model interface in hvigor, through which we can obtain the configuration, attributes and tasks of the module.

Do you still remember where our plug-in development was written in the previous article? Did we customize a plug-in method and implement an apply method, in which we received a parameter that is the HvigorNode type, which provides many methods, can meet our operation of the project.

function myPlugin(): HvigorPlugin {
  return {
    pluginId: 'myPlugin',
    apply(node: HvigorNode) {
      console.log('I am a simple plugin');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Introduction to methods in HvigorNode

1, get the current node path

get the node path, that is, get the current project path, you can get the getnopath () method, the code is as follows:

2, get the current node name

the node name, which can be passed through getNodeName() method to get, directly get is the name of the current project.

3. Obtain all child nodes under the project

the child nodes under the project can be obtained through the subNodes() method, which obtains independent module information and other files will not be returned.

4, get node object based on node name

if you don't want to get child nodes through traversal, you can also get them through known module names, that is, through the getSubNodeByName() method.

 const subNode = node.getSubNodeByName('entry');
 console.log(subNode);
Enter fullscreen mode Exit fullscreen mode

5, get Parent Node Object

get Parent Section point objects can be obtained by getParentNode() method. One thing to note when using this method is that it is only applicable to the acquisition of Project child nodes. If the acquisition on the project node is undefined, therefore, do not directly obtain the parent node through the project node.

The following code is no problem and is obtained through the project node.


      node.subNodes((childDode: HvigorNode) => {

        const parentNode = childDode.getParentNode();
        console.log(parentNode.getNodeName());
      })
Enter fullscreen mode Exit fullscreen mode

6, node Registration Task

the registration task, which is executed in the configuration phase of the hvigor lifecycle, needs to complete the implementation of hvigo task as the input object.


node.registerTask({
    name: 'myTask',
    run() {
        console.log('myTask');
    }
});
Enter fullscreen mode Exit fullscreen mode

7,Task objects registered in the node

node.getTaskByName('myTask')
Enter fullscreen mode Exit fullscreen mode

8, pluginId obtains information about the current node.

In this case, you can also get information about the project, such as the name of the project, the path, and so on.

 const appContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
  console.log('projectName:', appContext.getProjectName());
Enter fullscreen mode Exit fullscreen mode

9, get the pluginId collection loaded by the current node.

 const allPluginIds = node.getAllPluginIds();
            allPluginIds.forEach((id) => {
                console.log(id);
            })
Enter fullscreen mode Exit fullscreen mode

10, add Extended Attributes

 node.addExtraOption('key', 'value');
Enter fullscreen mode Exit fullscreen mode

11, get node object based on node name

node.getExtraOption('key');
Enter fullscreen mode Exit fullscreen mode

12, add a callback function before node evaluation

node.beforeNodeEvaluate(hvigorNode => {

      })
Enter fullscreen mode Exit fullscreen mode

13, add a callback function after node evaluation

node.afterNodeEvaluate(hvigorNode => {

      })
Enter fullscreen mode Exit fullscreen mode

related Summary

hvigorNode, in actual development, can assist us in obtaining information about projects and submodules, which is convenient for us to operate files. It is very convenient. It can be applied to the implementation of two plug-ins, which greatly improves the efficiency of writing plug-ins.

This article label: Hongmeng Development Tools/DevEco Studio

Top comments (0)