在Angular中,当以编程方式更新URL时,queryParams不起作用的问题可能是由于使用了不正确的导航方法导致的。下面是一个解决方法的示例代码:
首先,确保在使用queryParams时,使用的是Router.navigate方法而不是Router.navigateByUrl方法。
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
  selector: 'app-example',
  template: `
    
  `,
})
export class ExampleComponent {
  constructor(private router: Router) {}
  updateUrl() {
    const queryParams = { key: 'value' };
    // 使用Router.navigate方法而不是Router.navigateByUrl
    this.router.navigate([], { queryParams });
  }
}
在这个示例中,我们在updateUrl方法中使用Router.navigate方法来更新URL,同时传递了一个包含queryParams的对象。这样,当按钮被点击时,URL将被更新,并且queryParams将会生效。
确保在你的应用中使用类似的方法,并且在导航时使用Router.navigate方法而不是Router.navigateByUrl方法,这样queryParams就会起作用了。