VueJs Style Guide
- File name must be lowercase and kebab case always.
appTable.vue
StaticMap.vue
app-table.vue
static-map.vue
- Components name must be lowercase and kebab case always and equal to the file name (see rule #1)
export default {
name: 'AppTable',
};
export default {
name: 'app-table',
};
- The name attribute must be the first attribute in the component. ALWAYS.
export default {
methods,
watch,
data
computed,
name: 'app-table',
};
export default {
name: 'app-table',
data,
watch,
};
- All the functions of a vue component must be declared outside the component definition
export default {
name: 'app-table',
data() {},
methods: {
myMethod() {},
},
computed: {
myComputed() {},
},
watch: {
name: () => {},
},
};
function data() {}
function myComputed() {}
function myMethod() {}
function name() {}
export default {
name: 'app-table',
data,
methods: {
myMethod,
},
computed: {
myComputed,
},
watch: {
name,
},
};
- Prefer shortcuts like @ or : over v-bind and v-on
<input v-on:change="onChange" />
<button v-bind:disabled="isDisabled">Submit</button>
<input @change="onChange" />
<button :disabled="isDisabled">Submit</button>
- All components must use the scoped attribute in the style section. The only exception is the root component App.
<template>
<button class="button">Submit</button>
</template>
<style>
.button {
border: none;
border-radius: 2px;
}
</style>
<template>
<button class="button">Submit</button>
</template>
<style scoped>
.button {
border: none;
border-radius: 2px;
}
</style>
- Use $ as a preffix for private properties
import Vue from 'vue';
Vue.userInfo = { id: 1, name: 'Cosme Fulanito' };
import Vue from 'vue';
Vue.$userInfo = { id: 1, name: 'Cosme Fulanito' };
- Never put logic inside templates, use methods/computed instead.
<template>
<div v-if="someArray.length > 0">
</div>
</template>
<template>
<div v-if="hasValues">
</div>
</template>
<script>
function hasValues() {
return this.someArray.length > 0;
}
</script>
- Always use v-key together with v-for directive
<ul v-for="item in items">
<ul v-for="item in items" :key="item">
- Avoid using v-if within v-for
<ul v-for="user in users" v-if="user.active" :key="user.id">
</ul>
<ul v-for="user in filteredUsers" :key="user.id">
</ul>
<script>
function filteredUsers() {
return this.users.filter(u => u.active);
}
export default {
name: 'user-list',
computed: {
filteredUsers,
},
};
</script>
- Use filters whenever you want to present the value in a different way but without changing the original value.
<td>
{{ amount }}
</td>
<script>
function created() {
this.amount = this.amount.toFixed(2);
}
export default {
created,
}
</script>
<td>
{{ amount | round }}
</td>
<script>
function round(value) {
return value.toFixed(2);
}
export default {
filters: {
round,
},
};
</script>
- Always use classes over any other kind of css selectors for scoped components
<template>
<button>Submit</button>
</template>
<style scoped>
button {
background-color: peru;
padding: 20px;
}
</style>
<template>
<button class="btn">Submit</button>
</template>
<style scoped>
.btn {
background-color: peru;
padding: 20px;
}
</style>
- Inside a vue component always put the template, script and style tag in that order.
<script>
</script>
<style>
</style>
<template>
</template>
<template>
</template>
<script>
</script>
<style lang="scss" scoped>
</style>