Page reloading on child routing

别说谁变了你拦得住时间么 提交于 2019-12-13 17:36:54

问题


Using Angular 2 Router. I have a 2 level routing (root routing and child routing]

My problem is that when navigating to a child route the page is reloading after the Child is being loaded.

child level routing

const childRoutes: Routes = [
   {
    path: '',
    component: BaseComponent,
    children: [
           {
            path: '',
            component: DetailsComponent
           },
           {
               path: 'other',
               component: OtherComponent
           }
       ]
   }
];

export const childRouting = RouterModule.forChild(childRoutes);

Top Level Routing

const appRoutes: Routes = [
    {
        path: '',
        redirectTo: '/overview',
        pathMatch: 'full'},
    {
        path: 'overview',
        component: OverviewComponent},
    {
        path: 'sub',
        //This is the module which the childRoutes belongs to.
        loadChildren: 'app/components/sub.module#SubModule'
    }
];

export const appRoutingProviders: any[] = [];

export const routing = RouterModule.forRoot(appRoutes);

call for navigation from DetailsComponent

export class DetailsComponent implements OnInit {
    constructor(private router: Router) { }

    ngOnInit() { }

    onSubmit() {
        this.router.navigate(['/sub/other'])
    }
}

p.s

I'm trying to keep this question short so if more code needed then please let me know and i will gladly add it.


回答1:


It's caused by the default browser submit behavior. Either call preventDefault(), return false in the event handler or add type="button" to prevent the submit event default behavior.



来源:https://stackoverflow.com/questions/39001709/page-reloading-on-child-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!