How to trace routing in Angular 2?

后端 未结 3 1500
闹比i
闹比i 2020-12-08 06:11

I have component with separated file of routing settings:

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


        
3条回答
  •  孤城傲影
    2020-12-08 07:12

    In addition to devqons excellent answer: Debugging your route definitions will be much easier if you temporarily out-comment wildcard routes. Wildcard routes are handy in production to show e.g. a NotFound component, but are a pain while debugging.

    For example:

    const routes: Routes = [
        ... (your route definions)
    
        // If you have a catch-all route defined, outcomment is like below
        // {
        //     path: '**',
        //     redirectTo: '/error/not-found',
        // },
    ];
    

    After outcommenting your catch-all route, the router will not swallow your error and show in your browser console exactly what route could not be matched against your definitions.

    For example, when the following error is shown:

    core.js:4002 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'projects/123'
    Error: Cannot match any routes. URL Segment: 'projects/123'
        at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2459)
    

    you immediately known that there is a problem with matching 'projects/123' in your route definitions.

提交回复
热议问题