• Home
Name Date Size #Lines LOC

..--

AppScope/12-May-2024-3734

entry/12-May-2024-1,2651,107

library/12-May-2024-593503

screenshots/12-May-2024-

.gitignoreD12-May-202479 66

README.mdD12-May-20242.8 KiB6030

README_zh.mdD12-May-20242.4 KiB6030

build-profile.json5D12-May-20241.1 KiB4745

hvigorfile.jsD12-May-2024168 21

package.jsonD12-May-2024366 1818

README.md

1# Usage of npm
2
3### Introduction
4
5This sample shows how npm references third-party JS class libraries (mathjs and dayjs) and local libraries. The display effect is as follows:
6
7![](./screenshots/en/index.png)
8
9### Concepts
10
11npm: a package manager that references third-party JS class libraries and local libraries.
12
13##### Importing a Third-Party JS Class Library
14
151. Open the **Terminal** window and run the following command to go to the **entry** directory:
16
17    cd entry
18
192. Run the following command to install a third-party library package. In this example, **dayjs** is being installed.
20
21    npm install dayjs --save
22
233. Import the third-party library package in the corresponding .ets file.
24
25    import dayjs from 'dayjs'
26
27##### Creating a Local Library
28
291. Right-click the project name, choose **New > Module**, click **Ohos Library** on the **Choose Your Ability Template** page displayed, enter the module name, and click **Finish**.
30
312. In the new module directory, edit the **name** attribute in the **Package.json** file, for example, **name:@ohos/library**.
32
333. In the **entry** directory, add the new module as a dependency to the **dependencies** attribute in the **Package.json** file, for example, **"@ohos/library": "../library"**.
34
354. In the new **Module** directory, add the common components or interfaces to be exported to **Index.ets** file.
36
375. In the **entry** directory, import the common component or interface in the new module to be used, for example, **import {ThirdTitleBar} from '@ohos/library'**.
38
39### Required Permissions
40
41N/A
42
43### Usage
44
451. The home page of the sample app shows the application of mathjs. After you touch **Click generate verification code**, a six-digit random verification code is generated in the lower part. If you touch the button again, a new verification code is displayed. You can touch **Back** to exit the app.
46
472. Touch **Next page**, and the application of dayjs is displayed. Enter the date to be calculated, enter either the number of days backwards or forwards, or both of them, and touch **Confirm**. The calculation result is displayed in the lower part. You can touch **Back** to return to the mathjs application page.
48
493. Touch **Next page**, and the application of the local library is displayed. Enter two strings as prompted and touch **Confirm**. The string after concatenation is displayed in the lower part. You can touch **Back** to return to the dayjs application page.
50
51### Constraints
52
531. This sample can only be run on standard-system devices.
54
552. In this sample, only open-source pure logic third-party libraries can be imported. The **format** method in dayjs is not supported.
56
573. This sample is based on the stage model, which is supported from API version 9.
58
594. DevEco Studio 3.0 Beta4 (Build version: 3.0.0.992, built on July 14, 2022) must be used.
60

README_zh.md

1# npm的使用
2
3### 简介
4
5本示例展示了npm引用第三方js类库和本地库,主要展示了mathjs、dayjs和本地库应用。实现效果如下:
6
7![](./screenshots/device/index.png)
8
9### 相关概念
10
11npm:npm引用第三方js类库和本地库。
12
13##### 第三方js类库导入步骤
14
151.打开Terminal窗口,通过如下指令进入到entry目录。
16
17    cd entry
18
192.以引入“dayjs”为例,执行以下指令进行安装。
20
21    npm install dayjs --save
22
233.命令执行完成后在需要应用dayjs库的ets文件中引入该库。
24
25    import dayjs from 'dayjs'
26
27##### 本地库新建步骤
28
291.右键点击工程名,选择New,选择Module,选择Ohos library方式创建,修改Module的名称,点击finish。
30
312.在新建的Module目录下,编辑Package.json中的“name”属性,比如:“name”:“@ohos/library”。
32
333.在entry目录下,编辑Package.json中的“dependencies”属性,新增新建的Module作为依赖,比如:"@ohos/library": "../library"。
34
354.在新建的Module目录下,编辑Index.ets,将需要export的公共组件或者接口添加进去。
36
375.在entry目录下,在需要用到新建Module里面公共组件或者接口时,import该组件进而应用,比如:import {ThirdTitleBar} from '@ohos/library'。
38
39### 相关权限
40
41不涉及。
42
43### 使用说明
44
451.首页是对mathjs的应用,点击**点击生成验证码**,下方会生成六位随机验证码,点击一次,验证码会刷新一次,点击**返回**,退出该程序。
46
472.点击**下一页**按钮,页面跳转到第二页对dayjs的应用,按照提示条件必需输入要计算的日期,至少填写一个向前或者向后推多少天,点击**确认**按钮,下方会显示出计算的结果,点击**返回**按钮,返回到第一页对mathjs的应用。
48
493.点击**下一页**按钮,页面跳转到第三页对本地库的应用,按照提示必须输入两个字符串,点击**确定**,下方会显示两个字符串拼接后的字符串。点击**返回**按钮,返回第二页对dayjs的应用。
50
51### 约束与限制
52
531.本示例仅支持在标准系统上运行。
54
552.本示例仅支持已经开源纯逻辑第三方库的导入。
56
573.本示例为Stage模型,从API version 9开始支持。
58
594.本示例需要使用DevEco Studio 3.0 Beta4 (Build Version: 3.0.0.992, built on July 14, 2022)才可编译运行。
60