• angular跳转和传参


    使用routerLink跳转

    • <a routerLink=["/exampledetail",id]></a>
    • <a routerLink=["/exampledetail",{queryParams:object}] ></a>

    使用navigate跳转

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

    1. this.router.navigate(['user', 1]);
      以根路由为起点跳转
    2. this.router.navigate(['user', 1],{relativeTo: route});
      默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute
    3. this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
      路由中传参数 /user/1?id=1
    4. this.router.navigate(['view', 1], { preserveQueryParams: true });
      默认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1
    5. this.router.navigate(['user', 1],{ fragment: 'top' });
      路由中锚点跳转 /user/1#top
    6. this.router.navigate(['/view'], { preserveFragment: true });
      默认值为false,设为true,保留之前路由中的锚点/user/1#top to /view#top
    7. this.router.navigate(['/user',1], { skipLocationChange: true });
      默认值为false,设为true路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效
    8. this.router.navigate(['/user',1], { replaceUrl: true });
      未设置时默认为true,设置为false路由不会进行跳转

    获取query方式的参数

    import { ActivateRoute } from '@angular/router';
    public id: any;
    snapshot
    constructor( public activeRoute: ActivateRoute ) { };
    ngOnInit(){
        this.id= this.activeRoute.snapshot.params['id'];
    };
    queryParams
        ngOnInit(){
            this.activeRoute.queryParams.subscribe(params => {
            this.id = params['id'];
        });
    

      

  • 相关阅读:
    aws s3文件上传设置accesskey、secretkey、sessiontoken
    HTTP的Referrer和Referrer Policy设置
    小技巧-mac修改finder菜单栏
    使用katalon自带Spy功能获取/验证控件Selector、XPath
    java的8种基础类型
    Mac-搭建Hadoop集群
    新公司入职56天后的面谈小结
    Java对字符串加密并返回星号※
    为什么要写设计文档
    在Linux上部署Web项目
  • 原文地址:https://www.cnblogs.com/mary-123/p/11095981.html
Copyright © 2020-2023  润新知