温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Angular中路由及其用法的示例

发布时间:2021-03-03 11:27:44 来源:亿速云 阅读:246 作者:小新 栏目:web开发

这篇文章主要介绍了Angular中路由及其用法的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

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

1、命令创建项目

ng new ng-demo --skip-install

Angular中路由及其用法的示例

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

<h2>
    <a routerLink="/home">首页</a>
    <a routerLink="/news">新闻</a>
</h2>
<router-outlet></router-outlet>

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

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

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

<h2>
  <a routerLink="/home" routerLinkActive="active">
    首页
  </a>
  <a routerLink="/news" routerLinkActive="active">
    新闻
  </a>
</h2>
<h2>
    <a [routerLink]="[ '/home' ]" routerLinkActive="active">首页</a>
    <a [routerLink]="[ '/news' ]" routerLinkActive="active">新闻</a>
</h2>

四、动态路由

4.1.问号传参

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

queryParams属性是固定的

<a [routerLink]="[’/list-item’]" [queryParams]="{id:item.id}">
{{ 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中路由及其用法的示例”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI