Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular nativescript |在android后退按钮导航后调用ngOnInit_Angular_Nativescript_Angular2 Nativescript - Fatal编程技术网

Angular nativescript |在android后退按钮导航后调用ngOnInit

Angular nativescript |在android后退按钮导航后调用ngOnInit,angular,nativescript,angular2-nativescript,Angular,Nativescript,Angular2 Nativescript,我在处理back按钮时遇到问题,当用户点击back按钮时,ngOnInit未被触发,组件未按预期工作。 从我所看到的,我明白,当拍摄回来的时候,它只会引起恩戈尼特的火灾,而不会引起恩戈尼特的火灾 这就是生命周期 打开应用程序-->调用ngOnInit() 单击名为的新组件的另一个组件-->ngOnInit() 回击-->非灾难 有人有同样的问题吗?你是如何处理的? 谢谢为什么你认为ngOnInit应该在后面运行?没有 解决方案是使用路由器事件: 例如:从“/beatles/john/songs/

我在处理back按钮时遇到问题,当用户点击back按钮时,ngOnInit未被触发,组件未按预期工作。 从我所看到的,我明白,当拍摄回来的时候,它只会引起恩戈尼特的火灾,而不会引起恩戈尼特的火灾

这就是生命周期 打开应用程序-->调用ngOnInit()

单击名为的新组件的另一个组件-->ngOnInit()

回击-->非灾难

有人有同样的问题吗?你是如何处理的?
谢谢

为什么你认为
ngOnInit
应该在后面运行?没有

解决方案是使用路由器事件:

例如:从
“/beatles/john/songs/strawberryfields”
“/doors/jim/Friends/pam”

是:

将此方法添加到共享服务中:

 public onNavigatePreviousToCurrent(pre: string, current: string  ): Observable<any>
        {
            return this.router.events.pipe(filter(e => e instanceof NavigationEnd), pairwise(), filter((f: [any, any]) =>
                                                                                                       {
                                                                                                           return f[0].url.toLowerCase()
                                                                                                                      .indexOf(pre.toLowerCase()) > -1 &&
                                                                                                                  f[1].url.toLowerCase()
                                                                                                                      .indexOf(current.toLowerCase()) > -1;
                                                                                                       })  );
        }
请注意,您也可以在Doors中使用来自
/beatles/john/songs/strawberryfields
beatles
john
songs
strawberryfields
)的任何标记。(子字符串)

基本上,此方法检测您从哪里来以及返回后的目的地是什么

顺便说一句,你可能会想说:“嗯,我不知道我在哪里,我只想要一个用户点击“back”的情况

答复:

this._routingService.onNavigatePreviousToCurrent("/", "pam") ;
// "/" will always be present , so basically this ^ detects back from anywhere to Doors page  

经过一番研究,我决定使用这个解决方案

在我需要ngOnInit运行的组件中,即使导航是从后退按钮或功能开始的,我添加了以下内容

constructor(private location : PlatformLocation) {}

ngOnInit() {
    this.location.onPopState(() => {
    this.initComponent();
});
}

您可以使用window的ondestroy事件按back按钮就像导航rout to back组件,为什么要启动ngOnInit?这不会区分您来自何处…有时,您希望根据您所在的上一页执行不同的逻辑…(您可以检查路径/url)此外,请考虑:<代码> A---导航->导航->导航> ->导航> -> A/<代码>,用我的代码可以检测到--->一种情况。但是这是一个很好的解决简单问题的方法,谢谢!它现在看起来很好,但是我会对它进行更多的测试,如果它不起作用,我就不把它替换给你。ng
this.location.onPopState(()=>{this.initComponent();});
this._routingService.onNavigatePreviousToCurrent("/", "pam") ;
// "/" will always be present , so basically this ^ detects back from anywhere to Doors page  
constructor(private location : PlatformLocation) {}

ngOnInit() {
    this.location.onPopState(() => {
    this.initComponent();
});
}