1# API切换概述
2
3
4FA模型和Stage模型由于线程模型和进程模型的差异,部分接口仅在FA模型下才能使用,针对这部分接口在SDK的接口中有FAModelOnly的标记,用于提醒开发者这部分接口仅能在FA模型下使用。因此在切换到Stage模型时,需要将应用中用到的FAModelOnly接口替换成Stage模型下对应的接口。下面是startAbility的接口切换示例,全量接口列表请查看后续章节:
5
6![api-switch-overview](figures/api-switch-overview.png)
7
8startAbility接口由FA模型切换到Stage模型的示例:
9
10- FA模型示例
11
12  ```ts
13  import featureAbility from '@ohos.ability.featureAbility';
14  import Want from '@ohos.app.ability.Want';
15  import hilog from '@ohos.hilog';
16
17  const TAG: string = 'PagePageAbilityFirst';
18  const domain: number = 0xFF00;
19
20  @Entry
21  @Component
22  struct PagePageAbilityFirst {
23
24    build() {
25      Column() {
26        List({ initialIndex: 0 }) {
27          ListItem() {
28            Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
29              //...
30            }
31            .onClick(() => {
32              (async (): Promise<void> => {
33                try {
34                  hilog.info(domain, TAG, 'Begin to start ability');
35                  let want: Want = {
36                    bundleName: 'com.samples.famodelabilitydevelop',
37                    moduleName: 'entry',
38                    abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton'
39                  };
40                  await featureAbility.startAbility({ want: want });
41                  hilog.info(domain, TAG, `Start ability succeed`);
42                }
43                catch (error) {
44                  hilog.error(domain, TAG, 'Start ability failed with ' + error);
45                }
46              })()
47            })
48          }
49          //...
50        }
51        //...
52      }
53      //...
54    }
55  }
56
57  ```
58
59- Stage示例示例
60
61  ```ts
62  import hilog from '@ohos.hilog';
63  import Want from '@ohos.app.ability.Want';
64  import common from '@ohos.app.ability.common';
65  import { BusinessError } from '@ohos.base';
66  import { Caller } from '@ohos.app.ability.UIAbility';
67
68  const TAG: string = '[Page_UIAbilityComponentsInteractive]';
69  const DOMAIN_NUMBER: number = 0xFF00;
70
71  @Entry
72  @Component
73  struct Page_UIAbilityComponentsInteractive {
74    private context = getContext(this) as common.UIAbilityContext;
75    caller: Caller | undefined = undefined;
76    build() {
77      Column() {
78        //...
79        List({ initialIndex: 0 }) {
80          ListItem() {
81            Row() {
82              //...
83            }
84            .onClick(() => {
85              // context为Ability对象的成员,在非Ability对象内部调用需要
86              // 将Context对象传递过去
87              let wantInfo: Want = {
88                deviceId: '', // deviceId为空表示本设备
89                bundleName: 'com.samples.stagemodelabilitydevelop',
90                moduleName: 'entry', // moduleName非必选
91                abilityName: 'FuncAbilityA',
92                parameters: { // 自定义信息
93                  info: '来自EntryAbility Page_UIAbilityComponentsInteractive页面'
94                },
95              };
96              // context为调用方UIAbility的UIAbilityContext
97              this.context.startAbility(wantInfo).then(() => {
98                hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
99              }).catch((error: BusinessError) => {
100                hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
101              });
102            })
103          }
104          //...
105        }
106        //...
107      }
108      //...
109    }
110  }
111  ```
112