npm install -g ionic
ionic start ionic4_demo blank --type=angular
如果第一次安装的时候失败,没关系 删掉已经生成的ioinc4Learn文件包,重新运行命令
cd ionic4_demo
npm install --save @ionic/storage
创建新页面
ionic g page public/login
ionic g page public/register
ionic g page members/dashboard
ionic g service services/authentication
ionic g service services/authGuard
ng generate module members/member-routing --flat
启动项目
ng serve
给app.module.ts添加IonicStorageModule
删除src/app/home文件夹 ,调整app-routing.module.ts的首页路由配置
设置authentication.service.ts文件
import { Platform } from '@ionic/angular';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Storage } from '@ionic/storage';
const TOKEN_KEY = 'auth-token';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
authenticationState = new BehaviorSubject(false);
constructor(private storage: Storage, private plt: Platform) {
this.plt.ready().then(() => {
this.checkToken();
});
}
login() {
return this.storage.set(TOKEN_KEY, 'liuyi 123456').then(
res => {
this.authenticationState.next(true);
}
);
}
logout() {
return this.storage.remove(TOKEN_KEY).then(() => {
this.authenticationState.next(false);
});
}
isAuthenticated() {
return this.authenticationState.value;
}
checkToken() {
return this.storage.get(TOKEN_KEY).then(res => {
if (res) {
this.authenticationState.next(true);
}
});
}
}
设置 auth-guard.service.ts文件
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthenticationService) { }
canActivate(): boolean {
return this.authService.isAuthenticated();
}
}
设置子路由member-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardPageModule' }
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
export class MemberRoutingModule { }
设置主路由app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuardService } from './services/auth-guard.service';
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', loadChildren: './public/login/login.module#LoginPageModule' },
{ path: 'register', loadChildren: './public/register/register.module#RegisterPageModule' },
{
path: 'members',
canActivate: [AuthGuardService],
loadChildren: './members/member-routing.module#MemberRoutingModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
对app.component.ts配置
然后对login resgister和 dashboard页面分别设置
其中login.page.html
<ion-header>
<ion-toolbar>
<ion-title>Login</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-button (click)="login()" expand="block">Login</ion-button>
<ion-button expand="block" color="secondary" herf="/register" routerDirection="forward">Register</ion-button>
</ion-content>
login.page.ts
import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from '../../services/authentication.service';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
constructor(private authService: AuthenticationService) { }
ngOnInit() {
}
login() {
this.authService.login();
}
}
register.page.html
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button defaultHref="/login"></ion-back-button>
</ion-buttons>
<ion-title>Register</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
</ion-content>
register.page.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-register',
templateUrl: './register.page.html',
styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
constructor() { }
ngOnInit() {
}
}
dashboard.page.html
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-button (click)="logout()">
<ion-icon slot="icon-only" name="log-out"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>My Dashboard</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
</ion-content>
dashboard.page.ts
import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from '../../services/authentication.service';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.page.html',
styleUrls: ['./dashboard.page.scss'],
})
export class DashboardPage implements OnInit {
constructor(private authService: AuthenticationService) { }
ngOnInit() {
}
logout() {
this.authService.logout();
}
}
github源码地址 https://github.com/lilugirl/ionic4_demo