2016-07-14 99 views
1

我有一个非常简单的应用程序Angular2路由 - 手动网址导航

我app.component.html看起来是这样的:

<a [routerLink]="['/Test']">CLICK ME</a> 
<div class="main-container"> 
    <router-outlet></router-outlet> 
</div> 

我app.component.ts看起来是这样的:

@Component({ 
    selector: 'app', 
    templateUrl: 'app/app.component.html', 
    directives: [HomeComponent, TestComponent, ROUTER_DIRECTIVES] 
}) 

@RouteConfig([ 
    {path: '/', component: HomeComponent, as: 'HomeComponent'}, 
    {path: '/test', component: TestComponent, as: 'Test'} 
]) 

export class AppComponent { } 

导航到我的应用程序,我去

http://localhost/app 

这是完美的,它导航我到我的家庭组件视图如预期。 当我点击“按我”按钮,我浏览到

http://localhost/app/test 

和预期的一样我的测试组件呈现......但是,如果我手动导航到

http://localhost/app/test 

我家成分被加载而不是我的测试组件...什么给了?我如何设置路由,以便手动导航到测试网址实际上在路由器插座中返回测试组件的视图?这可能吗?我不想去,每次登陆页面...

+0

您是否在index.html中设置了您的基础href?它应该像[文档建议](https://angular.io/docs/ts/latest/guide/router.html#!#base-href)'' –

+0

什么是你的angular2版本? –

+0

beta17,基础href是好的,我有一个重新编写规则自动服务/app/index.html,我认为是需要获得PathLocationStrategy工作...但它不...使用节点精简版服务器 – Mames

回答

1

在新的路由器especification你需要的东西是这样的:

router.navigateByUrl("/app"); 

router.navigate(['HomeComponent'], {relativeTo: route}); 
0

这可能是由于到服务器配置。对于上下文之后的任何错误路径或路径,您的服务器可能会重定向到index.html

您应该将服务器配置为重写路径而不是重定向。

提供服务器上的代码中使用了

+0

你是正确的,在我的服务器web.config我有 <条件logicalGrouping =”MatchAll“> <动作类型= “重写” URL = “/应用/ index.html中”/> 卸下此线和检测 - 我现在我404刷新上的HTTP后://localhost/app/test链接 – Mames

+0

正如我所说的,你需要配置重写路径而不是重定向。你正在使用哪个服务器可能会帮助建议如何重写 –

+0

对于Apache tomcat:https://tomcat.apache.org/ tomcat-8.0-doc/rewrite.html –

0

解决更多的信息和服务器 - 我的引导是缺少一些内容......我有

bootstrap(AppComponent, [ 
    ROUTER_BINDINGS, 
    bind(APP_BASE_HREF).toValue(location.pathname), 
    bind(LocationStrategy).toClass(PathLocationStrategy) 
    ]); 

然而,这是不正确的,手动输入:

bootstrap(AppComponent, [ 
    ROUTER_BINDINGS, 
    bind(APP_BASE_HREF).toValue("/"), 
    bind(LocationStrategy).toClass(PathLocationStrategy) 
    ]); 

似乎工作。感谢大家的帮助!