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

Angular 5.x 学习笔记之Router(路由)应用

亚连
发布: 2018-05-26 13:40:47
原创
1368 人浏览过

本篇文章主要介绍了Angular 5.x 学习笔记之Router(路由)应用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

序言:

Angular APP 视图之间的跳转,依赖于 Router (路由),这一章,我们来讲述 Router 的应用

实例讲解

运行结果如下。 设置了3个导航栏, Home、 About、Dashboard。 点击不同的导航栏,跳转到相应的页面:


创建3个 component

  1. ng g c home

  2. ng g c about

  3. ng g c dashboard

路由与配置

(1)**引入 Angular Router **

当用到 Angular Router 时,需要引入 RouterModule,如下:

// app.module.ts
import { RouterModule } from '@angular/router';
imports: [
 BrowserModule, RouterModule
],
登录后复制

(2) 路由配置

还记得由谁来管理component 的吧,没错,由 module 来管理。 所以,把新创建的 component,引入到 app.moudle 中。 如下:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { appRoutes } from './routerConfig';

import { AppComponent } from './app.component';
import { AboutComponent } from './components/about/about.component';
import { HomeComponent } from './components/home/home.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
登录后复制

提示: 注意component的路径,为便于管理,我们把新创建的component 移到了 components 文件夹中。

创建 Router Configure 文件

在 app 目录下, 创建 routerConfig.ts 文件。 代码如下:

import { Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
import { AboutComponent } from './components/about/about.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

export const appRoutes: Routes = [
 { path: 'home', 
 component: HomeComponent 
 },
 {
 path: 'about',
 component: AboutComponent
 },
 { path: 'dashboard',
 component: DashboardComponent
 }
];
登录后复制

说明: Angular 2.X 以上版本,开始使用 TypeScript 编写代码,而不再是 JavaScript,所以,文件的后缀是: ts 而不是 js

这个 routerConfigue 文件,怎么调用呢? 需要把它加载到 app.module.ts 中,这是因为 app.moudle.ts 是整个Angular App 的入口。

// app.module.ts
import { appRoutes } from './routerConfig';
imports: [
 BrowserModule,
 RouterModule.forRoot(appRoutes)
],
登录后复制

声明 Router Outlet

在 app.component.html 文件中,添加代码:

<p style="text-align:center">
 <h1>
  {{title}}!!
 </h1>
 <nav>
  <a routerLink="home" routerLinkActive="active">Home</a>
  <a routerLink="about">About</a>
  <a routerLink="dashboard">Dashboard</a>
 </nav>
 <router-outlet></router-outlet>
 </p>
登录后复制

运行

进入到该工程所在的路径, 运行;

ng serve --open
登录后复制

当 webpack 编译成功后,在浏览器地址栏中,输入: http://localhost:4200

即可看到本篇开始的结果。

关于Router,换一种写法:

在 app.moudle.ts 文件中,代码如下 :

 imports: [
  BrowserModule,
  RouterModule.forRoot(
  [
   { path: &#39;home&#39;, 
   component: HomeComponent 
   },
   {
   path: &#39;about&#39;,
   component: AboutComponent
   },
   {
   path: &#39;dashboard&#39;,
   component: DashboardComponent
   }
  ]
  )
 ],
登录后复制

这样一来,可以不用单独创建 routerConfigure.ts 文件。

小结

自从引入了面向组件(component)后,路由管理相比 AngularJS (1.X),方便了很多。

进一步优化:

或许你已经注意到,当访问 http://localhost:4200 时,它的路径应该是 “/”, 我们应该设置这个默认的路径。

{
   path: &#39;&#39;,
   redirectTo:&#39;/home&#39;,
   pathMatch: &#39;full&#39;
   },
登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

零基础学习AJAX之AJAX框架

零基础学习AJAX之制作自动校验的表单

ajax的get请求时缓存处理解决方法

以上是Angular 5.x 学习笔记之Router(路由)应用的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!