1e41f4b71Sopenharmony_ci# Importing a Native Module 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciIn ECMAScript 6.0 (ES6) module design, the community uses the **import** syntax to load the content exported from other files (the ECMA specification defines the syntax specifications). 4e41f4b71Sopenharmony_ciTo help you easily use this feature to import the content exported from the native module (.so), ArkTS performs adaptation and provides several import methods. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci## Direct Import 7e41f4b71Sopenharmony_ciExport the content from the **index.d.ts** file of a native module, and then import the content to the file. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Named Import 10e41f4b71Sopenharmony_ci```ts 11e41f4b71Sopenharmony_ci// index.d.ts corresponding to libentry.so 12e41f4b71Sopenharmony_ciexport const add: (a: number, b: number) => number; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci```ts 15e41f4b71Sopenharmony_ci// test.ets 16e41f4b71Sopenharmony_ciimport { add } from 'libentry.so' 17e41f4b71Sopenharmony_ciadd(2, 3); 18e41f4b71Sopenharmony_ci``` 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci### Default Import 21e41f4b71Sopenharmony_ci```ts 22e41f4b71Sopenharmony_ci// index.d.ts corresponding to libentry.so 23e41f4b71Sopenharmony_ciexport const add: (a: number, b: number) => number; 24e41f4b71Sopenharmony_ci``` 25e41f4b71Sopenharmony_ci```ts 26e41f4b71Sopenharmony_ci// test.ets 27e41f4b71Sopenharmony_ciimport add from 'libentry.so' 28e41f4b71Sopenharmony_ciadd.add(2, 3); 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### Namespace Import 32e41f4b71Sopenharmony_ci```ts 33e41f4b71Sopenharmony_ci// index.d.ts corresponding to libentry.so 34e41f4b71Sopenharmony_ciexport const add: (a: number, b: number) => number; 35e41f4b71Sopenharmony_ci``` 36e41f4b71Sopenharmony_ci```ts 37e41f4b71Sopenharmony_ci// test.ets 38e41f4b71Sopenharmony_ciimport * as add from 'libentry.so' 39e41f4b71Sopenharmony_ciadd.add(2, 3); 40e41f4b71Sopenharmony_ci``` 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci## Indirect Import 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci### Converting to Named Variables Before Export and Import 45e41f4b71Sopenharmony_ci```ts 46e41f4b71Sopenharmony_ci// test1.ets 47e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog' 48e41f4b71Sopenharmony_ciexport { hilog } 49e41f4b71Sopenharmony_ci``` 50e41f4b71Sopenharmony_ci```ts 51e41f4b71Sopenharmony_ci// test2.ets 52e41f4b71Sopenharmony_ciimport { hilog } from './test1' 53e41f4b71Sopenharmony_cihilog.info(0x000, 'testTag', '%{public}s', 'test'); 54e41f4b71Sopenharmony_ci``` 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci### Converting to Namespaces Before Export and Import 57e41f4b71Sopenharmony_ci```ts 58e41f4b71Sopenharmony_ci// index.d.ts corresponding to libentry.so 59e41f4b71Sopenharmony_ciexport const add: (a: number, b: number) => number; 60e41f4b71Sopenharmony_ci``` 61e41f4b71Sopenharmony_ci```ts 62e41f4b71Sopenharmony_ci// test1.ets 63e41f4b71Sopenharmony_ciexport * from 'libentry.so' 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci```ts 66e41f4b71Sopenharmony_ci// test2.ets 67e41f4b71Sopenharmony_ciimport { add } from './test1' 68e41f4b71Sopenharmony_ciadd(2, 3); 69e41f4b71Sopenharmony_ci``` 70e41f4b71Sopenharmony_ciNote: Namespaces cannot be used simultaneously during the export and import of native modules. 71e41f4b71Sopenharmony_ci**Negative example:** 72e41f4b71Sopenharmony_ci```ts 73e41f4b71Sopenharmony_ci// test1.ets 74e41f4b71Sopenharmony_ciexport * from 'libentry.so' 75e41f4b71Sopenharmony_ci``` 76e41f4b71Sopenharmony_ci```ts 77e41f4b71Sopenharmony_ci// test2.ets 78e41f4b71Sopenharmony_ciimport * as add from 'file1' 79e41f4b71Sopenharmony_ci// The add object cannot be obtained. 80e41f4b71Sopenharmony_ci``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci## Dynamic Import 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci### Direct Import 85e41f4b71Sopenharmony_ci```ts 86e41f4b71Sopenharmony_ci// index.d.ts corresponding to libentry.so 87e41f4b71Sopenharmony_ciexport const add: (a: number, b: number) => number; 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci```ts 90e41f4b71Sopenharmony_ci// test.ets 91e41f4b71Sopenharmony_ciimport('libentry.so').then((ns:ESObject) => { 92e41f4b71Sopenharmony_ci ns.default.add(2, 3); 93e41f4b71Sopenharmony_ci}) 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci### Indirect Import 96e41f4b71Sopenharmony_ci```ts 97e41f4b71Sopenharmony_ci// test1.ets 98e41f4b71Sopenharmony_ciimport add from 'libentry.so' 99e41f4b71Sopenharmony_ciexport { add } 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci// test2.ets 102e41f4b71Sopenharmony_ciimport('./test1').then((ns:ESObject) => { 103e41f4b71Sopenharmony_ci ns.add.add(2, 3); 104e41f4b71Sopenharmony_ci}) 105e41f4b71Sopenharmony_ci``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**Note**: When dynamic loading is not supported, a file must be exported using the namespace. 108e41f4b71Sopenharmony_ci**Negative example:** 109e41f4b71Sopenharmony_ci```ts 110e41f4b71Sopenharmony_ci// test1.ets 111e41f4b71Sopenharmony_ciexport * from 'libentry.so' 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_ci```ts 114e41f4b71Sopenharmony_ci// test2.ets 115e41f4b71Sopenharmony_ciimport('./test1').then((ns:ESObject) => { 116e41f4b71Sopenharmony_ci // The ns object cannot be obtained. 117e41f4b71Sopenharmony_ci}) 118e41f4b71Sopenharmony_ci``` 119