zoukankan      html  css  js  c++  java
  • angular路由学习笔记

    文章目录

    标签routerLink路由传递参数

    url中get传值

    // queryParams 传递的是一个对象
    <a [routerLink]="[ '/endpage/']" [queryParams]="{name:'huangbiao',age:30}">endpage</a>

    定义路由

    const routes: Routes = [
      { path: 'endpage', component: EndPageComponent }
    ];
    url的地址是 http://localhost:4200/endpage?name=huangbiao&age=30

    获取参数

    import { Component, OnInit } from '@angular/core';
    import {ActivatedRoute} from '@angular/router';
    
    @Component({
      selector: 'app-end-page',
      templateUrl: './end-page.component.html',
      styleUrls: ['./end-page.component.scss']
    })
    export class EndPageComponent implements OnInit {
      constructor(public route:ActivatedRoute) { }
    
      ngOnInit() {
        console.dir(this.route);
        debugger
        this.route.params.subscribe((data)=>{
          console.log(data); // 打印的是一个对象,key为name和age
        });
      }
    
    }
    

    获取参数要依赖注入 route:ActivatedRoute对象

    配置动态路由

    // 第一个参数对应路由的name,第二个参数对应路由的age
    <a [routerLink]="[ '/endpage/', 'huangbiao','30' ]">endpage</a>

    定义路由

    const routes: Routes = [
      { path: 'endpage/:name/:age', component: EndPageComponent }
    ];
    url地址是 http://localhost:4200/endpage/huangbiao/30

    获取参数

    import { Component, OnInit } from '@angular/core';
    import {ActivatedRoute} from '@angular/router';
    
    @Component({
      selector: 'app-end-page',
      templateUrl: './end-page.component.html',
      styleUrls: ['./end-page.component.scss']
    })
    export class EndPageComponent implements OnInit {
      constructor(public route:ActivatedRoute) { }
    
      ngOnInit() {
        console.dir(this.route);
        debugger
        this.route.params.subscribe((data)=>{
          console.log(data); // 打印的是一个对象,key为name和age
        });
      }
    
    }

    API js路由跳转

    配置动态路由

    this.router.navigate(['/newscontent/','1243'])
    //this.router.navigate(['/home']);

    如果是动态路由,则路由后面的/是不能少的

    定义路由

    const routes: Routes = [
      { path: 'endpage', component: EndPageComponent }
    ];
    url地址是 http://localhost:4200/endpage/huangbiao/30

    获取参数

    import { Component, OnInit } from '@angular/core';
    import { Router} from '@angular/router';
    
    @Component({
      selector: 'app-end-page',
      templateUrl: './end-page.component.html',
      styleUrls: ['./end-page.component.scss']
    })
    export class EndPageComponent implements OnInit {
      constructor(public router:Router) { }
    
      ngOnInit() {
        console.dir(this.route);
        debugger
        this.route.params.subscribe((data)=>{
          console.log(data); // 打印的是一个对象
        });
      }
    
    }

    get传值

    this.router.navigate(['/news'],{
        queryParams:{
        aid:123
        }
    });

    定义路由

    const routes: Routes = [
      { path: 'endpage', component: EndPageComponent }
    ];
    url地址是 http://localhost:4200/endpage/huangbiao/30

    获取参数

    import { Component, OnInit } from '@angular/core';
    import { Router} from '@angular/router';
    
    @Component({
      selector: 'app-end-page',
      templateUrl: './end-page.component.html',
      styleUrls: ['./end-page.component.scss']
    })
    export class EndPageComponent implements OnInit {
      constructor(public router:Router) { }
    
      ngOnInit() {
        console.dir(this.route);
        debugger
        this.route.params.subscribe((data)=>{
          console.log(data); // 打印的是一个对象
        });
      }
    
    }
    

      

    1.以根路由跳转/login

    this.router.navigate(['login']);

    2.设置relativeTo相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

    this.router.navigate(['login', 1],{relativeTo: route});

    3.路由中传参数 /login?name=1

    this.router.navigate(['login', 1],{ queryParams: { name: 1 } });

    4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1

    this.router.navigate(['home'], { preserveQueryParams: true });

    5.路由中锚点跳转 /home#top

    this.router.navigate(['home'],{ fragment: 'top' });

    6.preserveFragment默认为false,设为true,保留之前路由中的锚点/home#top to /role#top

    this.router.navigate(['/role'], { preserveFragment: true });

    7.skipLocationChange默认为false,设为true,路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

    this.router.navigate(['/home'], { skipLocationChange: true });

    8.replaceUrl默认为true,设为false,路由不会进行跳转

    this.router.navigate(['/home'], { replaceUrl: true });

  • 相关阅读:
    Linux下如何查看版本信息
    java单利模式设计
    MIT 2012 分布式课程基础源码解析-底层通讯实现
    MIT 2012分布式课程基础源码解析-事件管理封装
    MIT 2012分布式课程基础源码解析-线程池实现
    MIT 2012分布式课程基础源码解析一-源码概述
    Leetcode按Tag刷题
    网页搜集系统
    c/c++中的各种字符串转换
    gentoo装X服务器时显卡选择
  • 原文地址:https://www.cnblogs.com/PearlRan/p/10869760.html
Copyright © 2011-2022 走看看