简体   繁体   English

角度2/4:路由不起作用

[英]Angular 2/4: Routing is not working

My routing 我的路线

   RouterModule.forRoot([
            { path: 'login', component: LoginComponent },
            { path: 'home', component: HomeComponent },
        ])

In app component I have this.router.navigate(['home']); 在应用程序组件中,我有this.router.navigate(['home']); This doesn't navigate me to home component. 这不会使我导航到home组件。

在此处输入图片说明

When I routed to login page, my left nav is also displayed. 当我转到登录页面时,也会显示我的左侧导航。

My app.component.ts 我的app.component.ts

<div class='container-fluid'>
    <div class='row'>

        <div class='col-sm-3'>
            <nav-menu></nav-menu>
        </div>

        <div class='col-sm-9 body-content'>
            <router-outlet>
                <input style="float:right" placeholder="Search" autofocus>
            </router-outlet>
        </div>

    </div>
</div>

This Error is offently this one. 这个错误是这个错误。

You got many other module loading before, and so one is already defining an empty path, so router doesn't put your route to your module. 您之前加载了许多其他模块,因此已经在定义一个空路径,因此路由器不会将您的路由放到模块中。 (order of your modules imported matters). (模块导入顺序很重要)。 if you are lazy loading this module, check the order of other lazy loaded module, then check the order you put your RoutingModule into your module (order here matters to). 如果您要延迟加载此模块,请检查其他延迟加载的模块的顺序,然后检查将RoutingModule放入模块的顺序(此处的顺序很重要)。 Angular offical doc advice to put this as first, the first route matching url is rendered. Angular官方文档建议将其放在第一位,将呈现第一个与URL匹配的路由。

Try with this: 试试这个:

Create new file named as app.routing.ts 创建名为app.routing.ts的新文件

import { LoginComponent } form ...
import { HomeComponent } from ..

export const routing = RouterModule.forRoot([
    { path: 'login', component: LoginComponent },
    { path: 'home', component: HomeComponent }   
]);

//And Finaly in app.module.ts
//include the constant like this
import { routing } from './app.routing';

..... .....

@NgModule({
   imports: [routing]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM