• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# getContext
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @xiang-shouxing-->
5<!--Designer: @xiang-shouxing-->
6<!--Tester: @sally__-->
7<!--Adviser: @HelloCrease-->
8
9如果需要在页面中获得当前Ability的Context,可调用getContext接口获取当前页面关联的[UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)或[ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md)。
10
11> **说明:**
12> - 该接口从API version 9开始支持,从API version 18开始废弃,建议使用[UIContext](arkts-apis-uicontext-uicontext.md)中的[getHostContext](arkts-apis-uicontext-uicontext.md#gethostcontext12)替代。
13> - 该接口仅限Stage模型使用。
14
15## getContext<sup>(deprecated)</sup>
16
17getContext(component?: Object):Context
18
19获取与页面上下文组件关联的Context对象。
20
21> **说明:**
22>
23> 从API version 18开始废弃,建议使用[UIContext](arkts-apis-uicontext-uicontext.md)中的[getHostContext](arkts-apis-uicontext-uicontext.md#gethostcontext12)替代。
24>
25> 从API version 12开始,可以通过使用[UIContext](arkts-apis-uicontext-uicontext.md)中的[getHostContext](arkts-apis-uicontext-uicontext.md#gethostcontext12)来明确UI的执行上下文。
26
27**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
28
29**系统能力:** SystemCapability.ArkUI.ArkUI.Full
30
31**模型约束:** 此接口仅可在Stage模型下使用。
32
33**参数:**
34
35| 参数名 | 类型          | 必填 | 说明                             |
36| ------ | ----------- | ---- | ------------------------------- |
37| component  | Object | 否   | 当前自定义组件的实例。未传入component或传入的参数类型非法,则返回默认上下文。默认上下文是指通过追溯当前方法的调用链所跟踪到的Context。在异步调用的回调方法中使用该接口,或者该接口的起始调用不在当前页面,将可能导致无法跟踪到该实例的Context,则会返回undefined。             |
38
39**返回值:**
40
41| 类型 | 说明                             |
42| ------ | ------------------------------- |
43| [Context](#context)  | 返回当前组件所在Ability的Context,Context的具体类型为当前Ability关联的Context对象。例如:在UIAbility窗口中的页面调用该接口,返回类型为UIAbilityContext。在ExtensionAbility窗口中的页面调用该接口,返回类型为ExtensionContext。    |
44
45## Context
46
47type Context = Context
48
49**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
50
51**系统能力:** SystemCapability.ArkUI.ArkUI.Full
52
53**模型约束:** 此接口仅可在Stage模型下使用。
54
55| 类型 | 说明                             |
56| ------ | ------------------------------- |
57| [Context](../../application-models/application-context-stage.md)  | 返回当前组件所在Ability的Context,Context的具体类型为当前Ability关联的Context对象。例如:在UIAbility窗口中的页面调用该接口,返回类型为UIAbilityContext。在ExtensionAbility窗口中的页面调用该接口,返回类型为ExtensionContext。    |
58
59**示例:**
60
61在UIAbility中通过windowStage.loadContent加载具体页面。
62
63> **说明:**
64>
65> 直接使用getContext可能导致[UI上下文不明确](../../ui/arkts-global-interface.md#ui上下文不明确)的问题,建议使用getUIContext()获取[UIContext](arkts-apis-uicontext-uicontext.md)实例,并使用[getHostContext](arkts-apis-uicontext-uicontext.md#gethostcontext12)调用绑定实例的getContext。
66
67```ts
68// EntryAbility.ets
69import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';
70import { hilog } from '@kit.PerformanceAnalysisKit';
71import { window } from '@kit.ArkUI';
72
73export default class EntryAbility extends UIAbility {
74  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
75    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
76  }
77
78  onDestroy() {
79    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
80  }
81
82  onWindowStageCreate(windowStage: window.WindowStage) {
83    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
84
85    windowStage.loadContent('pages/Index', (err, data) => {
86      if (err.code) {
87        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
88        return;
89      }
90      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
91    });
92  }
93
94  onWindowStageDestroy() {
95    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
96  }
97
98  onForeground() {
99    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
100  }
101
102  onBackground() {
103    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
104  }
105}
106```
107在具体的Index.ets中可以通过getContext接口获取Context上下文,本示例返回的Context类型为UIAbilityContext。
108
109```ts
110//pages/Index.ets
111@Entry
112@Component
113struct Index {
114  @State message: string = 'Hello World'
115
116  build() {
117    Row() {
118      Column() {
119        Text(this.message)
120          .fontSize(50)
121          .fontWeight(FontWeight.Bold)
122          .onClick(() => {
123            // 建议使用this.getUIContext().getHostContext()
124            let context: Context = getContext(this) as Context;
125            console.info("CacheDir:" + context.cacheDir);
126          })
127      }
128      .width('100%')
129    }
130    .height('100%')
131  }
132}
133```
134