我的前端在http://host:4200中,并且当另一个前端要使用例如window.open(http://host:4200?username=a&department=b)打开它时,此http://host:4200?username=a&department=b将在地址栏中, 如何从该网址中删除查询参数?
如果我使用这两行代码来隐藏查询参数,则它可以工作,但会延迟,因为我在读取url之后将其删除,因此第二个查询参数可见:
const url1 = window.location.toString();
const sanitizedUrl = url1.substring(0, url1.indexOf('?'));
window.history.replaceState({}, document.title, sanitizedUrl);
这是我的cpp.component.ts代码,我需要在其中读取查询参数,它的角度为5,
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
username = '';
department = '';
constructor(
private feedbackService: ApiService,
private titleService: Title,
private route: ActivatedRoute) {
}
ngOnInit() {
this.route.queryParams.subscribe(params => {
if (params.hasOwnProperty('username')) {
this.username = params['username'];
}
if (params.hasOwnProperty('department')) {
this.department = params['department'];
}
});
}
答案 0 :(得分:0)
您可以将skipurlchange
属性与router.navigate
一起使用
constructor(private router:Router){}
this.router.navigate(['/yourview'], { queryParams: youParam ,skipLocationChange: true });