今天小編給大家分享一下Angular+NG-ZORRO怎么開發(fā)一個后臺系統(tǒng)的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、網(wǎng)站制作、工布江達網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、工布江達網(wǎng)絡(luò)營銷、工布江達企業(yè)策劃、工布江達品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供工布江達建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.chinadenli.net

系統(tǒng)功能包括下面的內(nèi)容:
歡迎頁面
用戶列表
用戶新增
用戶修改
用戶刪除
所有的 service 使用模擬的數(shù)據(jù)。
說干咱就干。
結(jié)合 ng-zorro
angular 比較流行的 ui 框架有:
Angular Material 官方指定 UI 框架
NG-ZORRO,又名 Ant Design of Angular 國內(nèi)比較流行的 UI 框架
Ant Design 相信做前端開發(fā)的人兒都比較熟悉了。所以這里我們結(jié)合 NG-ZORRO 這個框架來做。如果熟悉 Vue 或者 React 版本的 Ant Design,相信你可以無縫鏈接啊~
我們重新使用 angular-cli 生成一個項目 ng-zorro。
添加 ng-zorro 是很簡單的事情:進入 ng-zorro 根目錄,執(zhí)行 ng add ng-zorro-antd 即可。
當然你也可以執(zhí)行
npm install ng-zorro-antd添加,不推薦。
結(jié)合 ng-zorro 完成之后,我們運行項目起來 npm run start,你會在 http://localhost:4200 的頁面看到下圖內(nèi)容。

Not Bad, Bro.
配置路由
我們改成 hash 路由,并添加用戶路由,腳手架都幫我們完事了,我們只要做點小修改。
思路:
先添加頁面 user 用戶的列表頁面,使用 ng-zorro 中 table 組件
用戶的新增和更改頁面可以共用同一個頁面,使用 ng-zorro 中 form 組件
頁面刪除功能直接使用彈窗提示,使用 ng-zorro 中 modal 組件
對 ng-zorro 組件按需引入
調(diào)整路由文件
按照思路,我們得在 ng-zorro 引入:
// app.module.ts
import { ReactiveFormsModule } from '@angular/forms';
import { NzTableModule } from 'ng-zorro-antd/table';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
// ...
imports: [ // 是在 imports 中添加,而不是 declarations 中聲明
NzTableModule,
NzModalModule,
NzButtonModule,
NzFormModule,
ReactiveFormsModule,
NzInputModule
],簡單易理解原則,我們這里不使用 children 進行路由的嵌套:
// app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { WelcomeComponent } from './pages/welcome/welcome.component';
import { UserComponent } from './pages/user/user.component';
import { UserInfoComponent } from './pages/user/user-info/user-info.component';
// 相關(guān)的路由
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: '/welcome'
},
{
path: 'welcome',
component: WelcomeComponent
},
{
path: 'user',
component: UserComponent
},
{
path: 'user/add',
component: UserInfoComponent
},
{
path: 'user/edit/:uuid',
component: UserInfoComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(
routes,
{
useHash: true,// 使用 hash 模式
preloadingStrategy: PreloadAllModules
}
)],
exports: [RouterModule]
})
export class AppRoutingModule { }更改菜單
使用腳手架生成的菜單與我們需要開發(fā)的功能不符合,我們來調(diào)整下。
// app.component.html <nz-layout class="app-layout"> <nz-sider class="menu-sidebar" nzCollapsible nzWidth="256px" nzBreakpoint="md" [(nzCollapsed)]="isCollapsed" [nzTrigger]="null"> <div class="sidebar-logo"> <!-- 默認點擊 logo 跳轉(zhuǎn)到首頁 --> <a routerLink="/welcome"> <img src="/upload/otherpic54/logo.svg" alt="logo"> <h2>Ng-Zorro</h2> </a> </div> <ul nz-menu nzTheme="dark" nzMode="inline" [nzInlineCollapsed]="isCollapsed"> <li nz-submenu nzOpen nzTitle="用戶管理" nzIcon="dashboard"> <ul> <li nz-menu-item nzMatchRouter> <a routerLink="/user">用戶列表</a> </li> </ul> </li> </ul> </nz-sider> <nz-layout> <nz-header> <div class="app-header"> <span class="header-trigger" (click)="isCollapsed = !isCollapsed"> <i class="trigger" nz-icon [nzType]="isCollapsed ? 'menu-unfold' : 'menu-fold'" ></i> </span> </div> </nz-header> <nz-content> <div class="inner-content"> <router-outlet></router-outlet> </div> </nz-content> </nz-layout> </nz-layout>
菜單展示,如果我們需要做權(quán)限管理的話,是需要后端配合進行傳值的,然后我們再把相關(guān)的權(quán)限菜單渲染到頁面
替換成上面的代碼后,得到的基本骨架如下:

