代码之家  ›  专栏  ›  技术社区  ›  Arryn

如果发现任何无效Url,Angular 4+Universal重定向到404页面

  •  1
  • Arryn  · 技术社区  · 7 年前

      {
    path: '',
    component: FullLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: '',
        loadChildren: 'app/core/components/static/static.module#StaticModule'
      },
      {
        path: 'move',
        loadChildren: 'app/core/components/move/move.module#MoveModule'
      },
      {
        path: 'account',
        loadChildren: 'app/core/components/account/account.module#AccountModule'
      },
      { path: "**",redirectTo:"404"}
    ]
    

    }

    任何线索都肯定会有很大帮助。

    谢谢

    2 回复  |  直到 7 年前
        1
  •  2
  •   Can    6 年前

    export class NotFoundComponent implements OnInit {
    
      constructor(@Inject(PLATFORM_ID) private platformId: Object,
                  @Optional() @Inject(RESPONSE) private response: Response) {
      }
    
    
      ngOnInit() {
        if (!isPlatformBrowser(this.platformId)) {
          this.response.status(404);
        }
      }
    
    }
    

    app.engine('html', (_, options, callback) => {
      renderModuleFactory(AppServerModuleNgFactory, {
        // Our index.html
        document: template,
        url: options.req.url,
        extraProviders: [
          // make req and response accessible when angular app runs on server
          <ValueProvider>{
            provide: REQUEST,
            useValue: options.req
          },
          <ValueProvider>{
            provide: RESPONSE,
            useValue: options.req.res,
          },
        ]
      }).then(html => {
        callback(null, html);
      });
    })
    

    由于我自己刚刚偶然发现这个问题,我在一篇博客文章中记录了我的过程。如果您需要更多指导,请查看:

    https://blog.thecodecampus.de/angular-universal-handle-404-set-status-codes/

        2
  •  0
  •   Raaj Dubey    7 年前

      {
    path: '',
    component: FullLayoutComponent,
    data: {
     title: 'Home'
    },
    children: [
      {
       path: '',
       loadChildren:'app/core/components/static/static.module#StaticModule'
    },
    {
     path: 'move',
     loadChildren: 'app/core/components/move/move.module#MoveModule'
    },
    {
     path: 'account',
     loadChildren: 'app/core/components/account/account.module#AccountModule'
    }
    
     ]
    },
    
    { path: "**",redirectTo:"404"}