利用angular4和nodejs-express构建一个简单的网站(十)—好友模块

上一章讲解了用户登录的相关代码。用户登录成功后,就会进入好友模块,在好友模块中会根据不同的用户ID显示相应的好友列表,点击好友列表中的单个好友就会进入编辑单个好友页面,对好友信息进行编辑。点击列表页面的添加按钮,就会添加新的好友。
我们从这一章开始分析这个好友模块。

模块代码分析

模块基本代码如下:

import { NgModule } from ‘@angular/core‘;
import { CommonModule } from ‘@angular/common‘;
import { ReactiveFormsModule } from ‘@angular/forms‘;
import { HTTP_INTERCEPTORS } from ‘@angular/common/http‘;
import { BirthdaysComponent } from ‘./birthdays/birthdays.component‘;
import { BirthdayListComponent } from ‘./birthday-list/birthday-list.component‘;
import { BirthdayRoutingModule } from ‘./birthday-routing.module‘;
import { BirthdayService } from ‘./birthday.service‘;
import { BirthdayDetailComponent } from ‘./birthday-detail/birthday-detail.component‘;
import { AuthGuardService } from ‘../auth-guard.service‘;
import { AuthInterceptor } from ‘../auth-interceptor‘;
@NgModule({
  imports: [
    CommonModule,
    ReactiveFormsModule,
    BirthdayRoutingModule
  ],
  providers:[
    BirthdayService,
    AuthGuardService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi:true
    }
  ],
  declarations: [BirthdaysComponent, BirthdayListComponent, BirthdayDetailComponent]
})
export class BirthdaysModule { }

在模块代码中除了作为子模块必须的导入的CommonModule模块,还导入了ReactiveFormsModule,BirthdayRoutingModule两个模块,ReactiveFormsModule模块用于编辑用户信息的提交表单,基本用法和用户注册的表单相同,BirthdayRoutingModule模块用于设置路由。
在providers中提供了BirthdayService、AuthGuardService和一个HTTP请求拦截器,分别用于提供数据服务、路由守卫服务和HTTP拦截服务。
在这个模块下共有三个组件:BirthdaysComponent、BirthdayListComponent、BirthdayDetailComponent。
下面开始逐项进行分析。

路由

路由模块BirthdayRoutingModule负责整个Birthdays模块的全部路由。代码如下:

import { NgModule } from ‘@angular/core‘;
import { Routes, RouterModule } from ‘@angular/router‘;
import { BirthdaysComponent } from ‘./birthdays/birthdays.component‘;
import { BirthdayListComponent } from ‘./birthday-list/birthday-list.component‘;
import { AuthGuardService } from ‘../auth-guard.service‘;
import { BirthdayDetailComponent } from ‘./birthday-detail/birthday-detail.component‘;
const birthRoutes: Routes = [
    {
        path: ‘birthday‘,
        component: BirthdaysComponent,
        canActivate: [AuthGuardService],
        children: [
            { path: ‘‘, component: BirthdayListComponent },
            {
                path: ‘:id‘,
                component: BirthdayDetailComponent
            },
            {
                path:‘new‘,
                component:BirthdayDetailComponent
            }
        ]
    },
];
@NgModule({
    imports: [RouterModule.forChild(birthRoutes)],
    exports: [RouterModule]
})
export class BirthdayRoutingModule {}

当地址导航到localhost:4200/birthday时,首先加载BirthdaysComponent控件,BirthdaysComponent控件只要提供一个路由插座和用户的注销操作。
BirthdaysComponent代码比较简单:在这里直接给出html代码和类代码
html代码:

<button type="button" class="btn btn-secondary logout" (click)="logout()">
  Logout</button>
<router-outlet></router-outlet>

控件类代码:

import { Component, OnInit } from ‘@angular/core‘;
import { Router } from ‘@angular/router‘;
import { JumbotronServive, Jumbotron } from ‘../../jumbotron.service‘;
import { AuthTokenService } from ‘../../authtoken.service‘;
@Component({
  selector: ‘app-birthdays‘,
  templateUrl: ‘./birthdays.component.html‘,
  styleUrls: [‘./birthdays.component.css‘]
})
export class BirthdaysComponent{
  constructor(
    private jumbServ: JumbotronServive,
    private tokenServ: AuthTokenService,
    private router: Router) {
    jumbServ.setJumbotron(new Jumbotron(‘Friends‘, ‘‘, ‘‘));
  }
  logout() {
    this.tokenServ.setToken(null);
    this.router.navigate([‘/‘]);
  }
}

