Angular使用router路由,做页面跳转,传值

本文档详细介绍了在Angular应用中如何利用router模块进行页面跳转和值传递。步骤包括添加路由模块、创建组件,以及在不同组件间进行操作。通过示例代码和布局文件,展示了首页、我的页面和详情页面的实现过程,最终成功运行应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Angular使用router路由,做页面跳转,传值

step1: 添加路由模块,创建三个组件模块,首页 我的 详情页

ng generate module app-routing --flat --module=app
ng generate component user
ng generate component dashboard
ng generate component userDetail

step2: 路由 D:\vue\untitled2900\src\app\app-routing.module.ts

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from "@angular/router";
import {UserDetailComponent} from "./user-detail/user-detail.component";
import {UserComponent} from "./user/user.component";
import {DashboardComponent} from "./dashboard/dashboard.component";

const routes: Routes = [
  {path:'',redirectTo:'/dashboard',pathMatch:'full'},
  {path: 'users', component: UserComponent},
  {path: 'userDetail', component: UserDetailComponent},
  {path: 'dashboard', component: DashboardComponent}];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule {
}

step3: 主页布局 D:\vue\untitled2900\src\app\app.component.html

<h1>主页启动页</h1>
<nav>
  <a routerLink="/dashboard">Dashboard</a>
  <a routerLink="/users">Users</a>
</nav>
<router-outlet></router-outlet>

step4: 我的模块

4.1 我的 功能 D:\vue\untitled2900\src\app\user\user.component.ts

import { Component, OnInit } from '@angular/core';
import {NavigationExtras,Router} from "@angular/router";

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

  name:string ='李军鹏';

  constructor(private router:Router) { }

  ngOnInit(): void {
  }

  getClick(){
    let navigationExtras:NavigationExtras = {
      queryParams:{'name':this.name}
    }
    this.router.navigate(['userDetail'],navigationExtras).then((r: any) => console.log(r))
  }


}

4.2 我的 布局 D:\vue\untitled2900\src\app\user\user.component.html

<h1>我的fragment</h1>
<h1 style="background: brown" (click)="getClick()">请点击我,跳转到用户信息{{name}}</h1>

step5: 首页模块

5.1 首页 功能 D:\vue\untitled2900\src\app\dashboard\dashboard.component.ts

import { Component, OnInit } from '@angular/core';
import {NavigationExtras,Router} from "@angular/router";

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

  name:string='黄小明';
  constructor(private router:Router) { }


  ngOnInit(): void {
  }


  getClick(){
    let navigationExtras:NavigationExtras = {
      queryParams:{'name':this.name}
    }
    this.router.navigate(['userDetail'],navigationExtras).then((r: any) => console.log(r))
  }

}

5.2 首页 布局 D:\vue\untitled2900\src\app\dashboard\dashboard.component.html

<h1>首页fragment</h1>
<h1 style="background: green" (click)="getClick()">请点击我,跳转到数据列表{{name}}</h1>

step6: 详情模块

6.1 详情 功能 D:\vue\untitled2900\src\app\user-detail\user-detail.component.ts

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from "@angular/router";

@Component({
  selector: 'app-user-detail',
  templateUrl: './user-detail.component.html',
  styleUrls: ['./user-detail.component.css']
})
export class UserDetailComponent implements OnInit {

  name:string | undefined;

  constructor(private route: ActivatedRoute) {
    this.route.queryParams.subscribe((r: any) => {
      this.name = r.valueOf().name;
    })
  }


  ngOnInit(): void {
  }

}

6.2 详情 布局 D:\vue\untitled2900\src\app\user-detail\user-detail.component.html

<h1>详情页fragment</h1>
<h1 style="background: orange">详情页数据{{name}}</h1>

run,运行成功,

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值