【问题标题】:ionic 4 - android back button not working for tab-bar routesionic 4 - android后退按钮不适用于标签栏路线
【发布时间】:2020-04-07 03:16:05
【问题描述】:

我的应用的导航基本上是:

  1. 应用介绍屏幕
  2. 登录屏幕
  3. 带有 5 个不同标签的标签栏屏幕

所以,用户登录后,当我点击 Android 后退按钮时,它会返回登录页面。

我希望返回上一个导航页面/标签。

看起来路由器没有考虑标签页内的路由。

我的app.routes

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

import { AppintroGuard } from './guards/appintro.guard';
import { AuthGuard } from './guards/auth.guard';

const routes: Routes = [
  { path: '', loadChildren: './tabs/tabs.module#TabsPageModule', canActivate: [AuthGuard, AppintroGuard] },
  { path: 'appintro', loadChildren: './appintro/appintro.module#AppintroPageModule' },
  { path: 'auth-login', loadChildren: './auth-login/auth-login.module#AuthLoginPageModule' },
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule {}

我的/pages/tabs/tabs.router.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'tab-monitoring',
        children: [
          {
            path: '',
            loadChildren: '../tab-monitoring/tab-monitoring.module#TabMonitoringPageModule'
          },
          {
            path: 'tab-monitoring-filter',
            children: [
              {
                path: '',
                loadChildren: '../tab-monitoring-filter/tab-monitoring-filter.module#TabMonitoringFilterPageModule'
              }
            ]
          }
        ]
      },
      {
        path: 'tab-attachments',
        children: [
          {
            path: '',
            loadChildren: '../tab-attachments/tab-attachments.module#TabAttachmentsPageModule'
          }
        ]
      },
      {
        path: 'tab-dashboard',
        children: [
          {
            path: '',
            loadChildren: '../tab-dashboard/tab-dashboard.module#TabDashboardPageModule'
          }
        ]
      },
      {
        path: 'tab-progress',
        children: [
          {
            path: '',
            loadChildren: '../tab-progress/tab-progress.module#TabProgressPageModule'
          },
          {
            path: 'readjusts',
            children: [
              {
                path: ':id_painel',
                loadChildren: '../tab-readjusts/tab-readjusts.module#TabReadjustsPageModule'
              }
            ]
          },
          {
            path: 'project',
            children: [
              {
                path: ':id_projeto/:id_painel',
                loadChildren: '../tab-project/tab-project.module#TabProjectPageModule'
              },
              {
                path: ':id_projeto/:id_painel/readjusts',
                loadChildren: '../tab-readjusts-project/tab-readjusts-project.module#TabReadjustsProjectPageModule'
              }
            ]
          }
        ]
      },
      {
        path: 'tab-user',
        children: [
          {
            path: '',
            loadChildren: '../tab-user/tab-user.module#TabUserPageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/tab-dashboard',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/tab-dashboard',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ],
  exports: [RouterModule]
})

export class TabsPageRoutingModule {}

我是不是做错了什么?

我的登录函数回调:

this.authService.login(f.value.usuario, f.value.senha).subscribe(
  data => {
    this.authService.loadInitialData().then(value => {
      this.router.navigateByUrl('/tabs', {replaceUrl:true});
    });
  },
  error => {
    console.log('auth error');
    this.auth_error = true;
    this.is_loading = false;
  },
  () => { }
);

【问题讨论】:

    标签: android angular ionic-framework


    【解决方案1】:

    你可以试试这个将页面设置为root

    this.router.navigateByUrl(/* alternative root page */);
    

    例如

    this.router.navigateByUrl('tabs/tab-user');
    

    【讨论】:

    • 我正在使用此代码进行导航,它正确更改了 URL,但是当单击返回时,它会转到登录页面。
    • 添加到问题正文
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多