UNPKG

@ylz/plugins

Version:

ylz plugins

3 lines (2 loc) 1.75 kB
/* * Copyright © 2019-2019 chenwenbin * Released under the MIT License. */ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e=e||self)["route-then"]=r()}(this,(function(){"use strict";var e={$router:null,resolve:null,request:function(){var e=this,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"push",t=arguments.length>1?arguments[1]:void 0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null;if(!t||""===t)throw new Error("location is missing");return new Promise((function(u,l){if(e.$router)switch(e.resolve=u,r){case"push":e.$router.push(t,n,o);break;case"replace":e.$router.replace(t,n,o);break;case"go":e.$router.go(t);break;default:l(new Error("requestType error:"+r))}else l(new Error("$router missing"))})).catch((function(r){throw e.resolve=null,new Error(r)}))},push:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;return this.request("push",e,r,t)},replace:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;return this.request("replace",e,r,t)},go:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return this.request("go",e)}};return{install:function(r,t){var n=t.router;e.$router=n,Object.defineProperty(r.prototype,"$routerThen",{value:e}),r.mixin({beforeRouteEnter:function(r,t,n){e.resolve?n((function(r){try{e.resolve(r),e.resolve=null}catch(e){}})):n()},beforeRouteUpdate:function(r,t,n){if(e.resolve)try{e.resolve(this),e.resolve=null}catch(e){}n()}})}}}));