1# 启动指定页面 2 3 4当PageAbility的启动模式设置为单例时(具体设置方法和典型场景示例见[PageAbility的启动模式](pageability-launch-type.md),缺省情况下是单实例模式),若PageAbility已被拉起,再次启动PageAbility会触发onNewWant回调(即非首次拉起)。应用开发者可以通过want传递启动参数,例如开发者希望指定页面启动PageAbility,可以通过want中的parameters参数传递pages信息,具体示例代码如下: 5 6 7调用方PageAbility的app.ets中或者page中,使用startAbility再次拉起PageAbility,通过want中的uri参数传递页面信息: 8 9```ts 10import featureAbility from '@ohos.ability.featureAbility'; 11 12async function restartAbility() { 13 let wantInfo = { 14 bundleName: "com.sample.MyApplication", 15 abilityName: "EntryAbility", 16 parameters: { 17 page: "pages/second" 18 } 19 }; 20 featureAbility.startAbility({ 21 want: wantInfo 22 }).then((data) => { 23 console.info('restartAbility success.'); 24 }); 25} 26``` 27 28 29在目标端PageAbility的onNewWant回调中获取包含页面信息的want参数: 30 31```ts 32export default { 33 onNewWant(want) { 34 globalThis.newWant = want 35 } 36} 37``` 38 39 40在目标端页面的自定义组件中获取包含页面信息的want参数并根据uri做路由处理: 41 42```ts 43import router from '@ohos.router' 44@Entry 45@Component 46struct Index { 47 @State message: string = 'Router Page' 48 newWant = undefined 49 onPageShow() { 50 console.info('Index onPageShow') 51 let newWant = globalThis.newWant 52 if (newWant.hasOwnProperty("page")) { 53 router.push({ url: newWant.page }); 54 globalThis.newWant = undefined 55 } 56 } 57 58 build() { 59 Row() { 60 Column() { 61 Text(this.message) 62 .fontSize(50) 63 .fontWeight(FontWeight.Bold) 64 } 65 .width('100%') 66 } 67 .height('100%') 68 } 69} 70``` 71 72 73当PageAbility的启动模式设置为多实例模式或为首次启动单例模式的PageAbility时(具体设置方法和典型场景示例见[PageAbility的启动模式](pageability-launch-type.md)),在调用方PageAbility中,通过want中的parameters参数传递要启动的指定页面的pages信息,调用startAbility()方法启动PageAbility。被调用方可以在onCreate中使用featureAbility的getWant方法获取want,再通过调用router.push实现启动指定页面。 74 75 76调用方的页面中实现按钮点击触发startAbility方法启动目标端PageAbility,startAbility方法的入参want中携带指定页面信息,示例代码如下: 77 78```ts 79import featureAbility from '@ohos.ability.featureAbility' 80@Entry 81@Component 82struct Index { 83 @State message: string = 'Hello World' 84 85 build() { 86 ... 87 Button("startAbility") 88 .onClick(() => { 89 featureAbility.startAbility({ 90 want: { 91 bundleName: "com.exm.myapplication", 92 abilityName: "com.exm.myapplication.EntryAbility", 93 parameters: { page: "pages/page1" } 94 } 95 }).then((data) => { 96 console.info("startAbility finish"); 97 }).catch((err) => { 98 console.info("startAbility failed errcode:" + err.code) 99 }) 100 }) 101 ... 102 Button("page2") 103 .onClick(() => { 104 featureAbility.startAbility({ 105 want: { 106 bundleName: "com.exm.myapplication", 107 abilityName: "com.exm.myapplication.EntryAbility", 108 parameters: { page: "pages/page2" } 109 } 110 }).then((data) => { 111 console.info("startAbility finish"); 112 }).catch((err) => { 113 console.info("startAbility failed errcode:" + err.code) 114 }) 115 }) 116 ... 117 } 118} 119``` 120 121 122目标端PageAbility的onCreate生命周期回调中通过featureAbility的getWant方法获取want,并对参数进行解析,实现指定页面拉起: 123 124```ts 125import featureAbility from '@ohos.ability.featureAbility'; 126import router from '@ohos.router'; 127 128export default { 129 onCreate() { 130 featureAbility.getWant().then((want) => { 131 if (want.parameters.page) { 132 router.push({ 133 url: want.parameters.page 134 }) 135 } 136 }) 137 }, 138 onDestroy() { 139 ... 140 }, 141} 142``` 143