Zor*_*hgo 10 angular-new-router angular
如何在Angular 2中导航到不同的URL?我知道我们可以使用JavaScript
window.location.href ='...';
但这似乎是错误的,会导致页面刷新.我非常确定Angular 2中应该有一些功能,允许您在不刷新页面的情况下在URL之间移动.我似乎无法在文档中找到它.
提前致谢!
pun*_*nov 12
根据文档,您可以使用Router及其导航功能来更改当前状态,并在必要时传递参数:
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}]);
  }
}
这是官方文档的链接.
以下是种子项目的链接,其中包含使用路由器的一个很好的示例.
| 归档时间: | 
 | 
| 查看次数: | 22625 次 | 
| 最近记录: |