Angular2 路由跳转

2019-05-15 11:41:12 浏览数 (1)

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() {
    }

}

0 人点赞