【问题标题】:Angular 2 child router doesn't workAngular 2子路由器不起作用
【发布时间】:2017-03-03 13:41:28
【问题描述】:

我的项目结构是这样的:

登录 -> 仪表板 -> 主页

Home 是仪表板的子级。

我想要的是在用户未登录时重定向到 Login 页面。警卫类是:

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Meteor } from 'meteor/meteor';

@Injectable()
export class CanActivateGuard implements CanActivate {

  constructor() {}

  canActivate() {
    return Meteor.userId() != null;
  }
}

仪表板模块:

import { CanActivateGuard } from '../activate-guard';

@NgModule({
  providers: [
    CanActivateGuard
  ],

仪表板路由器:

import { CanActivateGuard } from '../activate-guard';

export const DASHBOARD_ROUTES: Route[] = [
  { 
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [CanActivateGuard],
    children: [
      ...HOME_ROUTES
    ]

家用模块:

import { CanActivateGuard } from '../../activate-guard';

@NgModule({
  providers: [
        CanActivateGuard
  ],

家用路由器:

import { CanActivateGuard } from '../../activate-guard';

export const HOME_ROUTES : Route[] = [
  { path: 'home', component: HomeComponent, canActivate: [CanActivateGuard] }
]

如果我的网址是:http://localhost:3000/dashboard,这可以正常工作,它将重定向到登录页面。但是如果url是:http://localhost:3000/dashboard/home就会变成黑屏。

我真的不明白发生了什么。任何想法?谢谢。

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    我在单个路由 file.ts 中定义了类似这样的路由,这对我来说很好用 常量 appRoutes: 路由 = [ { 小路: '', 重定向到:'/登录', 路径匹配:'完整' }, { 路径:'登录', 组件:登录组件 },

    {
        path: 'DashBoard',
        component: DashboardComponent ,
        children: [
            { path: '', component: DashboardComponent },
            { path: 'Home', component: HomeComponent},
    
    
    
        ]
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2017-01-01
      • 1970-01-01
      • 2017-05-05
      • 2016-09-20
      • 1970-01-01
      相关资源
      最近更新 更多