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

 更新时间:2018年04月08日 11:04:52   作者:全栈工程师的早读课  
本篇文章主要介绍了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 文件中,添加代码:

<div 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>
 </div>

运行

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

ng serve --open

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

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

关于Router,换一种写法:

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

 imports: [
  BrowserModule,
  RouterModule.forRoot(
  [
   { path: 'home', 
   component: HomeComponent 
   },
   {
   path: 'about',
   component: AboutComponent
   },
   {
   path: 'dashboard',
   component: DashboardComponent
   }
  ]
  )
 ],

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

小结

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

进一步优化:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 剖析Angular Component的源码示例

    剖析Angular Component的源码示例

    本篇文章主要介绍了剖析Angular Component的源码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • AngularJS快速入门

    AngularJS快速入门

    本文通过几个循序渐进的例子,给大家详细讲解了如何快速入门AngularJS,十分的实用,这里推荐给大家,有需要的小伙伴可以参考下。
    2015-04-04
  • angularjs中ng-bind-html的用法总结

    angularjs中ng-bind-html的用法总结

    这篇文章主要介绍了angularjs中ng-bind-html的用法总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • angular中的cookie读写方法

    angular中的cookie读写方法

    本篇文章主要介绍了angular中的cookie读写方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • AngularJS中过滤器的使用与自定义实例代码

    AngularJS中过滤器的使用与自定义实例代码

    这篇文章运用实例代码给大家介绍了angularjs中过滤器的使用和自定义过滤器,对大家学习AngularJS具有一定的参考借鉴价值,感兴趣的朋友们可以参考借鉴。
    2016-09-09
  • AngularJS实现多级下拉框

    AngularJS实现多级下拉框

    这篇文章主要为大家详细介绍了AngularJS实现多级下拉框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Angularjs中的页面访问权限怎么设置

    Angularjs中的页面访问权限怎么设置

    最近一直在忙一个手机端的项目,所以对js学习有点松撤了。今天小编抽时间跟大家分享一篇关于angularjs中的页面访问权限设置教处,对angularjs访问权限感兴趣的朋友一起学习吧
    2016-11-11
  • 详解Angular中实现自定义组件的双向绑定的两种方法

    详解Angular中实现自定义组件的双向绑定的两种方法

    这篇文章主要介绍了详解Angular中实现自定义组件的双向绑定的两种方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Angular.js中$resource高大上的数据交互详解

    Angular.js中$resource高大上的数据交互详解

    这篇文章主要给大家介绍了关于Angular.js中$resource高大上的数据交互的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用angular.js具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。
    2017-07-07
  • angularjs下ng-repeat点击元素改变样式的实现方法

    angularjs下ng-repeat点击元素改变样式的实现方法

    今天小编就为大家分享一篇angularjs下ng-repeat点击元素改变样式的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09

最新评论