Angular 2 等于 window.location.href?

Angular 2's equivalent to window.location.href?

如何导航到 Angular 2 中的另一个 URL?我知道我们可以使用 JavaScript's

window.location.href = '...';

但这似乎是错误的,会导致页面刷新。我很确定 Angular 2 中应该有允许您在 URL 之间移动而无需刷新页面的功能。我似乎无法在文档中找到它。

提前致谢!

根据文档,您可以使用 Router 及其 navigate 函数来更改当前状态,并传递参数,如果必要的:

import {Component, ...} from 'angular2/angular2';
import {Router, ...} from 'angular2/router';
import {HomeCmp} from '../home/home';

@Component({
  selector: 'app',
  // params of your component here
})
@RouteConfig([
  { path: '/', component: HomeCmp, as: 'MyHome' },
  // your other states here
])

export class AppCmp {
  router: Router;
  constructor(router: Router) {
    this.router = router;
  }
  navigateToHome() {
    // for example, that's how we can navigate to our home route
    this.router.navigate(['./MyHome', {param: 3}]);
  }
}

这里是 link 到 official documentation

这里是一个 link 种子项目,其中有一个很好的使用路由器的例子。