angular ngoninit 刷新html页面_Angular聊天室angular+ngrx手机端APP聊天实例

本文介绍了如何使用Angular8.0、@angular/cli、ngrx/store和rxjs等技术开发一个移动端聊天App。项目包括下拉刷新、右键菜单、表情、图片和视频预览等功能。同时,提到了状态管理的实现方式和Angular页面的两种常见写法。
摘要由CSDN通过智能技术生成

f350d4c33b36b2963a2bfcf63c5c71f5.gif

之前有分享过 Vue 和 React 技术来实现聊天实例项目系列,这次给小伙伴们分享的是基于 Angular 技术来实现一个移动端聊天App实例。

8203dad18fd9d55ad15b5abcf09b386f.png

简介

angular+angular-cli+angular-router+ngrx/store+rxjs等技术开发实现的仿微信app界面聊天室实例项目。实现了下拉刷新、右键菜单、发送消息、表情(动图),图片、视频预览,红包打赏等功能。

aa9255604d18235ff5b2e6652d6fc2d5.png

f7b54433b5f2c5c9cb8e9ec90f51fc4a.png

https://angular.cn/
https://github.com/angular/angular

技术栈

  • 框架技术:angular8.0 / @angular/cli

  • 状态管理:@ngrx/store / rxjs

  • 地址路由:@angular/router

  • 弹窗组件:wcPop

  • 打包工具:webpack 2.0

  • 环境配置:node.js + cnpm

  • 图片预览:previewImage

  • 轮播组件:swiper

1e249d325f5e9789bd51356ed2dd863c.png

引入阿里Iconfont字体图标

1beca994866d16af7896af072664f09e.png

https://www.iconfont.cn/

下载iconfont图标文件,解压放到angular资源目录下,如下图:解压到src/assets/fonts目录下

e2be51ee26b7415494439c4ec6d08527.png

引入

// 在公共样式styles.css引入即可/* 导入公共样式 */@import './assets/fonts/iconfont.css';@import './assets/css/reset.css';@import './assets/css/layout.css';// 通过如下方式调用class=

效果图

4bc11a587499c3dc6b27a3c7f0ffc88f.png

ad2ccc1370075a2adda3be2ba59c9857.png

ecd7d58603148287948654a4955cf2a2.png

e9c3060347efced5139fbd526b571698.png

0ec1c192b092d3eea130bcf5883060c4.png

7733131c29e4661a26c81867942ce5ec.png

91e347b557c9a0a62c40f898dc45a1a5.png

b6f92c5f060408107485b06d8cff2d1b.png

7ac838f173d9a6e82ef648895521e2cc.png

dbe3f529aa63af6a854183e3bb892a1f.png

5fa0716d1d8b1c1b058d76f8fe797127.png

9cf8c847e54824da35edfd8c30a1a4ed.png

b31288dbebf8cda302177150e8200380.png

主模板app.component.html

79824bb2185387943f7c24f6f1e219e2.png

<div class="weChatIM__panel clearfix">    <div class="we__chatIM-wrapper flexbox flex__direction-column">                <header-bar>header-bar>                        <div class="wcim__container flex1">            <router-outlet>router-outlet>        div>                <tab-bar>tab-bar>    div>div>

a3a0102f4f05ef3fda60671d026afb83.png

angular页面后缀名为.ts,常见的页面写法有2种。

1、把html和css分离单独文件

import { Component } from '@angular/core';@Component({  selector: 'app-root',  templateUrl: './app.component.html',  styleUrls: ['./app.component.css']})export class AppComponent {  constructor(){}  ngOnInit(){    //...  }}

2、把html和css写在一个页面

import { Component, OnInit } from '@angular/core'@Component({    selector: 'app-root',    template: `      <div class="weChatIM__panel clearfix">        <div class="we__chatIM-wrapper flexbox flex__direction-column">                    <header-bar>header-bar>                              <div class="wcim__container flex1">            <router-outlet>router-outlet>          div>                      <tab-bar>tab-bar>        div>      div>    `,    styles: [``]})export class IndexComponent implements OnInit {    constructor(){}      ngOnInit(){    //...    }}

页面路由配置app-routing.module.ts

/* *  angular/router路由配置 */import { NgModule } from '@angular/core'import { Routes, RouterModule } from '@angular/router'// 引入路由验证import { Auth } from '../views/auth/auth'// 引入页面组件import { NotFoundComponent } from '../components/404'import { IndexComponent } from '../views/index'import { GroupChatComponent } from '../views/chat/group-chat'// ...export const routes: Routes = [  {    path: '', redirectTo: 'index', pathMatch: 'full',    data: { showHeader: true, showTabBar: true },  },    // 首页、联系人、我  {    path: 'index', component: IndexComponent, canActivate: [Auth],    data: { showHeader: true, showTabBar: true },  },    {    path: 'chat/group-chat', component: GroupChatComponent, canActivate: [Auth]  },  // 404  {    path: '**', component: NotFoundComponent,  },  // ...];@NgModule({  // imports: [RouterModule.forRoot(routes)],  imports: [RouterModule.forRoot(routes, { useHash: true })],  //开启hash模式  exports: [RouterModule],  providers: [Auth]})export class AppRoutingModule {}

angular是通过 ngrx/store 来进行状态管理。这里不作详细介绍,想了解用法去官网查阅资料。

https://ngrx.io/
https://github.com/ngrx/platform

Angular表单验证

export class LoginComponent implements OnInit {    private formField = {        tel: '',        pwd: ''    }    handleSubmit(){        let that = this        if(!this.formField.tel){            wcPop({ content: '手机号不能为空!', style: 'background:#eb5a5c;color:#fff;', time: 2 });        }else if(!checkTel(this.formField.tel)){            wcPop({ content: '手机号格式不正确!', style: 'background:#eb5a5c;color:#fff;', time: 2 });        }else if(!this.formField.pwd){            wcPop({ content: '密码不能为空!', style: 'background:#eb5a5c;color:#fff;', time: 2 });        }else{            this.store.dispatch(new actions.setToken(getToken(64)))            this.store.dispatch(new actions.setUser(this.formField.tel))            wcPop({                content: '登录成功,跳转中...', style: 'background:#378fe7;color:#fff;', time: 2, shadeClose: false,                end: function () {                    that.router.navigate(['/index'])                }            });        }    }}

END

ok,今天就分享到这里,希望能帮助到你。如果喜欢本文,可以点个「在看」,谢谢!

5dab9c77fa23a168930f854177247543.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值