Skip to content

Instantly share code, notes, and snippets.

@barnslig
Last active May 8, 2020 12:22
Show Gist options
  • Select an option

  • Save barnslig/1d937af4455fdedc773e0fc45ece152d to your computer and use it in GitHub Desktop.

Select an option

Save barnslig/1d937af4455fdedc773e0fc45ece152d to your computer and use it in GitHub Desktop.
Vue.js: watch vs computed
<script>
export default {
props: {
foobar: {
type: String,
required: true
}
},
data() {
return {
myFoobar: null
};
},
watch: {
foobar() {
this.updateMyFoobar();
}
},
mounted() {
this.updateMyFoobar();
},
methods: {
updateMyFoobar() {
this.myFoobar = `${this.foobar}-baz`;
}
}
};
</script>
<script>
export default {
props: {
foobar: {
type: String,
required: true
}
},
computed: {
myFoobar() {
return `${this.foobar}-baz`;
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment