angular——路由

本文详细介绍了Angular路由的使用,包括在app-routing.module.ts中配置路由、路由器的get传值和动态路由传值方法。同时,讨论了JavaScript如何进行路由跳转,特别是get跳转和动态路由跳转。对于路由重载的问题,文章提出了Angular风格的解决方案,通过配置onSameUrlNavigation实现页面重载,并讲解了何时触发路由事件。最后,补充了routerLink、navigate的跳转方式以及如何获取query参数。
摘要由CSDN通过智能技术生成

路由使用

  • app-routing.module.ts 中配置路由
// 引入组件
import {
    FunComponent } from './components/fun/fun.component'
import {
    HomeComponent } from './components/home/home.component'
import {
    Son1Component } from './components/son1/son1.component'
import {
    Son2Component } from './components/son2/son2.component'

// 配置路由
const routes: Routes = [
  {
    path: '', redirectTo: 'home', pathMatch: 'full' }, // 默认路由
  {
   
    // children 配置子路由
    path: 'home', component: HomeComponent, children: [
      {
    path: '', redirectTo: 'son1', pathMatch: 'full' }, // 默认路由
      {
    path: 'son1', component: Son1Component },
      {
    path: 'son2', component: Son2Component },
    ],
  },
  {
    path: 'fun/:id', component: FunComponent },
]
  • 配置显示 router-outlet 路由
<!-- app.component.html 下 -->
<h2>
  <!-- routerLinkActive 添加的 active 可以在 css 中自定义自己想要的样式 -->
  <a routerLink="/home" routerLinkActive="active">home</a>
  &nbsp;&nbsp;&nbsp;
  <a routerLink="/fun" routerLinkActive="active">fun</a>
</h2>
<router-outlet></router-outlet>

<!-- home.component.html 下(子路由) -->
<h3>
  <a routerLinkActive="active" routerLink=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值