Resource
Provides APIs for accessing application and system resources. For details about resource types and usage, see Resource Categories and Access.
NOTE
The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
$r
$r(value: string, ...params: any[]): Resource
Accesses application or system resources. During compilation, the toolchain converts $r into a Resource object. For details about how to access application or system resources using $r, see Resource Categories and Access.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | string | Yes | Resource identifier in the format belonging.type.name. belonging: resource source. Values: sys, app, or [hsp_name]. type: resource type. Values: boolean, color, float, intarray, integer, pattern, plural, strarray, string, or media. name: resource name. Application resource names are defined in the project's resources directory. For details about system resource names, see Resource Categories and Access. |
| ...params | any[] | No | Additional parameters passed by you. |
Return value
| Type | Description |
|---|---|
| Resource | Resource object containing the package name, module name, resource ID, and more. |
Example
@Entry
@Component
struct Page {
build() {
Row() {
Column() {
Text($r('app.string.app_name'))
}
.width('100%')
}
.height('100%')
}
}
For sample code about how to access resources in an HSP, see Accessing Resources Across HAPs/HSPs.
$rawfile
$rawfile(value: string): Resource
Accesses resources in the rawfile directory of the project. During compilation, the toolchain converts $rawfile into a Resource object. For details about how to access application or system resources using $rawfile, see Resource Categories and Access.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | string | Yes | Relative path in the rawfile directory. The file name must contain an extension, and the path cannot start with a slash (/). |
Return value
| Type | Description |
|---|---|
| Resource | Resource object containing the package name, module name, resource ID, and more. |
// Add startIcon.png to the src/main/resources/rawfile directory.
@Entry
@Component
struct Page {
build() {
Row() {
Column() {
Image($rawfile("startIcon.png"))
}
.width('100%')
}
.height('100%')
}
}
For sample code about how to access resources in an HSP, see Accessing Resources Across HAPs/HSPs.
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 arkts-apis-uicontext-contextmenucontroller
openharmony 鸿蒙 errorcode-canvas
openharmony 鸿蒙 capi-oh-nativexcomponent-native-xcomponent-oh-nativexcomponent
openharmony 鸿蒙 errorcode-bindSheet
openharmony 鸿蒙 js-apis-arkui-uiExtension-sys
openharmony 鸿蒙 capi-arkui-accessibility-arkui-accessibilityeventinfo
openharmony 鸿蒙 capi-arkui-rendernodeutils
openharmony 鸿蒙 js-apis-arkui-node
openharmony 鸿蒙 capi-native-node-h
openharmony 鸿蒙 capi-arkui-nativemodule-arkui-listitemswipeactionitem