Skip to content

Instantly share code, notes, and snippets.

@carcinocron
Created July 8, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carcinocron/c1303ae85b9138a06398d1fa1ccfcc2c to your computer and use it in GitHub Desktop.
Save carcinocron/c1303ae85b9138a06398d1fa1ccfcc2c to your computer and use it in GitHub Desktop.
Example of Vue.js Mixin to bind property from URL to component
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