当点击Logout按钮时,执行logout()函数,清空保存在本地的认证信息,并导航到首页。
...
<继续路由分析>
birthday路径下有三个子路由,分别为:"空",对应BirthdayListComponent组件。":id"和"new",对应同一个BirthdayDetailComponent组件,当导航到"new"路径时,[routerLink]="[0]",":id"的routerLink为具体的id。
这一篇先暂时写这么多,下一篇主要介绍三个主要的服务提供程序。敬请期待......

原文地址:https://www.cnblogs.com/jlfw/p/11951895.html

时间: 2024-08-30 07:15:10

利用angular4和nodejs-express构建一个简单的网站(十)—好友模块的相关文章

nodejs + jquery Mobile构建一个简单的移动web (客户端)

前面展示了使用nodejs技术和jqm来搭建一个简单的支持CRUD操作应用的服务端部分(参见:nodejs + jquery Mobile构建一个简单的移动web(服务端) ),服务端采用nodejs技术实现,使用了mongodb数据库和轻量级web开发框架expressJS, 路由使用restful风格,所以你也可以使用restify来开发. 客户端的实现主要通过ajax调用实现.使用jade模板引擎. 客户端主要包含两个文件:layout.jade和index.jade 1. layout.

通过python 构建一个简单的聊天服务器

构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使用 Telnet,客户机可以连接到 Python 聊天服务器上,并在全球范围内相互进行通信.提交到聊天服务器的消息可以由其他人进行查看(以及一些管理信息,例如客户机加入或离开聊天服务器).这个模型如图 1 所示. 图 1. 聊天服务器使用 select 方法来支持任意多个客户机 聊天服务器的一个重要

第三周——构建一个简单的Linux系统MenuOS

[洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个简单的Linux系统MenuOS

第一节 构建一个简单的WCF应用

先吐个槽,到目前为止接触的东西也就是些ado.net.select.delete.update.create.临时表的批量操作.及稍微复杂点的几个表之间查询再带几个excel导入导出 然后会点前端的js.jquery等,所以在公司目前薪水并不高(能在广州生活下去吧,什么买车买房的想都别想),拿自己身边的同志一比较感觉心里不怎么平衡,凡事还是得靠自己 自强才是硬道理,就吐到这里吧!开始我的wcf之旅吧 本人理工科类型的文笔很烂 希望各位大神不要喷小弟哦(参照的书本:WCF全面解析) 咱们还是从小学

Android学习路线(四)构建一个简单的UI

Android应用的图形化用户界面的构建使用的是View 和 ViewGroup 对象的层次嵌套. View 对象通常是UI部件,例如 buttons 或者 text fields ,而 ViewGroup 是用来定义它的子布局如何排布的容器,它通常是不可见的,例如一个网格或者一个垂直的列表. Android提供XML词汇与View或者ViewGroup的子类的对应,这样的话你就可以通过XML元素的层级嵌套来定义你的UI. 另一种布局 使用XML声明UI比在运行时代码中声明更有用处可以在很多地方

Spring学习(二)——使用用Gradle构建一个简单的Spring MVC Web应用程序

1.新建一个Gradle工程(Project) 在新建工程窗口的左侧中选择 [Gradle],右侧保持默认选择,点击next,模块命名为VelocityDemo. 2.在该工程下新建一个 module,在弹出的窗口的左侧中选择 [Gradle],右侧勾选[Spring MVC],如下图所示: 并勾选[Application server],下方选择框中选择Tomcat7.0,如无该选项,则选中右边的 [ New... ] -- [ Tomcat Server ], 配置 Tomcat .配置好后

构建一个简单的Linux系统 MenuOs —— start_kernel到init进程(20135304刘世鹏)

构建一个简单的Linux系统 MenuOs —— start_kernel到init进程 作者:刘世鹏20135304 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核代码简介 内核源码三个个重要目录 arch占有代码量最大,支持不同cpu的源代码,arch/x86目录下的代码是我们关注的重点 init,内核启动相关的代码基本都在init目录下,init/main.c中start_kernel是整

利用原始套接字实现一个简单的采集网络数据包

//利用原始套接字实现一个简单的采集网络数据包,并进行反向解析IP,MAC地址#include <stdio.h>#include <sys/socket.h>#include <unistd.h>#include <sys/types.h>#include <linux/if_ether.h>#include <linux/in.h> #define BUFFER_MAX 2048 int main(int argc, char *

Android官方入门文档[3]构建一个简单的用户界面

Android官方入门文档[3]构建一个简单的用户界面 Building a Simple User Interface构建一个简单的用户界面 This lesson teaches you to1.Create a Linear Layout2.Add a Text Field3.Add String Resources4.Add a Button5.Make the Input Box Fill in the Screen Width You should also read?Layouts