1. Basic configuration
1. Scaffolding CLI
$ npm install -g @angular/cli
$ ng new PROJECT-NAME
2. Add UI framework ng add ng-zorro-ant
3. Develop ng generate. It is recommended to install vscode plug-in
4. Packing Package. Json
angular.json
Env
Two: development configuration
1. The relationship between module, Router, Components and UI component library (NZ)
2. Route (module) hash mode app-routing.module.ts
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true})].exports: [RouterModule]
})
Copy the code
Lazy load usage
{ path: ' '.loadChildren: () = > import('./layout/layout.module').then(m= > m.LayoutModule) },
Copy the code
Routing guard
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree,Router } from '@angular/router';
import { Observable } from 'rxjs';
import { UserService } from '.. /pages/user/user.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private userService: UserService,private router:Router) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (this.userService.userInfo && this.userService.userInfo.access_token) {
return true;
} else {
return this.router.parseUrl('/login')}}}Copy the code
Add validation to Layout
import { AuthGuard } from '.. /auth/auth.guard';// Route navigation guard
const routes: Routes = [
{
path: ' '.component: LayoutComponent,canActivate: [AuthGuard], children: [{path: 'dashboard'.component:DashboardComponent,data: {breadcrumb: 'home'}},
{
path: 'product'.loadChildren: () = > import('.. /pages/product/product.module').then(m= > m.ProductModule),
data: { breadcrumb: 'goods'}}, {path: 'order'.loadChildren: () = > import('.. /pages/order/order.module').then(m= > m.OrderModule),
data: { breadcrumb: 'order'}}]}];Copy the code
3.Service
Encapsulate a layer of HTTP in app/base.service.ts, If (resp){//do something} if(resp){//do something} if(resp){//do something
4. Common components
App /lib/ to store public components, declare (declarations) in app/common-module and export (exports)
5. Public filter(pipe pipe)
App/common-pipe stores custom public pipes that are referenced in modules when used