Is there any recommended method to run JavaScript based on the URL?

Asked 2 years ago, Updated 2 years ago, 91 views

I use rails and vue.js in combination.In rails, there is a system in which js eventually become one, so I would like to use one js for multiple URLs.

So I created the following code:

 if matches=location.pathname.match("/categories/(.*)")
  window.superagent.get("/api/v1/categories/"+matches[1]+".json")
.end(err,res)->
  vue_category.category=res.body

Actually, I would like to do the following.

Router.set("/categories/:id")=(id)->
  vue_category.category=res.body
Router.run()

Is there any library or standard way to do this?
Also, I understand that you want to sort by URL, but I think there are some things that JavaScript doesn't have.

I have also considered vue-router, but I understand that vue-router is a function for switching screens within a single page and does not seem to be suitable for this application. (After receiving advice via Twitter, it is being investigated.)

Thank you for your cooperation.

javascript ruby-on-rails coffeescript vue.js

2022-09-29 21:22

1 Answers

@mizchi/router seems to achieve my goal.
https://www.npmjs.com/package/ @mizchi/router

I was told via Twitter.
https://twitter.com/_ringogirl/status/702009261220450304


2022-09-29 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.