我尝试在浏览器上重定向到主页并刷新按钮点击。 我取消了app.component.ts中的更改,代码如下:
import { Component } from '@angular/core';
import { PlatformLocation } from '@angular/common'
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
landingUrl: string = "/";
constructor(private location: PlatformLocation, private router: Router) {
//redirect on back button.
location.onPopState(() => {
window.location.href = this.landingUrl;
});
//redirect on refresh button.
if (window.location.pathname != "/") {
window.location.href = this.landingUrl;
}
}
}

这有两个问题
我可以进一步改善吗?
答案 0 :(得分:2)
而不是:
window.location.href = this.landingUrl;
使用
this.router.navigate([this.landingUrl]);