Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$router.params set to false when accessing route directly #3

Closed
docweirdo opened this issue Nov 16, 2022 · 11 comments
Closed

$router.params set to false when accessing route directly #3

docweirdo opened this issue Nov 16, 2022 · 11 comments

Comments

@docweirdo
Copy link

Copying the example from README.md, the value of $router.params is set to false when accessing the route /hello/world directly without using the link provided by x-link.

@shaunlee
Copy link
Owner

shaunlee commented Nov 17, 2022

@docweirdo Thanks for the heads up, I've updated it to an empty object.

52971aa

@docweirdo
Copy link
Author

Is it not possible to have the parameters even when that route was accessed directly?

@shaunlee
Copy link
Owner

shaunlee commented Nov 17, 2022

Here's how the $router.params works:

x-route="/hello/world" -> $router.params === {}
x-route="/path/to/:arg1/:arg2/:arg3" -> $router.params === {"arg1": "...", "arg2": "...", "arg3": "..."}

I'm not sure if what you want is the URL query? Like so:

/users?page=1&status=2 -> $router.query === {"page": "1", "status": "2"}

@docweirdo
Copy link
Author

No, my trouble is the following. I define something like

<template x-route="/hello/:user">
    Hello <span x-text="$router.params.user"></span>
</template>

But when I go to https://example.com/hello/shaunlee, params is empty and the site only says Hello. It only works as intended if I visit the path via a built in <a x-link="/hello/shaunlee">Link</a>.

@shaunlee
Copy link
Owner

Probably because the template in Alpine.js can only have one root element. Change it like this:

<template x-route="/hello/:user">
    <div>Hello <span x-text="$router.params.user"></span></div>
</template>

and make sure you set the x-data directive in the <body ... > like so <body x-data ...>, to make all Alpine.js code work properly.

@jgauna
Copy link

jgauna commented Nov 27, 2022

I am facing exactly the same issue. I am using Vite and async-alpine, which could be the problem. I've used your code plus I double-checked the directive in the body

index.html

  <body x-data x-init="$router.config({ mode: 'hash' })">
    <div id="app" class="login">
      <div ax-load ax-load-src="js/components/Home/home.js" x-data="home()" x-html="view" x-ignore></div>
    </div>

    <script type="module" src="/main.js"></script>
  </body>

home.js component

<div ax-load ax-load-src="js/components/ChildModule/main.js" x-data="ChildModule()" x-html="view" x-ignore></div>

<a x-link href="/hello/world">Hello World</a>
<template x-route="/hello/:user">
  <div>Hello <span x-text="$router.params.user"></span></div> <!-- @IMPORTANT: @BUG When I refresh the params are missing and it shows just "Hello" -->
</template>

@docweirdo
Copy link
Author

Sorry for the delay. It took me some time to get around to testing this again.
This time, I've used the example @shaunlee provided in examples.

Accessing the path hello/Shaunlee directly leads to the following result:
image

@shaunlee
Copy link
Owner

@docweirdo Please try replacing the URL from http://127.0.0.1:5500/hello/Shaunlee to http://127.0.0.1:5500/#/hello/Shaunlee as the example uses the hash mode <body x-data x-init="$router.config({ mode: 'hash' })"
Screenshot 2022-11-28 at 19 43 13

@jgauna Are you experiencing the same situation?

@shaunlee shaunlee reopened this Nov 28, 2022
@docweirdo
Copy link
Author

docweirdo commented Nov 28, 2022

I forgot to mention that I, for this screenshot, had changed it to the default rooting mode, sorry. In fact, when changing it back to hash, it works as intended.

The issue does persist in the recommended HTML5 mode.

@shaunlee
Copy link
Owner

Fixed, please check the latest version.

Thank you @docweirdo @jgauna

@docweirdo
Copy link
Author

Wonderful, thank you very much, the example works now with mode set to 'web' as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants