DRY your code with Vue.js
DRY also stands for Don't Repeat Yourself, which is a popular phrase for Ruby on Rails developer, which means that you should not repeat your code more than once.
In Vue.js, with the power of Mixins and Directive, these can be achieved easily.
Mixins
Mixins are just like sass mixins, it can save you from writing bunch of codes.
const clickMixin = Vue.mixin({
methods: {
triggerClick: function() {
this.data = `Hello ${Math.random()}!`
}
}
})
Something like this can be shared across multiple components by simply use it by using mixins and pass it as an array.
mixins: [clickMixin]
Do becareful about the lifecycle of mixins when being used, mixins' lifecycle will run before the component's lifecycle kicks in.
Directives
Directive is use for DOM element.
Vue.directive("green", function(el) {
el.style.color = "green";
});
by using it in html, you simply add in v-green into your div, and you can get a green color text.
<h1 v-green v-on:click="triggerClick">{{data}}</h1>
Checkout sample at Codepen
Leave DRY your code with Vue.js to:
Read more #development posts
Best Posts From Johnson Lai
We have not curated any of superoo7's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Johnson Lai
- WHive Smart Contract Analysis
- Created Hive-Price-Feed
- Created Hive-Price-Feed
- Joining Steemfest 4 community
- [Tutorial] Ruby On Rails Push Notification
- Use "Do Not Track" on your web browser
- Build A Full Front End Stack with Quasar Framework and Vue
- Done my personal portfolio with Steem Blockchain and Quasar Framework
- Unexpected down time
- Side to side comparison of Digital Ocean and AWS LightSail
- What Front End Technology to use to create Steem Dapps?
- Trying out decentraland SDK v5
- Quick simple guide on making Redux-Observable works with TypeScript
- Testing latest React API: React Hook
- DRY your code with Vue.js
- A year on steem!
- Release of Vue CLI plugin generate Vue Project for Chrome Extension
- Advent of Code 2018 is available in 2 days
- Tailwind CSS - Utility-First CSS Framework
- Emoji Builder - ๐ Build your favourite combination of emoji ๐