当前位置: 代码网 > it编程>前端脚本>AngularJs > Angular 中的路由详解

Angular 中的路由详解

2024年05月15日 AngularJs 我要评论
1 使用 routerlink 指令 路由跳转命令创建项目:ng new ng-demo创建需要的组件:ng g component components/homeng g component com

1 使用 routerlink 指令 路由跳转

命令创建项目:

ng new ng-demo

创建需要的组件:

ng g component components/home
ng g component components/news
ng g component components/produect

找到 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'}
];

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

<h1>
  <a routerlink="/home" routerlinkactive="active">首页</a>
  <a routerlink="/news" routerlinkactive="active">新闻</a>
</h1>
<router-outlet></router-outlet>

routerlink 跳转页面默认路由:

//匹配不到路由的时候加载的组件 或者跳转的路由
{path: '**', redirectto: 'home'}

routerlinkactive: 设置 routerlink 默认选中路由

<h1>
  <a routerlink="/home" routerlinkactive="active">
    首页
  </a>
  <a routerlink="/news" routerlinkactive="active">
    新闻
  </a>
</h1>
.active {
  color: green;
}
<h1>
    <a [routerlink]="[ '/home' ]" routerlinkactive="active">首页</a>
    <a [routerlink]="[ '/news' ]" routerlinkactive="active">新闻</a>
</h1>

2 使用方法跳转路由 - 使用 router.navigate 方法

在组件中注入 router 服务,并使用 navigate 方法进行路由跳转:

import { component } from '@angular/core';
import { router} from "@angular/router";
@component({
  selector: 'app-root',
  templateurl: './app.component.html',
  styleurls: ['./app.component.scss']
})
export class appcomponent {
  title = 'routerproject';
  constructor(public router: router) {
  }
  gotopage(path: string) {
    this.router.navigate([path]).then(r => {})
  }
}
<h1>
  <button (click)="gotopage('home')">首页</button>
  <button (click)="gotopage('news')">新闻</button>
</h1>
<router-outlet></router-outlet>

3 routerlink跳转页面传值 - get传值的方式

页面跳转 - queryparams属性是固定的:

<h1>
  <a routerlink="/home" routerlinkactive="active" [queryparams]="{name: 'index'}">首页</a>
  <a routerlink="/news" routerlinkactive="active" [queryparams]="{name: 'news'}">新闻</a>
</h1>
<router-outlet></router-outlet>

获取参数方式:

import {component, oninit} from '@angular/core';
import {activatedroute} from "@angular/router";
@component({
  selector: 'app-home',
  templateurl: './home.component.html',
  styleurls: ['./home.component.scss']
})
export class homecomponent implements oninit{
  constructor(public activatedroute: activatedroute) {
  }
  ngoninit(): void {
    this.activatedroute.queryparams.subscribe(data => {
      console.log(data)
    })
  }
}

4 使用方法跳转页面传值 - get传值的方式

<h1>
    <button (click)="gotopage('home', 'home')">首页</button>
    <button (click)="gotopage('news', 'news')">新闻</button>
</h1>
<router-outlet></router-outlet>
import { component } from '@angular/core';
import { router} from "@angular/router";
@component({
  selector: 'app-root',
  templateurl: './app.component.html',
  styleurls: ['./app.component.scss']
})
export class appcomponent {
  title = 'routerproject';
  constructor(public router: router) {
  }
  gotopage(path: string, param: string) {
    this.router.navigate([path], {
      queryparams: {
        name: param
      }
    }).then(r => {})
  }
}

5 动态路由的方式-路由跳转

配置路由文件:

import {ngmodule} from '@angular/core';
import {routermodule, routes} from '@angular/router';
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/:id', component: homecomponent},
];
@ngmodule({
  imports: [routermodule.forroot(routes)],
  exports: [routermodule]
})
export class approutingmodule {
}

页面设置参数:

<h1>
  <a [routerlink]="['/home', '1000']" routerlinkactive="active">首页</a>
</h1>
<router-outlet></router-outlet>

参数接受:

import {component, oninit} from '@angular/core';
import {activatedroute} from "@angular/router";
@component({
  selector: 'app-home',
  templateurl: './home.component.html',
  styleurls: ['./home.component.scss']
})
export class homecomponent implements oninit{
  constructor(public activatedroute: activatedroute) {
  }
  ngoninit(): void {
    this.activatedroute.params.subscribe(data => {
      console.log(data)
    })
  }
}

6 父子路由

创建组件引入组件

import {homecomponent} from "./components/home/home.component";
import {newscomponent} from "./components/news/news.component";

配置路由

import {ngmodule} from '@angular/core';
import {routermodule, routes} from '@angular/router';
import {homecomponent} from "./components/home/home.component";
import {newscomponent} from "./components/news/news.component";
const routes: routes = [
  {
    path: 'home',
    component: homecomponent,
    children: [
      {
        path: 'news',
        component: newscomponent
      },
      {path: '**', redirectto: 'home'}
    ]
  },
  {path: '**', redirectto: 'home'}
];
@ngmodule({
  imports: [routermodule.forroot(routes)],
  exports: [routermodule]
})
export class approutingmodule {
}

父组件中定义router-outlet

<router-outlet></router-outlet>

到此这篇关于angular 中的路由的文章就介绍到这了,更多相关angular 中的路由内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com