Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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 2中绕过路由器_Angular - Fatal编程技术网

如何在Angular 2中绕过路由器

如何在Angular 2中绕过路由器,angular,Angular,我需要绕过特定相对路由的角度路由,例如“/register”,但我似乎找不到一种方法来指示Angular的RouterModule不处理特定的相对路由 任何未指定的路由都将被RouterModule捕获并重定向到“/”,即使我没有设置通配符路由 我尝试了dev服务器和真正的linux服务器 这个问题以前被问过,但没有令人满意的答案 以下是我当前路由代码的简化版本: export const module_page1_path = 'page1.module'; export const modu

我需要绕过特定相对路由的角度路由,例如“/register”,但我似乎找不到一种方法来指示Angular的RouterModule不处理特定的相对路由

任何未指定的路由都将被RouterModule捕获并重定向到“/”,即使我没有设置通配符路由

我尝试了dev服务器和真正的linux服务器

这个问题以前被问过,但没有令人满意的答案

以下是我当前路由代码的简化版本:

export const module_page1_path = 'page1.module';
export const module_page2_path = 'page2.module';

const createImportFunction = (path: string, moduleName: string) => () =>
        import('./' + path).then(mod => mod[moduleName]);

    export const appRoutes: Routes = [
        {
            path: 'page1',
            loadChildren: createImportFunction(module_page1_path, 'Page1Module')
        },
        {
            path: 'page2',
            loadChildren: createImportFunction(module_page2_path, 'Page2Module'),
            canActivate: [AuthGuardService]
        },
        {
            path: 'callback',
            component: AuthCallbackComponent
        },
        {
            path: 'index.html',
            redirectTo: 'page1',
            pathMatch: 'full'
        }
        ,
        {
            path: '',
            redirectTo: 'page1',
            pathMatch: 'full'
        },
    ];

    @NgModule({
        imports: [RouterModule.forRoot(appRoutes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule {
    }
因此,我希望用户在同一个域上访问服务器生成的名为“register”的页面,这应该会让用户远离Angular应用程序


如何执行此操作?

如果您尝试将用户重定向到Angular的
NgZone
之外,它可能会阻止您,因此您首先需要使用
NgZone.runTask()

我建议您只创建一个组件来执行重定向。因此,当您单击链接时,它会通过路由器打开组件,然后组件重定向到外部站点

您可以尝试以下方法:

import { Component, OnInit, NgZone } from '@angular/core';

@Component({
  ...
})
export class SomeComponent implements OnInit {
  constructor(private ngZone: NgZone) {}

  ngOnInit() {
    this.ngZone.runTask(() => {
      window.location.href = 'www.yoururl.com';
    });
  }
}

想象一下,您正在使用SPA,Angular Router负责
NgZone
上下文。

现在,我在StackOverflow上找到了我自己问题的答案,占用所有路由的不是RouterModule,而是Angular PWA模块。我没有提到,因为我觉得这不相关

为了绕过Angular对相对路径(如“/register”)的控制,需要在ngsw-config.json文件中添加以下内容:

{
   "dataGroups": [
      {
        "name": "register",
        "urls": ["/register"],
        "cacheConfig": {
            "maxSize": 0,
            "maxAge": "0u",
            "strategy": "freshness"
          }
       }
   ]
}

提供一个示例,说明您的路由模块的外观,以及您用于导航到特定路由的方法,以便回答您的问题…现在添加代码,谢谢!谢谢你的回答,但事实证明是角度PWA模块引起了我的问题。