瀏覽代碼

Call the `parse` method when `web mode` and `base` are empty

Shaun 2 年之前
父節點
當前提交
2af91020e8
共有 2 個文件被更改,包括 7 次插入5 次删除
  1. 1 1
      package.json
  2. 6 4
      src/index.js

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "@shaun/alpinejs-router",
-  "version": "1.2.12",
+  "version": "1.2.13",
   "description": "Easy to use and flexible router for Alpine.js",
   "type": "module",
   "main": "dist/module.cjs.js",

+ 6 - 4
src/index.js

@@ -7,7 +7,7 @@ export default function (Alpine) {
   const state = Alpine.reactive({
     mode: 'web',
     base: '',
-    href: location.origin,
+    href: location.href,
     path: '',
     query: {},
     params: {},
@@ -61,15 +61,17 @@ export default function (Alpine) {
     return new RouterURL(href, { mode: state.mode, base: state.base })
   }
 
-  Alpine.effect(() => {
+  function parse () {
     const url = getTargetURL(state.href)
     state.path = url.path
     state.query = url.query
     state.params = router.match(url)
-  })
+  }
+
+  Alpine.effect(() => parse())
 
   Alpine.nextTick(() => {
-    if (state.mode === 'web' && !state.base) state.href = location.href
+    if (state.mode === 'web' && !state.base) parse()
   })
   window.addEventListener('popstate', () => state.href = location.href)