import { Injectable, EventEmitter } from '@angular/core'; import { Location as Local } from '@angular/common'; import { Platform, App } from 'ionic-angular'; import { Route } from './route' import { stringifyUrl, patternToPath } from '../utils/url' import { Log } from "../utils/log"; const log = new Log('Location'); // 通用location方法 @Injectable() export class Location{ pathname $$path params constructor(public app:App, public local:Local) { } monitor = (scope) => { // 监听页面进入 this.app.viewWillEnter.subscribe((view) => { const RouteName = view.data.componentName || view.name; const route = Route.getRoute(RouteName); if(route){ this.params = view.data; this.pathname = patternToPath(route.url); this.$$path = stringifyUrl(this.pathname,this.params,route.url); this.setSwipeBackEnabled(route,scope,true); } }); // 监听页面退出 this.app.viewWillLeave.subscribe((view) => { const RouteName = view.data.componentName || view.name; const route = Route.getRoute(RouteName); if(route){ this.setSwipeBackEnabled(route,scope); } }); } // 滑动后退设置,注意全局为启用 setSwipeBackEnabled = (route,scope, isEnter?) => { if(typeof route.swipeBackEnabled != 'boolean') return; if(!route.swipeBackEnabled){ // 配置为关闭swipeBackEnabled则进入禁用,退出恢复 scope.navCtrl.swipeBackEnabled = isEnter ? false : true; } } path = (origin?) => { const originPath = this.local.path(); return origin ? originPath : originPath.replace(/\//,''); } }