离子4-检查是否可以返回

时间:2019-05-27 21:36:01

标签: angular ionic-framework ionic4

我正在寻找一种方法,可以在单击按钮后将用户重定向(如果存在的话),然后将其重定向到特定的路由。

我需要在代码中添加一个条件,但是我似乎找不到办法。

java.lang.NullPointerException
at First.doGet(First.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

在Ionic 3中,我们具有canGoBack()1函数……Ionic 4的任何想法?由于它使用的是Angular路由器...

谢谢。

3 个答案:

答案 0 :(得分:1)

由于Ionic 4使用Angular路由,因此您可以通过Angular方式实现此功能,因此可以通过创建如下服务来实现:

import { Injectable } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Injectable()
export class PreviousRouteService {

  private previousUrl: string;
  private currentUrl: string;

  constructor(private router: Router) {
    this.currentUrl = this.router.url;
    router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {        
        this.previousUrl = this.currentUrl;
        this.currentUrl = event.url;
      };
    });
  }

  public getPreviousUrl() {
    return this.previousUrl;
  }    
}

现在将服务注入到您要检查其状况的组件中:

在您的组件中:

import { Component } from '@angular/core';
import { PreviousRouteService } from '../services/previous-route.service';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  constructor(private previousRouteService: PreviousRouteService) {}  

  goback() {
    if(this.previousRouteService.getPreviousUrl()) {
      this.navCtlr.setDirection('back');
      this.location.back();
    }
    else {
      this.router.navigate(['/']);
    }
  }
}

答案 1 :(得分:1)

我想到的唯一想法是使用Angular路由防护器来允许或限制返回,即使是resolve函数也会有所帮助。

并使用导航历史记录服务(RoutingStateService),例如https://blog.hackages.io/our-solution-to-get-a-previous-route-with-angular-5-601c16621cf0中提到的 在我使用RoutingStateService处理导航历史记录的情况下,它比直接信任NavigationEnd事件可靠得多(因为RouterStateService在内部使用了它)。

希望有帮助。最好的问候。

答案 2 :(得分:1)

您不能将<ion-back-button>defautHref一起使用吗?

<ion-back-button defaultHref="/"></ion-back-button>