NativeScript 从自定义导航到另一个页面 Android activity
NativeScript navigate to another page from custom Android activity
我正在开发一个带有微信登录功能的APP。获得微信批准后,它将调用我的 NativeScript APP 的自定义 activity。我得到的响应是正确的,但是在进行一些验证后,我如何才能移动到主页的另一个页面。我正在使用 NativeScript + Angular.
getJSON("https://api.weixin.qq.com/sns/oauth2/access_token?appid=ID&secret=SECRET&code=" + res.code + "&grant_type=authorization_code").then((res) => {
console.dir(res);
// ===> here I want navigation
}, err => {
console.dir(err);
})
我这样试过:
frame.topmost().navigate("src/app/login/login.component");
但出现错误:
JS: Unhandled Promise rejection: Cannot set property '_moduleName' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot set property '_moduleName' of undefined TypeError: Cannot set property '_moduleName' of undefined
请给我一些建议。提前致谢:)
一个类似的问题让我花了整个周末,不知道配套控制器 .js
或 .ts
文件看起来如何我只能建议你去基础知识/来源:
https://docs.nativescript.org/core-concepts/navigation#frame
它帮助我找到了代码中的问题。
如果您编写的自定义页面不是从 template
开始的,那么您很可能也编写了控制器,并且很容易忽略存在于样本 - 即使你看起来不需要它。
如果您粘贴您的控制器 JS,我们可以理解您的上下文。
例如,您绝对应该包含一段代码来帮助您进行调试:
在你的page-you-navigate-to.xml
<Page loaded="onPageLoaded" class="page">
...
</Page>
在你的page-you-navigate-to.ts
import { EventData } from "tns-core-modules/data/observable";
import { Page } from "tns-core-modules/ui/page";
export function onPageLoaded(args: EventData): void {
const page = <Page>args.object;
var context = page.navigationContext;
page.bindingContext = context;
console.log("Target page Loaded.");
}
启动您的调试器,逐步查看您的 null
值并阅读这些方法调用及其参数。
从 Activity 回调中触发一个事件,例如
import { android } from "tns-core-modules/application";
...
public onResp(res: com.tencent.mm.opensdk.modelbase.BaseResp) {
console.log("onResp");
console.dir(res);
androidApp.notify(<AndroidActivityEventData>{ eventName: 'wxapiresponse', object: android, activity: this });
}
在app组件中,监听事件
export class AppComponent implements OnInit {
constructor(private ngZone: NgZone, private routerExtensions: RouterExtensions) {}
ngOnInit() {
application.android.on('wxapiresponse', this.wxApiResponse, this);
}
wxApiResponse() {
// making sure the event callback runs inside Angular zone
this.ngZone.run(() => {
this.routerExtensions.navigate(...);
});
}
}
我正在开发一个带有微信登录功能的APP。获得微信批准后,它将调用我的 NativeScript APP 的自定义 activity。我得到的响应是正确的,但是在进行一些验证后,我如何才能移动到主页的另一个页面。我正在使用 NativeScript + Angular.
getJSON("https://api.weixin.qq.com/sns/oauth2/access_token?appid=ID&secret=SECRET&code=" + res.code + "&grant_type=authorization_code").then((res) => {
console.dir(res);
// ===> here I want navigation
}, err => {
console.dir(err);
})
我这样试过:
frame.topmost().navigate("src/app/login/login.component");
但出现错误:
JS: Unhandled Promise rejection: Cannot set property '_moduleName' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot set property '_moduleName' of undefined TypeError: Cannot set property '_moduleName' of undefined
请给我一些建议。提前致谢:)
一个类似的问题让我花了整个周末,不知道配套控制器 .js
或 .ts
文件看起来如何我只能建议你去基础知识/来源:
https://docs.nativescript.org/core-concepts/navigation#frame
它帮助我找到了代码中的问题。
如果您编写的自定义页面不是从 template
开始的,那么您很可能也编写了控制器,并且很容易忽略存在于样本 - 即使你看起来不需要它。
如果您粘贴您的控制器 JS,我们可以理解您的上下文。
例如,您绝对应该包含一段代码来帮助您进行调试:
在你的page-you-navigate-to.xml
<Page loaded="onPageLoaded" class="page">
...
</Page>
在你的page-you-navigate-to.ts
import { EventData } from "tns-core-modules/data/observable";
import { Page } from "tns-core-modules/ui/page";
export function onPageLoaded(args: EventData): void {
const page = <Page>args.object;
var context = page.navigationContext;
page.bindingContext = context;
console.log("Target page Loaded.");
}
启动您的调试器,逐步查看您的 null
值并阅读这些方法调用及其参数。
从 Activity 回调中触发一个事件,例如
import { android } from "tns-core-modules/application";
...
public onResp(res: com.tencent.mm.opensdk.modelbase.BaseResp) {
console.log("onResp");
console.dir(res);
androidApp.notify(<AndroidActivityEventData>{ eventName: 'wxapiresponse', object: android, activity: this });
}
在app组件中,监听事件
export class AppComponent implements OnInit {
constructor(private ngZone: NgZone, private routerExtensions: RouterExtensions) {}
ngOnInit() {
application.android.on('wxapiresponse', this.wxApiResponse, this);
}
wxApiResponse() {
// making sure the event callback runs inside Angular zone
this.ngZone.run(() => {
this.routerExtensions.navigate(...);
});
}
}