Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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:单击routerLink时如何在auth.guard.ts中获取url_Angular - Fatal编程技术网

Angular:单击routerLink时如何在auth.guard.ts中获取url

Angular:单击routerLink时如何在auth.guard.ts中获取url,angular,Angular,我想在单击由routerLink创建的链接时获取auth.guard.ts文件中的URL。请参见.html文件中的以下我的代码: <li><a href="javascript:void(0)" routerLink="genre" routerLinkActive="active">Genre</a></li> <li><a href="javascript:void(0)" routerLink="category" rout

我想在单击由
routerLink
创建的链接时获取auth.guard.ts文件中的URL。请参见
.html
文件中的以下我的代码:

<li><a href="javascript:void(0)" routerLink="genre" routerLinkActive="active">Genre</a></li>
<li><a href="javascript:void(0)" routerLink="category" routerLinkActive="active">Category</a></li>
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { 
  console.log('URL = ' + state.url)
  ...
}
只有在刷新浏览器/页面时,我才能在控制台中看到URL。但当我点击上面指定的“类别”或“流派”之类的链接时,控制台上什么也没有显示

以下是我的路由文件:

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

import { PagesComponent } from './pages.component';
import { AuthGuard } from '../_guards/auth.guard';

const routes: Routes = [{
  path: '',
  component: PagesComponent, canActivate: [ AuthGuard ],
  children: [
      {
        path: 'genre',
        loadChildren: () => import('./genre/genre.module')
          .then(m => m.GenreModule),
      },
      {
        path: 'category',
        loadChildren: () => import('./category/category.module')
          .then(m => m.CategoryModule),
      },
  ]
},

]

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

您是否可以尝试在子路由以及下面添加canActivate:[AuthGuard]

children: [
      {
        path: 'genre',
        canActivate: [ AuthGuard ],
        loadChildren: () => import('./genre/genre.module')
          .then(m => m.GenreModule),
      },
      {
        path: 'category',
        canActivate: [ AuthGuard ],
        loadChildren: () => import('./category/category.module')
          .then(m => m.CategoryModule),
      },
  ]

您是否可以尝试在子路由以及下面添加canActivate:[AuthGuard]

children: [
      {
        path: 'genre',
        canActivate: [ AuthGuard ],
        loadChildren: () => import('./genre/genre.module')
          .then(m => m.GenreModule),
      },
      {
        path: 'category',
        canActivate: [ AuthGuard ],
        loadChildren: () => import('./category/category.module')
          .then(m => m.CategoryModule),
      },
  ]

您还可以共享路由器配置吗?@LaminooLawrance如果您能提及我需要共享的while文件,那就太好了。您正在使用的路由文件[authGuard]@LaminooLawrance添加了路由文件。您还可以共享路由器配置吗?@LaminooLawrance如果您能提及我需要共享的文件,那就太好了。您正在使用的路由文件[authGuard]@LaminooLawrance添加的路由文件不起作用。我单击了流派中的类别链接,没有控制台。在PagesComponent中删除canActivate,然后tryI在单个组件路由中添加了canActivate:[AuthGuard],它无法工作。我点击了流派中的分类链接,没有控制台。在PagesComponent中删除canActivate,tryI在单个组件路由中添加了canActivate:[AuthGuard],它就可以工作了