Detect when the user edits the URL on the address bar #9611
Unanswered
jfmdev
asked this question in
Help/Questions
Replies: 2 comments
-
import { onBeforeRouteUpdate } from 'vue-router'; |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can listen for the window.addEventListener('hashchange', (e) => {
console.log('changed', e. newURL, e.oldURL)
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there an easy way to detect when the user changes the URL on address bar by manually typing something (and then pressing Enter)?
I'm aware that you can add a watcher to
$route
, and be notified every time the URL changes, but this method doesn't allows you to differentiate when the URL was changed because your code calledthis.$router.replace()
or because the user edited the address bar.Additional context: I have an screen that opens with URL
/#/products
, and that contains a text input to search for products. By default the input is empty and the screen display all products.If the user writes "food" on the input, the application will execute an AJAX request (to fetch products that contain the word "food") and the URL will be updated to
#/products?search=food
.If then the user reloads the page, then the application (on the
mounted
hook) will read$route.query.search
to populate the search input (with "food" on this case) and to only display the products that matches the search criteria.The requirement I have is that if the user edits the address bar (e.g. changing
#/products?search=food
to#/products?search=electronics
) the application should detect the change and execute an AJAX request to update the list of products.Currently I'm using a flag, to indicate when the URL was changed my code, but I would like to know if the library has some built-in method that does the same:
Beta Was this translation helpful? Give feedback.
All reactions