详解Angular中的路由及其用法


本文摘自PHP中文网,作者青灯夜游,侵删。

本篇文章带大家了解一下Angular中的路由,以及Angular路由的使用。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

相关推荐:《angular教程》

一、 Angular 创建一个默认带路由的项目

1、命令创建项目

ng new ng-demo --skip-install

在这里插入图片描述

2、创建需要的组件

1

2

3

ng g component components/home

ng g component components/news

ng g component components/newscontent

3、找到 app-routing.module.ts 配置路由

引入组件

1

2

3

import { HomeComponent } from './components/home/home.component';

import { NewsComponent } from './components/news/news.component';

import { ProductComponent } from './components/product/product.component';

配置路由

1

2

3

4

5

6

const routes: Routes = [

{path: 'home', component: HomeComponent},

{path: 'news', component: NewsComponent},

{path:'product', component:ProductComponent },

{path: '*', redirectTo: '/home', pathMatch: 'full' }

];

4、找到 app.component.html 根组件模板,配置 router-outlet 显示动态加载的路由

1

2

3

4

5

<h1>

    <a routerLink="/home">首页</a>

    <a routerLink="/news">新闻</a>

</h1>

<router-outlet></router-outlet>

二、Angular routerLink 跳转页面默认路由

1

2

<a routerLink="/home">首页</a>

<a routerLink="/news">新闻</a>

1

2

3

4

5

6

//匹配不到路由的时候加载的组件 或者跳转的路由

{

    path: '**', /*任意的路由*/

    // component:HomeComponent

    redirectTo:'home'

}

三、Angular routerLinkActive 设置 routerLink 默认选中路由

1

2

3

4

5

6

7

8

<h1>

  <a routerLink="/home" routerLinkActive="active">

    首页

  </a>

  <a routerLink="/news" routerLinkActive="active">

    新闻

  </a>

</h1>

1

2

3

4

<h1>

    <a [routerLink]="[ '/home' ]" routerLinkActive="active">首页</a>

    <a [routerLink]="[ '/news' ]" routerLinkActive="active">新闻</a>

</h1>

四、动态路由

4.1.问号传参

跳转方式,页面跳转或js跳转
问号传参的url地址显示为 …/list-item?id=1

queryParams属性是固定的

<a [routerLink]="[’/list-item’]" [queryParams]="{id:item.id}">
{{ item.name }}

//js跳转
//router为ActivatedRoute的实例

1

2

3

4

5

6

7

8

9

10

11

12

import { Router } from '@angular/router';

.

constructor(private router: Router) {}

.

this.router.navigate(['/newscontent'],{

  queryParams:{

    name:'laney',

    id:id

  },

  skipLocationChange: true

  //可以不写,默认为false,设为true时路由跳转浏览器中的url会保持不变,传入的参数依然有效

});

获取参数方式

1

2

3

4

5

6

7

8

import { ActivatedRoute } from '@angular/router';

 

constructor(public route:ActivatedRoute) { }

ngOnInit() {

    this.route.queryParams.subscribe((data)=>{

      console.log(data);

 })

}

4.2 路径传参

路径传参的url地址显示为 …/list-item/1

1

2

3

<a [routerLink]="[’/list-item’, item.id]"> {{ item.name }}

//js跳转 //router为ActivatedRoute的实例

this.router.navigate([’/list-item’, item.id]);

路径配置:

1

{path: ‘list-item/:id’, component: ListItemComponent}

获取参数方式

1

2

3

4

5

this.route.params.subscribe(

  param => {

      this.id= param['id'];

  }

)

五、父子路由

1、创建组件引入组件

1

2

import { WelcomeComponent } from ‘./components/home/welcome/welcome.component’;

 import { SettingComponent } from ‘./components/home/setting/setting.component’;

2、配置路由

1

2

3

4

5

6

7

8

9

10

11

12

13

{

    path:'home',

    component:HomeComponent,

    children:[{

      path:'welcome',

      component:WelcomeComponent

    },{

      path:'setting',

      component:SettingComponent

    },

    {path: '**', redirectTo: 'welcome'}

  ]

},

3、父组件中定义router-outlet

更多编程相关知识,请访问:编程视频!!

以上就是详解Angular中的路由及其用法的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

浅谈Angular中控制器、服务和指令的关系

了解Angular中的变化检测(change detection)机制

Angular中的firebase身份验证(代码示例)

浅谈Angular路由跳转中的navigatebyurl和navigate

浅谈Angular中导入本地json文件的方法

Angular和vue.js的区别是什么?Angular和vue.js的深度对比

浅谈Angular10配置@路径别名的方法

5种Angular中组件通信的方法介绍

了解一下Angular中的@input()和@output()

浅谈Angular中的模块(ngmodule),延迟加载模块

更多相关阅读请进入《Angular》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...