Reputation:
I am fairly new to vue.
Lets say, this is my URL: https://mywebsite.com/states/ABC?query=
and https://mywebsite.com/search?query=
I am grabbing the path /states/ABC
and /search
using $route.path
. I am doing this path inside mounted()
Now, based on this value of the $route.path
, I want to fire different methods eg if it's states
then statesEvent()
and if it's search
, then searchEvent
.
EDITED: both these events are present in a mixin. I am loading that mixin into this component.
Upvotes: 1
Views: 769
Reputation: 7150
this.$route.path
is a simple String you can match on with any of Javascripts string matching operators (from ==
to match(/regex/)
to includes
etc.).
In your particular case I'd suggest to go with
const path = this.$route.path
if(path.startsWith('/states') {
// execute states specific code
}
else if(path.startsWith('/search')) {
// search specific code
}
Upvotes: 0