完成用戶列表
接下來完成用戶列表的骨架,因為使用了 UI 框架,我么寫起來異常的方便:
獲取用戶列表
// user.component.html
<nz-table #basicTable [nzData]="list">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!-- 對獲取到的數(shù)據(jù)進行遍歷 -->
<tr *ngFor="let data of basicTable.data">
<td>{{data.name}}</td>
<td>{{data.position}}</td>
<td>
<a style="color: #f00;">Delete</a>
</td>
</tr>
</tbody>
</nz-table>我們模擬了些數(shù)據(jù)在 assets 文件夾中 user.json:
{
"users": [
{
"uuid": 1,
"name": "Jimmy",
"position": "Frontend"
},
{
"uuid": 2,
"name": "Jim",
"position": "Backend"
}
],
"environment": "development"
}編寫好服務(wù)之后,我們調(diào)用獲取用戶的數(shù)據(jù):
// user.component.ts
import { Component, OnInit } from '@angular/core';
import { UserService } from 'src/app/services/user.service';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {
public list: any = []
constructor(
private readonly userService: UserService
) { }
ngOnInit(): void {
if(localStorage.getItem('users')) {
let obj = localStorage.getItem('users') || '{}'
this.list = JSON.parse(obj)
} else {
this.getList()
}
}
// 獲取用戶列表
getList() {
this.userService.getUserList().subscribe({
next: (data: any) => {
localStorage.setItem('users', JSON.stringify(data.users))
this.list = data.users
},
error: (error: any) => {
console.log(error)
}
})
}
}因為沒有引入后端服務(wù),這里我們采用 localstorage 的方式記錄狀態(tài)。
上面完成后,我們得到列表信息如下:

新增用戶和編輯用戶
我們簡單建立個表單,里面含有的字段就兩個,分別是 name 和 position。這兩個功能是公用一個表單的~
我們在 html 中添加:
// user-info.component.html <form nz-form [formGroup]="validateForm" class="login-form" (ngSubmit)="submitForm()"> <nz-form-item> <nz-form-control nzErrorTip="請輸入用戶名!"> <input type="text" nz-input formControlName="username" placeholder="請輸入用戶名" style="width: 160px;" /> </nz-form-control> </nz-form-item> <nz-form-item> <nz-form-control nzErrorTip="請輸入職位!"> <input type="text" nz-input formControlName="position" placeholder="請輸入職位" style="width: 160px;"/> </nz-form-control> </nz-form-item> <button nz-button class="login-form-button login-form-margin" [nzType]="'primary'">確認</button> </form>
頁面長這樣子:

然后就是邏輯的判斷,進行添加或者是修改。如果是連接帶上 uuid 的標識,就表示是編輯,show you the codes。
// user-info.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, ParamMap } from '@angular/router';
@Component({
selector: 'app-user-info',
templateUrl: './user-info.component.html',
styleUrls: ['./user-info.component.scss']
})
export class UserInfoComponent implements OnInit {
public isAdd: boolean = true;
public userInfo: any = []
public uuid: number = 0;
validateForm!: FormGroup;
constructor(
private fb: FormBuilder,
private route: ActivatedRoute,
) { }
ngOnInit(): void {
this.userInfo = JSON.parse(localStorage.getItem('users') || '[]')
this.route.paramMap.subscribe((params: ParamMap)=>{
this.uuid = parseInt(params.get('uuid') || '0')
})
// 是編輯狀態(tài),設(shè)置標志符
if(this.uuid) {
this.isAdd = false
}
if(this.isAdd) {
this.validateForm = this.fb.group({
username: [null, [Validators.required]],
position: [null, [Validators.required]]
});
} else {
let current = (this.userInfo.filter((item: any) => item.uuid === this.uuid))[0] || {}
// 信息回填
this.validateForm = this.fb.group({
username: [current.name, [Validators.required]],
position: [current.position, [Validators.required]]
})
}
}
submitForm() {
// 如果不符合提交,則報錯
if(!this.validateForm.valid) {
Object.values(this.validateForm.controls).forEach((control: any) => {
if(control?.invalid) {
control?.markAsDirty();
control?.updateValueAndValidity({ onlySelf: true });
}
})
return
}
// 獲取到表單的數(shù)據(jù)
const data = this.validateForm.value
// 新增用戶
if(this.isAdd) {
let lastOne = (this.userInfo.length > 0 ? this.userInfo[this.userInfo.length-1] : {});
this.userInfo.push({
uuid: (lastOne.uuid ? (lastOne.uuid + 1) : 1),
name: data.username,
position: data.position
})
localStorage.setItem('users', JSON.stringify(this.userInfo))
} else { // 編輯用戶,更新信息
let mapList = this.userInfo.map((item: any) => {
if(item.uuid === this.uuid) {
return {
uuid: this.uuid,
name: data.username,
position: data.position
}
}
return item
})
localStorage.setItem('users', JSON.stringify(mapList))
}
}
}我們先設(shè)定一個標志符 isAdd,默認是新建用戶;當 uuid 存在的時候,將其設(shè)置為 false 值,表示是編輯的狀態(tài),對內(nèi)容進行表單的回填。提交表單的操作也是按照該標志符進行判斷。我們直接對 localStorage 的信息進行變更,以保證同步列表信息。
刪除功能
我們引入模態(tài)對話框進行詢問是否刪除。
// user.component.ts
// 刪除
delete(data: any) {
this.modal.confirm({
nzTitle: '<i>你想刪除該用戶?</i>',
nzOnOk: () => {
let users = JSON.parse(localStorage.getItem('users') || '[]');
let filterList = users.filter((item: any) => item.uuid !== data.uuid);
localStorage.setItem('users', JSON.stringify(filterList));
this.list = filterList
}
});
}
我們找到刪除的數(shù)據(jù),將其剔除,重新緩存新的用戶數(shù)據(jù),并更新 table 的用戶列表數(shù)據(jù)。
以上就是“Angular+NG-ZORRO怎么開發(fā)一個后臺系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:Angular+NG-ZORRO怎么開發(fā)一個后臺系統(tǒng)
鏈接URL:http://www.chinadenli.net/article32/gisdsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站維護、外貿(mào)建站、網(wǎng)站排名、云服務(wù)器、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)