-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow accessing history state from Route-objects #1535
Comments
Note that right now this can be achieved with a guard: router.afterEach(to => {
to.state = history.state
}) but there are other alternatives like a composable import { shallowRef, onScopeDispose } from 'vue'
function useHistoryState<T = unknown>(router?: Router) {
const state = shallowRef<T>(history.state)
const remove = (router || useRouter()).afterEach(() => {
state.value = history.state
})
onScopeDispose(remove)
return state
} This variant is in a pending PR at #2106 I think ultimately, we want to go with another route location variant but it would be helpful to explore other possibilities and possible caveats. |
Hey! I found this issue looking for an alternative solution for sending arbitrary params (or artificial params @amxmln) when using {
scrollBehavior (to) {
// It would be great to be able to retrieve history state or more generally artificial params here
},
} Bottom line, what I'm trying to say is that it seems there is plenty of use-case for artificial params (aka navigation-based arbitrary data) and I understand history state is probably not the right fit. But with the new version preventing arbitrary params what are the alternatives ? |
@AlexandreBonaventure alternatives are in the changelog |
Yes, I appreciate you are providing migration steps, but going back to my specific use-case I don't see any alternatives working for me:
Let me know if I missed something or if I'm off-topic. I know this is a github issue and not a support forum, but I wonder if the above scenario is warranting another API for dealing with so-called artificial params, which could benefit to other users as well. --- Alternatives (things that could work for me)
|
There won’t be a new api for artificial params (aka state) as it was removed before for being a bad practice (for the reasons mentioned in the changelog and others I probably forgot since it’s been more than 5 years 😅) Your use case does sound like it should use a store. Stores are not only for global state or you could also say that if state lives through different pages, it is global. Or maybe vuejs/rfcs#460 👀 |
@AlexandreBonaventure what about using |
While I was refactoring my code to remove artificial params in lieu of the 4.1.4 release to write my data to the
history.state
object, I noticed that I can pass astate
property to$router.push()
and was kind of surprised that I could not retrieve it with$route.state
(like I can with queries or params) but had to access it usingwindow.history.state
.Would it be possible to implement a
state
property containing only the passed state in theRouteLocationNormalizedLoaded
interface?That would make it a more intuitive replacement for the removed artificial params IMO.
The text was updated successfully, but these errors were encountered: