在我的 url 中解析查询参数后隐藏查询参数



我的前端在 http://host:4200,当另一个前端想要打开它时,例如使用window.open(http://host:4200?username=a&Department=b(,所以这个http://host:4200?username=a&Department=b将在地址栏中,如何从此 URL 中删除查询参数?

如果我使用这两行代码来Hie查询参数,它可以工作但有延迟,因为我在读取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'];
      }
    });
  }
您可以将

skipurlchange 属性与 router.navigate

一起使用
constructor(private router:Router){}
this.router.navigate(['/yourview'], { queryParams:  youParam ,skipLocationChange: true });

最新更新