前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Angular2 路由跳转

Angular2 路由跳转

作者头像
用户1437675
发布2019-05-15 11:41:12
2.7K0
发布2019-05-15 11:41:12
举报
文章被收录于专栏:Angular&服务Angular&服务

1.通过ts来实现

代码语言:javascript
复制
//EmailComponent 

import { Component, OnInit} from '@angular/core';
//route是ActivatedRoute的实例,使用需要导入ActivatedRoute
import {Router,ActivatedRoute} from '@angular/router';

@Component({
    selector: 'app-email',
    templateUrl: './email.component.html',
    styleUrls: ['./email.component.scss']
})
export class EmailComponent implements OnInit{

    constructor(public router: Router) { }

    //通过点击页面routeIsActive 方法来实现跳转
    routeIsActive(routePath: string) {
        this.router.navigateByUrl("routePath","wuuwu")
        //1.以根路由为起点跳转路径为   routePath/wuuwu

        this.router.navigate(['user', 1],{relativeTo: route}); 
        //2.默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

        this.router.navigate(['user', 1],{ queryParams: { id: 1 } }); 
        //3.路由中传参数 /user/1?id=1

        this.router.navigate(['view', 1], { preserveQueryParams: true }); 
        //4.默认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1

        this.router.navigate(['user', 1],{ fragment: 'top' }); 
        //5.路由中锚点跳转 /user/1#top

        this.router.navigate(['/view'], { preserveFragment: true }); 
        //6.默认值为false,设为true,保留之前路由中的锚点/user/1#top to /view#top

        this.router.navigate(['/user',1], { skipLocationChange: true }); 
        //7.默认值为false,设为true路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

        this.router.navigate(['/user',1], { replaceUrl: true }); 
        //8.未设置时默认为true,设置为false路由不会进行跳转
    }


    ngOnInit() {
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档