首页 > web前端 > js教程 > 正文

详解Angular中的路由及其用法

青灯夜游
发布: 2021-03-03 10:01:48
转载
1975人浏览过

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

详解Angular中的路由及其用法

相关推荐:《angular教程

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

1、命令创建项目

ng new ng-demo --skip-install

在这里插入图片描述

2、创建需要的组件

ng g component components/home
ng g component components/news
ng g component components/newscontent
登录后复制

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

引入组件

import { HomeComponent } from './components/home/home.component';
import { NewsComponent } from './components/news/news.component';
import { ProductComponent } from './components/product/product.component';
登录后复制

配置路由

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 显示动态加载的路由

<h1>
    <a routerLink="/home">首页</a>
    <a routerLink="/news">新闻</a>
</h1>
<router-outlet></router-outlet>
登录后复制

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

<a routerLink="/home">首页</a>
<a routerLink="/news">新闻</a>
登录后复制
//匹配不到路由的时候加载的组件 或者跳转的路由
{
    path: '**', /*任意的路由*/
    // component:HomeComponent
    redirectTo:'home'
}
登录后复制

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

<h1>
  <a routerLink="/home" routerLinkActive="active">
    首页
  </a>
  <a routerLink="/news" routerLinkActive="active">
    新闻
  </a>
</h1>
登录后复制
<h1>
    <a [routerLink]="[ '/home' ]" routerLinkActive="active">首页</a>
    <a [routerLink]="[ '/news' ]" routerLinkActive="active">新闻</a>
</h1>
登录后复制

四、动态路由

4.1.问号传参

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

queryParams属性是固定的


{{ item.name }}

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

import { Router } from '@angular/router';
.
constructor(private router: Router) {}
.
this.router.navigate(['/newscontent'],{
  queryParams:{
    name:'laney',
    id:id
  },
  skipLocationChange: true 
  //可以不写,默认为false,设为true时路由跳转浏览器中的url会保持不变,传入的参数依然有效
});
登录后复制

获取参数方式

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

constructor(public route:ActivatedRoute) { }
ngOnInit() { 
    this.route.queryParams.subscribe((data)=>{
      console.log(data);
 })
}
登录后复制

4.2 路径传参

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

<a [routerLink]="[’/list-item’, item.id]"> {{ item.name }}
//js跳转 //router为ActivatedRoute的实例
this.router.navigate([’/list-item’, item.id]);
登录后复制

路径配置:

{path: ‘list-item/:id’, component: ListItemComponent}
登录后复制

获取参数方式

this.route.params.subscribe(
  param => {
      this.id= param['id'];
  }
)
登录后复制

五、父子路由

1、创建组件引入组件

import { WelcomeComponent } from ‘./components/home/welcome/welcome.component’;
 import { SettingComponent } from ‘./components/home/setting/setting.component’;
登录后复制

2、配置路由

{
    path:'home',
    component:HomeComponent,
    children:[{
      path:'welcome',
      component:WelcomeComponent
    },{
      path:'setting',
      component:SettingComponent
    },
    {path: '**', redirectTo: 'welcome'}
  ]
},
登录后复制

3、父组件中定义router-outlet

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

以上就是详解Angular中的路由及其用法的详细内容,更多请关注php中文网其它相关文章!

路由优化大师
路由优化大师

路由优化大师是一款及简单的路由器设置管理软件,其主要功能是一键设置优化路由、屏广告、防蹭网、路由器全面检测及高级设置等,有需要的小伙伴快来保存下载体验吧!

下载
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号