• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# getContext
2
3如果需要在页面中获得当前Ability的Context,可调用getContext接口获取当前页面关联的UIAbilityContext或ExtensionContext。
4
5> **说明:**
6> - 该接口从API version 9开始支持。
7> - 该接口仅限Stage模型使用。
8
9getContext(component?: Object):Context
10
11**参数:**
12
13| 参数名 | 类型          | 必填 | 说明                             |
14| ------ | ----------- | ---- | ------------------------------- |
15| component  | Object | 否   | 当前自定义组件的实例。未传入component或传入的参数类型非法,则返回默认上下文。默认上下文是指通过追溯当前方法的调用链所跟踪到的Context。在异步调用的回调方法中使用该接口,或者该接口的起始调用不在当前页面,将可能导致无法跟踪到该实例的Context,则会返回undefined。             |
16
17**返回值:**
18
19| 类型 | 说明                             |
20| ------ | ------------------------------- |
21| [Context](../../application-models/application-context-stage.md#应用上下文context)  | 返回当前组件所在Ability的Context,Context的具体类型为当前Ability关联的Context对象。例如:在UIAbility窗口中的页面调用该接口,返回类型为UIAbilityContext。在ExtensionAbility窗口中的页面调用该接口,返回类型为ExtensionContext。    |
22
23**示例:**
24
25在UIAbility中通过windowStage.loadContent加载具体页面。
26
27```ts
28// EntryAbility.ets
29import AbilityConstant from '@ohos.app.ability.AbilityConstant';
30import hilog from '@ohos.hilog';
31import UIAbility from '@ohos.app.ability.UIAbility';
32import Want from '@ohos.app.ability.Want';
33import window from '@ohos.window';
34
35export default class EntryAbility extends UIAbility {
36  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
37    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
38  }
39
40  onDestroy() {
41    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
42  }
43
44  onWindowStageCreate(windowStage: window.WindowStage) {
45    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
46
47    windowStage.loadContent('pages/Index', (err, data) => {
48      if (err.code) {
49        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
50        return;
51      }
52      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
53    });
54  }
55
56  onWindowStageDestroy() {
57    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
58  }
59
60  onForeground() {
61    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
62  }
63
64  onBackground() {
65    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
66  }
67}
68```
69在具体的Index.ets中可以通过getContext接口获取Context上下文,本示例返回的Context类型为UIAbilityContext。
70
71```ts
72//pages/Index.ets
73@Entry
74@Component
75struct Index {
76  @State message: string = 'Hello World'
77
78  build() {
79    Row() {
80      Column() {
81        Text(this.message)
82          .fontSize(50)
83          .fontWeight(FontWeight.Bold)
84          .onClick(() => {
85            let context : Context = getContext(this) as Context
86            console.info("CacheDir:" + context.cacheDir)
87          })
88      }
89      .width('100%')
90    }
91    .height('100%')
92  }
93}
94```
95