angular路由知识整理

基本路由配置

1.创建带路由的angular项目

//创建是在是否选择路由时选择yes
ng new my-app

2.创建组件并配置路由

//创建组件
ng g component name
//app-routing.module.ts中引入组件
const routes: Routes = [
  {path:'home',component:HomeComponent,},
  {path:'news',component:NewsComponent},
  {path:'product',component:ProductComponent},
  {path:'newsContent',component:NewsContentComponent},
  {path:'productContent',component:ProductContentComponent,}
];

3.页面中使用路由

<a [routerLink]="['/home']" routerLinkActive="active">首页</a> &nbsp;
<a [routerLink]="['/news']" routerLinkActive="active">新闻</a> &nbsp;
<a [routerLink]="['/productContent']" routerLinkActive="active">产品</a> &nbsp;

4.设置默认路由地址

const routes: Routes = [
  {path:'home',component:HomeComponent, },
  {path:'news',component:NewsComponent},
  {path:'product',component:ProductComponent},
  {path:'newsContent',component:NewsContentComponent},
  {path:'productContent',component:ProductContentComponent, },
  //匹配不到路由则跳转到home
  {path:'**',redirectTo:'home'}
];

路由传值

1. 静态传值

传值

<ul>
  <li *ngFor="let item of list;let key = index">
    <a [routerLink]="['/newsContent']" [queryParams]="{aid:key,name:'wo shi name'}">{{item}}</a>
  </li>
</ul>

接收

import {ActivatedRoute} from '@angular/router'

constructor(public router:ActivatedRoute) { }

this.router.queryParams.subscribe(data=>{
      console.log(data)
    })

2.页面动态传值

传值

//在router文件中声明传值变量
{path:'newsContent/:aid',component:NewsContentComponent},
//在页面中动态传值
<ul>
  <li *ngFor="let item of list;let key = index">
    <a [routerLink]="['/newsContent',key]">{{item}}</a>
  </li>
</ul>

接收

import {ActivatedRoute} from '@angular/router'

constructor(public router:ActivatedRoute) { }

this.router.params.subscribe(data=>{
      console.log(data)
 })

3.js动态跳转传值

//在router文件中声明传值变量
{path:'productContent/:aid',component:NewsContentComponent},

//引用router包
import { Router } from '@angular/router';
//通过router进行动态传值
goProductContent(){
    //动态js传值
    this.router.navigate(['/productContent/','1234'])
  }

4.get传值动态跳转路由

//引入依赖包
import { Router,NavigationExtras } from '@angular/router';
//使用NavigationExtras 定义queryParams
getNewsContent(){
    //跳转并进行传值
    let queryParams:NavigationExtras = {
      queryParams:{'aid':123}
    }
    this.router.navigate(['/productContent/'],queryParams)
  }

父子路由

1.app-routing.moudle.ts文件中声明子路由

{
    path:'home',component:HomeComponent,
    //子路由
    children:[
      {path: 'setting', component: SettingComponent},
      {path: 'welcome', component: WelcomeComponent},
      //进入页面时的默认路由
      {path:'**',redirectTo:'welcome'}
    ]
  },

2.父路由中嵌套子路由

<div class="product-list">
  <div class="left">
    <a [routerLink]="['/home/welcome']">欢迎首页</a><br><br>
    <a [routerLink]="['/home/setting']">系统设置</a>
  </div>
  <div class="right">
    <!--加载子路由界面-->
    <router-outlet></router-outlet>
  </div>
</div>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值