2

尝试在 Angular 2 上设置一个简单的测试应用程序我快疯了。我有 3 个页面/组件要使用新的组件路由器加载。我已经让它半工作了。目前我有两个问题...

1 -如果您键入要转到 Ex 的组件的路径。“ http://127.0.0.1:8080/Test2 ”没有加载我的 Test2 页面,而是说找不到该路由。

2 -当尝试加载不同的页面时,我第一次点击链接时,它会正确加载页面。当我单击链接时,所有其他页面都会抛出异常。

这是我的设置:

应用程序.ts

import {Component, bootstrap, provide} from 'angular2/angular2';
import {ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig, APP_BASE_HREF} from 'angular2/router';

import {Test1} from './Test1';
import {Test2} from './Test2';
import {Test3} from './Test3';

@Component({
    selector: 'my-app',
    template: '<test1></test1><router-outlet></router-outlet>',
    directives: [ROUTER_DIRECTIVES, Test1, Test2]
})

@RouteConfig([
  {path: '/', as: 'Home', component: Test1},
  {path: '/Test2', as: 'Test2', component: Test2},
  {path: '/Test3', as: 'Test3', component: Test3}   
])

class AppComponent {
  constructor() {}
 }

bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(APP_BASE_HREF, { useValue: '/' })]); 

测试1.ts

import {Component, bootstrap} from 'angular2/angular2';
import {RouterLink} from 'angular2/router';

@Component({
    selector: 'test1',
    templateUrl: 'app/Test1.html',
    directives: [RouterLink] 
})
export class Test1 { }

测试1.html

<h1> Test 1 </h1>
<a [router-link]="['/Test2']">Test Link 2</a>

测试2.ts

import {Component, bootstrap} from 'angular2/angular2';
import {RouterLink} from 'angular2/router';

@Component({
    selector: 'test2',
    templateUrl: 'app/Test2.html',
    directives: [RouterLink]
})
export class Test2 { }

测试2.html

<h1> Test 2 </h1>
<a [router-link]="['/Test3']">Test Link 3</a>

测试3.ts

import {Component, bootstrap} from 'angular2/angular2';
import {RouterLink} from 'angular2/router';

@Component({
    selector: 'test3',
    templateUrl: 'app/Test3.html',
    directives: [RouterLink]
})
export class Test3 { } 

测试3.html

<h1> Test 3 </h1>
<a [router-link]="['/Test2']">Test Link 2</a>

谢谢你们的帮助!

Yakov 的 LocationStrategy 建议解决了这个问题。对于任何有同样问题的人, https://github.com/zorthgo/Angular2QuickstartTest上的完整工作代码和我的 App.ts 的工作版本如下所示:

import {Component, bootstrap, provide} from 'angular2/angular2';
import {ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig, APP_BASE_HREF, LocationStrategy, HashLocationStrategy} from 'angular2/router';

import {Test1} from './Test1';
import {Test2} from './Test2';
import {Test3} from './Test3';

@Component({
    selector: 'my-app',
    template: ` <a [router-link]="['/Home']">Test1 Link</a>
                <a [router-link]="['/Test2', {id: 3}]">Test2 Link</a>
                <a [router-link]="['/Test3']">Test3 Link</a>
                <router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES, Test1, Test2,Test3]
})

@RouteConfig([
  {path: '/',          component: Test1, as: 'Home'},
  {path: '/Test2/:id', component: Test2, as: 'Test2'},
  {path: '/Test3',     component: Test3, as: 'Test3'}

])

class AppComponent {
  constructor() { }
 }

bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })]);
4

1 回答 1

5

我做了类似的事情并且有效。试试这个:

bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})]);
于 2015-10-24T15:19:39.333 回答