从静态页面/ URL进行角度路由

时间:2018-08-06 02:18:40

标签: angular angular-routing

Angular 5(修订版)

经过一晚上的阅读和测试并尝试了不同的方法之后,我已经提炼了自己的问题。

问题变成了,如何从应用程序外导航到我的角度应用程序中的路径。

我得到相同的结果-键入并通过href链接到除应用程序根目录以外的任何其他路径,会导致该应用程序包括公共html

这是我能想到的最简单的情况。

dist
  |- members
  |- index.html

其中“成员”是有角度的应用程序的根-在(我尝试过没有影响)中指定

/index.html(称为主页)

<a href="members/login" >login</a>
<a href="members/register" >register</a>

应用路由器看起来像这样

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';    
import { RegisterComponent } from './components/register/register.component';
import { LoginComponent } from './components/login/login.component';
const appRoutes: Routes = [
    { path: '', component: LoginComponent, pathMatch: 'full' },
    { path: 'register', component: RegisterComponent, pathMatch: 'full' },
    { path: 'login', component: LoginComponent, pathMatch: 'full' }
    { path: '**', redirectTo: 'members/login' }
];    
@NgModule({
    imports: [
        RouterModule.forRoot( appRoutes )
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule {}

我的4种情况 1)输入“ localhost / index.html”-给我(主页)链接 2)单击任一链接-给我(主页) 3)在地址栏中输入“ localhost / members / login”或“ localhost / members / register”-给我(主页)链接 4)在地址栏中输入“ localhost / members”-给我该应用

我缺少有关路由的信息...无法解决。 我希望我能重提问题,得到答案-

请帮助 谢谢

1 个答案:

答案 0 :(得分:0)

尝试pathMatch:'full'作为默认组件

const appRoutes: Routes = [
    { path: '', component: ProfileComponent, pathMatch:'full' },
    { path: 'register', component: RegisterComponent },
    { path: 'login', component: LoginComponent },
    ...
    { path: '**', redirectTo: '/login' }
];