Shaun 2 жил өмнө
parent
commit
7b630bc951
3 өөрчлөгдсөн 13 нэмэгдсэн , 3 устгасан
  1. 1 1
      package.json
  2. 6 2
      src/url.js
  3. 6 0
      tests/url.test.js

+ 1 - 1
package.json

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

+ 6 - 2
src/url.js

@@ -17,7 +17,11 @@ export class RouterURL {
 
   get path () {
     return (this.mode === 'hash')
-      ? this.#url.hash.slice(1).split('?').shift()
+      ? (
+        this.#url.hash
+        ? this.#url.hash.slice(1).split("?").shift()
+        : this.#url.href.replace(new RegExp('^' + this.#url.origin + this.base), '').split("?").shift()
+      )
       : this.#url.pathname.replace(this.base, '')
   }
 
@@ -32,7 +36,7 @@ export class RouterURL {
   resolve (path, params, replace = false) {
     let [l, r] = this.#url.href.split('?')
     l = (this.mode === 'hash')
-      ? l.replace(/#.+$/, '#' + path)
+      ? l.indexOf('#') > -1 ? l.replace(/#.+$/, '#' + path) : this.#url.origin + this.base + '#' + path
       : l.replace(new RegExp(this.#url.pathname + '$'), this.base + path)
     const q = replace
       ? new URLSearchParams(params).toString()

+ 6 - 0
tests/url.test.js

@@ -75,5 +75,11 @@ describe('URL', () => {
       expect(u.resolve('/', {}, true).url).toBe('http://localhost/base#/')
       expect(u.resolve('', {}, true).url).toBe('http://localhost/base#')
     })
+
+    test('convert web to hash', () => {
+      const u = new RouterURL('http://localhost/hello/world?a=1&b=c', { mode: 'hash' })
+      expect(u.path).toBe('/hello/world')
+      expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/#/xyz?a=123&b=c&d=1')
+    })
   })
 })