简体   繁体   English

如何使用角度4中的路由正确导航页面?

[英]How to properly navigate pages using routing in angular 4?

'app.routing.ts' File 'app.routing.ts'文件

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

//Layouts
import { FullLayoutComponent } from './layouts/full-layout.component';

//pages
import { DashboardComponent } from './pages/dashboard.component'
import { UploadFileComponent } from './pages/UploadFile.component'


export const routes: Routes = [
  {
    path: '',
    redirectTo: 'full-layout',
    pathMatch: 'full',
  },
  {
    path: '',
    component: FullLayoutComponent,
    data: {
      title: 'Dashboard'
    }
  },
   {
    path: 'UploadFile',
    component: UploadFileComponent,
    data: {
      title: 'UploadFile'
    }
  },
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

'full-layout.component.html' File 'full-layout.component.html'文件

<a class="nav-link" routerLinkActive="active" [routerLink]="['UploadFile']">
  <i class="icon-star"></i> Upload
</a>

I want to redirect to UploadFile.component.html by clicking a link. 我想通过单击链接重定向到UploadFile.component.html Though this redirection is working, but layout(master-page) is not loaded in this page so I think it is redirecting only on this page. 尽管此重定向有效,但该页面未加载layout(master-page),因此我认为它仅在此页面上重定向。 I'm referencing this page from side-bar menu but after redirection display only 'UploadFile' page will open but it will not load layout in this page. 我是从侧栏菜单中引用此页面的,但是在重定向显示后,仅“ UploadFile”页面将打开,但不会在此页面中加载布局。

Try routerLink="/UploadFile" 尝试routerLink =“ / UploadFile”

<a class="nav-link" routerLinkActive="active" routerLink="/UploadFile"><i class="icon-star"></i> Upload</a

and use lowercase routes 并使用小写路由

I would change it to this: 我将其更改为:

export const routes: Routes = [
  {
    path: 'uploadfile',
    component: UploadFileComponent,
    data: {
      title: 'UploadFile'
    }
  },
  {
    path: 'full-layout',
    component: FullLayoutComponent,
    data: {
      title: 'Dashboard'
    }
  },
  {
    path: '',
    redirectTo: 'full-layout',
    pathMatch: 'full',
  },
];

Please note the order of your routes. 请注意您的路线顺序。 The router executes the first route which matches the current route. 路由器执行与当前路由匹配的第一条路由。 This is the reason why you shouldn't have to routes with path: '' . 这就是为什么您不必使用path: ''进行路由的原因。

The redirectTo: 'foo' property redirects to the path that is given in the path: 'foo' property. redirectTo: 'foo'属性重定向到path: 'foo'属性中给定的path: 'foo'

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

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