shaunlee / alpinejs-router Goto Github PK
View Code? Open in Web Editor NEWEasy to use and flexible router for Alpine.js
License: MIT License
Easy to use and flexible router for Alpine.js
License: MIT License
Hi, I like your approach and would like to use your alpinejs-router in a project. However I need a working router for a page in a sub directory of the servers root like /calculator/. When I load your example the first time the URL is
http://127.0.0.1:5500/examples/index.html, however all routes in the example transform the URL to http://127.0.0.1:5500/#/ instead of http://127.0.0.1:5500/examples/#/.
Is there any way I can configure the router to work in a sub directory of the servers root?
Thanks, Bernd
Hey, thanks for your work.
I am trying to do something in the EventListener of alpine:init
depending on the current route. I do not seem to be able to do that, since the magic property $router
is not available in JS context.
Is there any way to expose/export an instance of the route
object of index.js so the same functions which are accessible in HTML are accessible in JS?
Hi @shaunlee
I saw the code, how to make sure the active template is up, and others are hidden?
Thanks
Eric Xin
when using template="./login.html", it still references the login.html at the project root instead of the same folder of current file, no difference of template="/login.html"
Hi Shaunlee, first thanks for this amazing code!
For the web mode you say:
Not to worry: To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in. Beautiful, again!
But I don't fully understand this. I'm using Vite (no-vue, just Alpine) and I don't know how to add a catch-all fallback route
.
This is the piece I'm using with no luck. Could you help me with this?
server: {
proxy: {
'/hello/world': {
target: 'http://localhost:3000/',
changeOrigin: true,
secure: false,
configure: (proxy, _options) => {
proxy.on('error', (err, _req, _res) => {
console.log('proxy error', err);
});
proxy.on('proxyReq', (proxyReq, req, _res) => {
console.log('Sending Request to the Target:', req.method, req.url);
});
proxy.on('proxyRes', (proxyRes, req, _res) => {
console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
});
}
}
}
}
I don't seem to be able to navigate to an anchor:
when I'm in hash mode.
The URL would look like this:
http://127.0.0.1:5500/app/#/test#my-anchor
Unfortunately due to the restrictions of the external static hoster I have to use hash mode. Do you think you could support anchors in hash mode?
Thanks, Bernd
Hello, how to add dynamic routes?, there is a fuction like $router.addRoute ?
thx for your work.
From your example (using cdn)
<div x-data>
<a x-link href="/hello/world">Hello World</a>
<template x-route="/hello/:name">
<div>Say hello to <span x-text="$router.params.name"></span></div>
</template>
</div>
When clicking, browser will just try to go to /hello/world, should there be a prevent.default or something ?
Unfortunately, there seems to be an issue with the new version. At least the path
property of $router
is not properly initialised when visiting a path, and only changes to the right value some time after. This worked in 1.2.9.
index.html
<body x-data x-init="fetchData($router)">
<p>Data is: <span x-text="$store.someContent"></span></p>
<script src="/app.js"></script>
</body>
app.js
document.addEventListener('alpine:init', () => {
Alpine.store('someContent', "")
})
function fetchData(r) {
router = Alpine.$router ? Alpine.$router : r;
Alpine.effect(() => console.log("router.path: " + router.path))
if (router.path == '/fetch'){
Alpine.store('someContent', "Hello World")
}
}
Thank you for your continuous effort.
$router is not defined
with both cdn.min.js and es6.min.js
Used with vite js and electron js
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
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.