@meng-xi/uni-router
Version:
为 uni-app 提供类似 vue-router 风格的路由
2 lines (1 loc) • 5.2 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("vue");var p=(e=>(e.NAVIGATION_ABORTED="NAVIGATION_ABORTED",e.NAVIGATION_REDIRECT="NAVIGATION_REDIRECT",e.NAVIGATION_FAILED="NAVIGATION_FAILED",e.INVALID_METHOD="INVALID_METHOD",e))(p||{});const b=["app","app-plus","app-plus-nvue","app-nvue","app-android","app-ios","app-harmony"];class c extends Error{type;location;constructor(t,n,i){super(n),this.type=t,this.location=i,Object.setPrototypeOf(this,c.prototype)}static navigationAborted(){return new c(p.NAVIGATION_ABORTED,"Navigation aborted by guard")}static navigationRedirect(t){return new c(p.NAVIGATION_REDIRECT,"Navigation redirected",t)}static navigationFailed(t){return new c(p.NAVIGATION_FAILED,t||"Navigation failed")}static invalidMethod(t){return new c(p.INVALID_METHOD,`Navigation method ${t} not available`)}}function l(e,t){if(e.startsWith("tabBar/"))return e.startsWith("/")?e:`/${e}`;const n=e.startsWith("/")?e:`/${e}`;if(e.includes("?")||!t)return n;const i=Object.keys(t).filter(o=>t[o]!==void 0&&t[o]!==null).map(o=>`${encodeURIComponent(o)}=${encodeURIComponent(String(t[o]))}`).join("&");return i?`${n}?${i}`:n}function d(e){if(!e)return null;let t={};const n=String(process.env.UNI_PLATFORM).toString().toLowerCase();return["mp-weixin","mp-alipay","mp-baidu","mp-toutiao","mp-qq"].includes(n)&&(t=e.options||{}),["h5"].includes(n)&&(t=e.$vm?.$route?.query||{}),["app-plus"].includes(n)&&(t=e.$vm?.$mp?.query||{}),{path:e.route||"",fullPath:l(e.route||""),query:t}}function I(e){if(typeof e=="string")return{path:e};const t=typeof e.query=="object"&&e.query!==null?Object.fromEntries(Object.entries(e.query).map(([n,i])=>[n,String(i)])):void 0;return{path:e.path,query:t}}function m(){return!0}function A(e){throw new Error(`[uni-router error]:${e}`)}function v(e){const t={};return Object.keys(e).map(n=>{t[n]=f.unref(e[n])}),t}function g(){const e=String(process.env.UNI_PLATFORM).toString().toLowerCase();return b.includes(e)}class u{static instance;routes;beforeEachHooks;afterEachHooks;customGetCurrentRoute;constructor(t={}){this.routes=t.routes||[],this.beforeEachHooks=[],this.afterEachHooks=[],this.customGetCurrentRoute=t.customGetCurrentRoute}static getInstance(t){return u.instance||(u.instance=new u(t)),u.instance}static push(t,n){return u.getInstance().push(t,n)}push(t,n){return this.navigate(t,"navigateTo",n)}static replace(t){return u.getInstance().replace(t)}replace(t){return this.navigate(t,"redirectTo")}static launch(t){return u.getInstance().launch(t)}launch(t){return this.navigate(t,"reLaunch")}static tab(t){return u.getInstance().tab(t)}tab(t){return this.navigate(t,"switchTab")}static go(t=-1,n){u.getInstance().go(t,n)}go(t=-1,n){const i={delta:Math.abs(t),...g()&&n&&{animationType:n.type||"pop-out",animationDuration:n.duration||300}};uni.navigateBack(i)}static back(t){u.getInstance().back(t)}back(t){this.go(-1,t)}static beforeEach(t){u.getInstance().beforeEach(t)}beforeEach(t){this.beforeEachHooks.push(t)}static afterEach(t){u.getInstance().afterEach(t)}afterEach(t){this.afterEachHooks.push(t)}static setCustomGetCurrentRoute(t){u.getInstance().setCustomGetCurrentRoute(t)}setCustomGetCurrentRoute(t){this.customGetCurrentRoute=t}static getCurrentRoute(){return u.getInstance().getCurrentRoute()}getCurrentRoute(){if(this.customGetCurrentRoute)return this.customGetCurrentRoute();const t=getCurrentPages();return t.length>0?d(t[t.length-1]):null}async navigate(t,n,i){const{path:o,query:h}=I(t),s=this.getCurrentRoute(),a={path:o,query:h||{},fullPath:l(o,h)};try{for(const r of this.beforeEachHooks)await this.runGuard(r,a,s);n==="switchTab"?await this.callMxMethod("switchTab",l(o)):await this.callMxMethod(n,a.fullPath,i);for(const r of this.afterEachHooks)r(a,s)}catch(r){if(r instanceof c&&r.type===p.NAVIGATION_REDIRECT&&r.location)return this.push(r.location);throw c.navigationFailed(r instanceof Error?r.message:String(r))}}runGuard(t,n,i){return new Promise((o,h)=>{const s=a=>{a===!1?h(c.navigationAborted()):typeof a=="string"||typeof a=="object"&&a!==null?h(c.navigationRedirect(a)):o()};try{const a=t(n,i,s);a!==void 0&&Promise.resolve(a).then(r=>{r===!1?h(c.navigationAborted()):typeof r=="string"||typeof r=="object"&&r!==null?h(c.navigationRedirect(r)):o()}).catch(h)}catch(a){h(a)}})}callMxMethod(t,n,i){return new Promise((o,h)=>{const s=uni[t],a={url:n};g()&&i&&(a.animationType=i.type||"pop-in",a.animationDuration=i.duration||300),typeof s=="function"?s(a).then(o).catch(r=>h(c.navigationFailed(r instanceof Error?r.message:String(r)))):h(c.invalidMethod(t))})}}function N(e){const t=f.ref(null),n=f.ref(!1);async function i(){const s=f.unref(t);return s||A("The router instance has not been obtained, please make sure that the router has been rendered when performing the router operation!"),await f.nextTick(),s}function o(s){f.onUnmounted(()=>{t.value=null,n.value=null}),!(f.unref(n)&&m()&&s===f.unref(t))&&(t.value=s,n.value=!0,f.watch(()=>e,()=>{e&&s.setProps(v(e))},{immediate:!0,deep:!0}))}return[o,{setProps:async s=>{(await i()).setProps(s)}}]}exports.Router=u;exports.buildUrl=l;exports.getCurrentRoute=d;exports.parseLocation=I;exports.useMxRouter=N;