【发布时间】:2017-10-04 00:06:05
【问题描述】:
我已经使用 ngx-rocket yeoman 生成器成功地为我的 Angular 4 应用程序中的某些页面设置了路由。例如,我创建了一个名为“吃”的页面,当您访问我的主页 www.haakon.io,然后单击“吃”链接时,您将被带到该页面。如果您尝试在浏览器中键入该链接,例如 http://www.haakon.io/eat 并尝试导航到那里,您会看到 404 错误页面。我知道这与深度链接问题有关,但我尝试了一些 angular 1.5 解决方案,但它们不起作用。具体来说,我在 index.html 页面中设置了基本 href:
<base href="/"/>
这是我的 app-routing.module.ts 文件:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
// Fallback when no prior route is matched
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule { }
这是我的 route.service.ts:
import { Routes } from '@angular/router';
import { ShellComponent } from './shell/shell.component';
/**
* Provides helper methods to create routes.
*/
export class Route {
/**
* Creates routes using the shell component and authentication.
* @param routes The routes to add.
* @return {Routes} The new routes using shell as the base.
*/
static withShell(routes: Routes): Routes {
return [{
path: '',
component: ShellComponent,
children: routes
}];
}
}
最后是我的eat-routing.module.ts:
import { Route } from '../core/route.service';
import { extract } from '../core/i18n.service';
import { EatComponent } from './eat.component';
const routes: Routes = Route.withShell([
{ path: 'eat', component: EatComponent, data: { title: extract('Eat') } }
]);
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: []
})
export class EatRoutingModule { }
【问题讨论】:
标签: javascript angular angular-routing yeoman-generator