mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
wip back btn
This commit is contained in:
@ -205,7 +205,7 @@ export function ionicBootstrap(ComponentType, config, router) {
|
|||||||
bootstrap(ComponentType, injectableBindings).then(appRef => {
|
bootstrap(ComponentType, injectableBindings).then(appRef => {
|
||||||
app.load(appRef);
|
app.load(appRef);
|
||||||
|
|
||||||
router.init();
|
router.init(window);
|
||||||
|
|
||||||
// resolve that the app has loaded
|
// resolve that the app has loaded
|
||||||
resolve(app);
|
resolve(app);
|
||||||
|
@ -36,7 +36,9 @@ export class IonicRouter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init(window) {
|
||||||
|
this.initHistory(window);
|
||||||
|
|
||||||
let rootViewCtrl = this.activeViewController();
|
let rootViewCtrl = this.activeViewController();
|
||||||
if (rootViewCtrl) {
|
if (rootViewCtrl) {
|
||||||
let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise();
|
let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise();
|
||||||
@ -44,6 +46,36 @@ export class IonicRouter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initHistory(window) {
|
||||||
|
this.location = window.location;
|
||||||
|
this.history = window.history;
|
||||||
|
|
||||||
|
window.addEventListener('popstate', (ev) => {
|
||||||
|
this.onPopState(ev);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onPopState(ev) {
|
||||||
|
let routeName = (ev.state && ev.state.name);
|
||||||
|
|
||||||
|
console.log('onPopState', routeName);
|
||||||
|
|
||||||
|
let activeViewCtrl = this.activeViewController();
|
||||||
|
if (activeViewCtrl) {
|
||||||
|
activeViewCtrl.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pushState(route) {
|
||||||
|
let newPath = route.path;
|
||||||
|
if (this.location.hash !== '#' + newPath) {
|
||||||
|
let state = {
|
||||||
|
name: route.name
|
||||||
|
};
|
||||||
|
this.history.pushState(state, '', '#' + newPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match(path) {
|
match(path) {
|
||||||
let matchedRoute = null;
|
let matchedRoute = null;
|
||||||
let routeMatch = null;
|
let routeMatch = null;
|
||||||
@ -101,20 +133,12 @@ export class IonicRouter {
|
|||||||
let matchedRoute = this.match(routeConfig.path);
|
let matchedRoute = this.match(routeConfig.path);
|
||||||
|
|
||||||
if (matchedRoute) {
|
if (matchedRoute) {
|
||||||
this.updateState(matchedRoute);
|
this.pushState(matchedRoute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateState(route) {
|
|
||||||
let newPath = route.path;
|
|
||||||
if (window.location.hash !== '#' + newPath) {
|
|
||||||
console.log('updateState', newPath);
|
|
||||||
window.location.hash = newPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addViewController(viewCtrl) {
|
addViewController(viewCtrl) {
|
||||||
this._viewCtrls.push(viewCtrl);
|
this._viewCtrls.push(viewCtrl);
|
||||||
}
|
}
|
||||||
@ -127,7 +151,7 @@ export class IonicRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getCurrentPath() {
|
getCurrentPath() {
|
||||||
let hash = window.location.hash;
|
let hash = this.location.hash;
|
||||||
// Grab the path without the leading hash
|
// Grab the path without the leading hash
|
||||||
return hash.slice(1);
|
return hash.slice(1);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user