这是我的项目目录结构:
--root
--app
--app.ts
--boot.ts
--index.html
--node_modules
这是我的代码:
boot.ts
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})
]);
app.ts
@RouteConfig([
{path: '/', name: 'root', redirectTo: ['/pageA']},
{path: '/page-a', name: 'pageA', component: PageA},
{path: '/page-b', name: 'pageB', component: PageB}
])
html文件:
<head>
<base href="/">
<script src="node_modules/angular2/bundles/router.dev.js"></script>
...
</head>
它有效,但当我看到控制台时,它会告诉我"http://localhost:63342/#/page-a Failed to load resource: the server responded with a status of 404 (Not Found)"
此外,当我刷新页面时,它显示&#34; 404未找到&#34;页。
答案 0 :(得分:1)
我会使用useAsDefault
代替redirectTo
:
@RouteConfig([
{path: '/page-a', name: 'pageA', component: PageA, useAsDefault: true},
{path: '/page-b', name: 'pageB', component: PageB}
])
对于HashLocationStrategy策略,不需要使用...
答案 1 :(得分:0)
最后通过两种方式解决了问题,强烈建议采用第一种方式。
1.请参阅此帖子了解详情:Angular 2 router no base href set
dependencies
2.没有设置<base href="/root/">
,我还无法弄清楚原因。
3.来自文档:
<base href...>
从官方文档和示例中获得提示:
https://angular.io/docs/ts/latest/guide/router.html
http://plnkr.co/edit/?p=preview
文件说明:
<script>document.write('<base href="' + document.location + '" />');</script>