Created
July 8, 2019 15:31
-
-
Save carcinocron/c1303ae85b9138a06398d1fa1ccfcc2c to your computer and use it in GitHub Desktop.
Example of Vue.js Mixin to bind property from URL to component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const defaultLimit = 25; | |
export default { | |
computed: { | |
limit: { | |
get() { | |
return parseLimit(this.$route.query.limit); | |
}, | |
set(value) { | |
this.$router.push( | |
{ | |
...this.$route, | |
query: { | |
...this.$route.query, | |
limit: parseLimit(value), | |
}, | |
} | |
); | |
}, | |
}, | |
}, | |
}; | |
function parseLimit(value) { | |
value = parseInt(value) || defaultLimit; | |
value = Math.min(1000, value); | |
value = Math.max(1, value); | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment