我试图更新(添加,删除)queryParams从一个组件。在angularJS中,它曾经是可能的,这要归功于:

$location.search('f', 'filters[]'); // setter
$location.search()['filters[]'];    // getter

我有一个应用程序的列表,用户可以过滤,顺序等,我想设置在url的queryParams所有过滤器激活,这样他就可以复制/粘贴url或与他人共享。

但是,我不希望每次选择筛选器时都重新加载页面。

新路由器能做到吗?


当前回答

如果你想改变查询参数而不改变路由。见下文 下面的例子可能对你有帮助: 当前路由为:/search &目标路由是(没有重载页面):/search?查询=爱

    submit(value: string) {
      this.router.navigate( ['.'],  { queryParams: { query: value } })
        .then(_ => this.search(q));
    }
    search(keyword:any) { 
    //do some activity using }

请注意:您可以使用this.router。导航(['search']而不是this.router。导航((“。”)

其他回答

首先,我们需要从angular router导入router模块,并声明它的别名

import { Router } from '@angular/router'; ---> import
class AbcComponent implements OnInit(){
constructor(
    private router: Router ---> decalre alias name
  ) { }
}

1. 您可以使用“router”更改查询参数。函数并传递查询参数

this.router.navigate([], { queryParams: {_id: "abc", day: "1", name: "dfd"} 
});

它将更新当前即激活路由中的查询参数

下面将重定向到abc页面_id,日 和name作为查询参数 this.router。导航([' / abc '], {queryParams: {_id:“abc”,天:“1”,名称: “目前”} }); 它将更新“abc”路由中的查询参数以及三个查询参数

获取查询参数:-

    import { ActivatedRoute } from '@angular/router'; //import activated routed

    export class ABC implements OnInit {

    constructor(
        private route: ActivatedRoute //declare its alias name
      ) {}

    ngOnInit(){
       console.log(this.route.snapshot.queryParamMap.get('_id')); //this will fetch the query params
    }

Angular的Location服务应该在与浏览器的URL交互时使用,而不是用于路由。这就是为什么我们要使用位置服务。

angular HttpParams用于创建查询参数。请记住HttpParams是不可变的,这意味着在创建值时它必须被链接。

最后,使用this._location。replaceState更改为URL,而无需重新加载页面/路由和原生js位置。获取没有参数的url的路径,以每次重置参数。

constructor(
    private _location: Location,
) {}

...

updateURLWithNewParamsWithoutReloading() {
    const params = new HttpParams().appendAll({
        price: 100,
        product: 'bag'
    });

    this._location.replaceState(
        location.pathname,
        params.toString()
    );
}

我遇到过一个有趣的情况,我们对所有的路线只使用了一个组件。这是路线的样子:

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    children: [
      { path: 'companies', component: HomeComponent },
      { path: 'pipeline', component: HomeComponent },
      // ...
    ]
  },
  // ...
];

基本上,paths / /companies和/pipeline都有相同的组件需要加载。而且,由于Angular会阻止组件在DOM中之前被加载,所以Router的navigate方法会返回一个总是以null解析的Promise。

为了避免这种情况,我不得不使用onSameUrlNavigation。通过设置这个值为'reload',我设法使路由器导航到相同的URL与更新的查询字符串参数:

@NgModule({
  imports: [RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload' })],
  exports: [RouterModule]
})

如果你想改变查询参数而不改变路由。见下文 下面的例子可能对你有帮助: 当前路由为:/search &目标路由是(没有重载页面):/search?查询=爱

    submit(value: string) {
      this.router.navigate( ['.'],  { queryParams: { query: value } })
        .then(_ => this.search(q));
    }
    search(keyword:any) { 
    //do some activity using }

请注意:您可以使用this.router。导航(['search']而不是this.router。导航((“。”)

在Angular 5中,你可以通过解析当前url来轻松获取和修改urlTree的副本。这将包括查询参数和片段。

  let urlTree = this.router.parseUrl(this.router.url);
  urlTree.queryParams['newParamKey'] = 'newValue';

  this.router.navigateByUrl(urlTree); 

修改查询参数的“正确方法”可能是使用 创建一个新的UrlTree从当前,同时让我们修改它使用NavigationExtras。

import { Router } from '@angular/router';

constructor(private router: Router) { }

appendAQueryParam() {

  const urlTree = this.router.createUrlTree([], {
    queryParams: { newParamKey: 'newValue' },
    queryParamsHandling: "merge",
    preserveFragment: true });

  this.router.navigateByUrl(urlTree); 
}

为了以这种方式删除查询参数,可以将其设置为undefined或null。