要在Angular 7中实现在相同的URL上进行完全重载,您可以使用路由重定向的方式来实现。下面是一个示例代码:
首先,在您的路由配置文件中,定义一个重定向路由,将原始URL重定向到自己本身:
const routes: Routes = [
{ path: 'your-url', redirectTo: 'your-url', pathMatch: 'full' },
// 其他路由配置...
];
然后,在您的组件中,您可以使用Router
服务手动触发重定向:
import { Router } from '@angular/router';
@Component({
// 组件配置...
})
export class YourComponent {
constructor(private router: Router) { }
reloadPage() {
this.router.navigate(['/your-url'], { skipLocationChange: true }).then(() => {
this.router.navigate(['/current-url']);
});
}
}
在上述示例中,reloadPage
方法通过先导航到其他路由('/your-url'
),再导航回当前路由('/current-url'
)来实现页面的完全重载。skipLocationChange
选项用于跳过浏览器历史记录中的新记录。
最后,您可以在需要触发页面重载的地方调用reloadPage
方法。