Angular 2 路由器在路由时刷新页面
Angular 2 router refreshing page while routing
我正在尝试设置一个简单的路由,同时学习 Angular 2,每当我点击一个 link,浏览器被路由到新路由但是浏览器正在重新请求所有资源(不表现为单页应用程序)。
我的索引文件,
<!--... . . 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{}
我做错了什么? Angular 版本是 2.0.0-beta.7
.
感谢您的任何见解。
您应该使用 routerLink
指令导航到路线:
<a [routerLink]="['Link']">link</a>
此指令可直接使用,因为您将 ROUTER_DIRECTIVES
指定到组件的 directives
属性中
我正在尝试设置一个简单的路由,同时学习 Angular 2,每当我点击一个 link,浏览器被路由到新路由但是浏览器正在重新请求所有资源(不表现为单页应用程序)。
我的索引文件,
<!--... . . 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{}
我做错了什么? Angular 版本是 2.0.0-beta.7
.
感谢您的任何见解。
您应该使用 routerLink
指令导航到路线:
<a [routerLink]="['Link']">link</a>
此指令可直接使用,因为您将 ROUTER_DIRECTIVES
指定到组件的 directives
属性中