在Angular升级后,$location服务可能会被废弃,需要使用新的Location服务来替换它。为了在路由变化时更新视图,需要使用$router.navigate方法来代替$location.path方法。下面是一个示例代码:
import { Component } from '@angular/core'; import { Router } from '@angular/router';
@Component({
selector: 'app-example',
template:
})
export class ExampleComponent {
constructor(private router: Router) {}
onButtonClick() { this.router.navigate(['/another-page']); } }
在这个示例中,使用了Angular的Router服务来进行路由操作,这样就可以在路由变化时自动更新视图。