我从 index.html 中的 url 中删除了查询参数,但我希望在 app.component.html 中使用这些查询参数,并且我的 app.component.ts 使用 ActivatedRoute,现在当我在 index.html 中有这个脚本时,然后我的应用程序。 componen.ts 也没有收到那些 queru 参数,我怎样才能让 app.component.ts 有我的查询参数?
这是我在 index.html 中删除查询参数的脚本:
<script type="text/javascript">
var url = window.location.toString();
if(url.indexOf("?") > 0) {
var sanitizedUrl = url.substring(0, url.indexOf("?"));
window.history.replaceState({}, document.title, sanitizedUrl);
}
</script>
这是我的 app.component.ts 来解析查询参数:
import {ApiService} from './api.service';
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Converter} from './converter';
import {Title} from '@angular/platform-browser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
username = '';
department = '';
json;
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'];
}
});
}