zoukankan      html  css  js  c++  java
  • [Angular] Preserve the current route’s query parameters when navigating with the Angular Router

    When we redirect to a different route from within our component's code using the Router.navigate or from within a component template via a [routerLink] directive, we may want to preserve the current route’s query parameters and carry them on to the next route. In this lesson we'll learn about the router's preserveQueryParams option as well as the [queryParams] directive on the [routerLink].

    If you using template syntax to do the routing:

    import { Component, OnInit } from '@angular/core';
    import { PeopleService } from './people.service';
    import { ActivatedRoute } from '@angular/router';
    
    @Component({
      selector: 'app-people-list',
      template: `
        <h3>People</h3>
        <ul>
          <li *ngFor="let person of people | async">
            <a [routerLink]="[person.id]" [queryParams]="activatedRoute.queryParams | async">{{ person.name }}</a>
          </li>
        </ul>
      `,
      styles: []
    })
    export class PeopleListComponent implements OnInit {
      people;
    
      constructor(
        private peopleService: PeopleService,
        public activatedRoute: ActivatedRoute
      ) {}
    
      ngOnInit() {
        this.people = this.peopleService.getAll();
      }
    }

    You can directly bind queryParams form the activeRoute.

    If you doing routing from the component:

    onSave(personName) {
        this.person.name = personName;
        this.peopleService.save(this.person).subscribe(() => {
          // redirect back people list
          this.router.navigate(['../'], {
            relativeTo: this.activatedRoute,
            preserveQueryParams: true
          });
        });
      }
  • 相关阅读:
    atom 震动特效
    CSRF和XSS
    解决remove @override annotation(jdk1.5和jdk1.6)
    JDK 工具列表
    解决Win10系统backgroundTaskHost占用cpu大
    ideaIU-15.0.2 注册码
    jprofiler_windows-x64_9_1注册码
    修改ligerui 默认确认按钮
    解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
    安装 Flex2packagebeta_1.994
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12319402.html
Copyright © 2011-2022 走看看