修改路径

This commit is contained in:
小海
2020-05-16 22:18:45 +08:00
parent abc792a561
commit 42177a7721
683 changed files with 92 additions and 18398 deletions

View File

@@ -0,0 +1,61 @@
import {Injectable} from '@angular/core';
import {filter} from 'rxjs/operators';
import {NavigationEnd, Router, RouterEvent} from '@angular/router';
import {Observable, of, Subscriber} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ComponentStateService {
constructor(private router: Router) {
this.watchRouterChange()
}
visible = {
header: true,
footer: true,
globalBackToTop: true
}
currentPath: string
getCurrentRouterPath = () => this.currentPath;
watchRouterChange() {
let subscriber: Subscriber<string>;
const ob = new Observable(o => subscriber = o);
this.router.events.pipe(filter(e => e instanceof NavigationEnd)).subscribe((e: RouterEvent) => {
// indexOf ==> -1/index
const indexOfParam = e.url.indexOf('?');
const path = e.url.substr(0, indexOfParam === -1 ? e.url.length : indexOfParam);
// lastIndexOf ==> 0/index
const indexOf = path.lastIndexOf('/');
const prefix = path.substr(0, indexOf === 0 ? path.length : indexOf);
this.dealWithPathChange(prefix)
this.currentPath = prefix;
if (subscriber) subscriber.next(prefix)
});
return ob;
}
private dealWithPathChange(path) {
// tslint:disable-next-line:forin
for (const visibleKey in this.visible) {
this.visible[visibleKey] = true
}
switch (path) {
case '/admin':
this.visible.header = false
this.visible.footer = false
this.visible.globalBackToTop = false
break
case '/user':
case '/write':
this.visible.footer = false
this.visible.globalBackToTop = false
break;
default:
}
}
}