Angular 2路由器在路由时刷新页面

时间:2016-03-02 06:22:51

标签: angular typescript angular2-routing

我尝试在学习Angular 2的同时设置一条简单的路线,每当我点击一个链接时,浏览器就会被路由到新的路线,但是浏览器正在重新请求所有资源(不像单页面应用程序那样) )。

我的索引文件

<!--... . .  various scripts and styles . . . . . --->
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });
  System.import('app/main')
        .then(null, console.error.bind(console));
</script>
</head>
<body>
  <app></app>
</body>

应用来源,

main.ts

import {bootstrap}    from 'angular2/platform/browser';
import {RoutingApp} from './routing/routing.app'
import {ROUTER_PROVIDERS} from 'angular2/router'

bootstrap(RoutingApp, [ROUTER_PROVIDERS]);

RoutingApp

import {Component} from "angular2/core"
import {RouteComponent} from './route.component'
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';


@Component({
    selector : 'app',
    template :  `
        go to this <a href="/link">link</a>
        <br />
        <router-outlet></router-outlet>

    `,
    directives: [ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})
@RouteConfig([
    {path: '/link', name: 'Link', component: RouteComponent}
])
export class RoutingApp{

}

RouteComponent

import {Component} from 'angular2/core'

@Component({
    template: `
        hello from RouteComponent
    `
})
export class RouteComponent{}

我做错了什么?角度版本为2.0.0-beta.7

感谢您的任何见解。

1 个答案:

答案 0 :(得分:7)

您应该使用function boolean checkIfCarManual(Vehicle vehicle){ return vehicle.isCar() && ((Car)vehicle).isManual(); } 指令导航到路线:

routerLink

此指令可直接使用,因为您已将<a [routerLink]="['Link']">link</a> 指定到组件的ROUTER_DIRECTIVES属性中