最近一直在学习angular技术,一顿研究下来发现并没想象的困难,不过倒是采坑了不少,后面通过网络及社区都解决了。之前就有使用vue.js react做过一些项目实战。今天要给大家分享的是angular聊天室案例。
使用angular8+angular-cli+angular-router+ngrx/store+rxjs+webpack+node+wcPop等技术开发的高仿微信界面angular版聊天室实例项目,实现了下拉刷新、聊天消息右键菜单、发送消息、表情(动图),图片、视频预览,红包打赏等功能。
整体采用flex布局,顶部/底部导航条是自定义组件,作为公共部分引入即可。
Auth为判断是否登录拦截,通过NG提供的canActivate方法进行页面验证
/*
* 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 { LoginComponent } from '../views/auth/login'
import { RegisterComponent } from '../views/auth/register'
import { IndexComponent } from '../views/index'
import { ContactComponent } from '../views/contact'
import { UinfoComponent } from '../views/contact/uinfo'
import { UcenterComponent } from '../views/ucenter'
import { GroupChatComponent } from '../views/chat/group-chat'
import { GroupInfoComponent } from '../views/chat/group-info'
import { SingleChatComponent } from '../views/chat/single-chat'
export const routes: Routes = [
{
path: '', redirectTo: 'index', pathMatch: 'full',
data: { showHeader: true, showTabBar: true },
},
// 登录、注册
{
path: 'login', component: LoginComponent,
},
{
path: 'register', component: RegisterComponent,
},
// 首页、联系人、我
{
path: 'index', component: IndexComponent, canActivate: [Auth],
data: { showHeader: true, showTabBar: true },
},
{
path: 'contact', component: ContactComponent, canActivate: [Auth],
data: { showHeader: true, showTabBar: true },
},
{
path: 'contact/uinfo', component: UinfoComponent
},
{
path: 'ucenter', component: UcenterComponent, canActivate: [Auth],
data: { showHeader: false, showTabBar: true },
},
// 聊天页面
{
path: 'chat/group-chat', component: GroupChatComponent, canActivate: [Auth]
},
{
path: 'chat/single-chat', component: SingleChatComponent, canActivate: [Auth]
},
{
path: 'chat/group-info', component: GroupInfoComponent, 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 {}
export class Auth implements CanActivate{
constructor(private router: Router){}
canActivate(){
let that = this
// 验证token
const token: boolean = window.sessionStorage.getItem('token') ? true : false
if(!token){
// 未登录授权
/*
wcPop({
content: '还未登录授权!', anim: 'shake', style: 'background:#e03b30;color:#fff;', time: 2,
end: function () {
that.router.navigate(['/login']);
}
});
*/
that.router.navigate(['/login']);
}
return token
}
}
不同于vue通过vuex进行状态管理,react通过react-redux/redux进行状态管理,而angular则通过@ngrx/store进行状态管理
项目中使用的弹窗则是自己开发的 wcPop 弹出框插件, 功能效果还ok~~, 支持多种参数配置,内置多种效果,具体的可以去看看这篇文章介绍? https://www.cnblogs.com/xiaoyan2017/p/8695783.html
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'])
}
});
}
}
}
//格式化标签
function surrounds() {
setTimeout(function () { //chrome
var sel = window.getSelection();
var anchorNode = sel.anchorNode;
if (!anchorNode) return;
if (sel.anchorNode === $(".J__wcEditor")[0] ||
(sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === $(".J__wcEditor")[0])) {
var range = sel.getRangeAt(0);
var p = document.createElement("p");
range.surroundContents(p);
range.selectNodeContents(p);
range.insertNode(document.createElement("br")); //chrome
sel.collapse(p, 0);
(function clearBr() {
var elems = [].slice.call($(".J__wcEditor")[0].children);
for (var i = 0, len = elems.length; i < len; i++) {
var el = elems[i];
if (el.tagName.toLowerCase() == "br") {
$(".J__wcEditor")[0].removeChild(el);
}
}
elems.length = 0;
})();
}
}, 10);
}
//每次输入完,定义最后光标位置
var _lastRange = null, _sel = window.getSelection && window.getSelection();
var _rng = {
getRange: function () {
if (_sel && _sel.rangeCount > 0) {
return _sel.getRangeAt(0);
}
},
addRange: function () {
if (_lastRange) {
_sel.removeAllRanges();
_sel.addRange(_lastRange);
}
}
}
//编辑器是否为空,正则匹配,过滤空格、回车
function isEmpty() {
// var html = $editor.html();
var html = $(".J__wcEditor").html();
html = html.replace(/
/ig, "\r\n");
html = html.replace(/<[^img].*?>/ig, "");
html = html.replace(/ /ig, "");
return html.replace(/\r\n|\n|\r/, "").replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, "") == "";
}
好了,以上就是今天的分享,希望以后能给大家分享更多知识。希望能喜欢??
附上最近开发的项目实例 ~~~
vue聊天web端:https://blog.csdn.net/yanxinyun1990/article/details/89735778
react聊天室:https://blog.csdn.net/yanxinyun1990/article/details/93230368