Vue - Js CheatSheet
Vue - Js CheatSheet
js cheatsheet
Table of Contents
1. Events
2. Styling
3. Conditionals
4. Looping
5. Components
6. Vue lifecycle
7. Development workflow
8. Vue CLI
o 8.1. Example: webpack-simple layout
9. Slots
o 9.1. Multiple slots: <slot name="">
o 9.2. Optional <slot>: slots with defaults
10. Dynamic components: <component>
11. Forms
12. Using v-model with components
13. Directives: v-...
14. Filters
15. Mixins
16. Transitions
o 16.1. appear attribute
o 16.2. Manual leave-enter classes:
o 16.3. Dynamic transition name
o 16.4. Multiple elements transitions
o 16.5. Transition JS hooks
17. HTTP: vue-resource
o 17.1. Setup
o 17.2. Importing vue-resource
o 17.3. Usage: simple example
o 17.4. Vue.http.interceptors hooks
o 17.5. Setting custom resources
o 17.6. URL templating
18. Routing
o 18.1. Setting up
o 18.2. First route
o 18.3. Routing modes (hash vs history)
o 18.4. Links and navigation
o 18.5. Styling active links
o 18.6. Using navigation from Javascript
o 18.7. Dynamic paths
1 Events
<button v-on:click="method">Click me!</button>
<button @click="method">Click me!</button>
2 Styling
<p v-bind:style="{ property: value }">...</p>
3 Conditionals
<p v-if="show">This element is shown/hidden in DOM</p>
<p v-else-if="show2">v-else-if is available in Vue 2.1+</p>
<p v-else>...</p>
<p v-show="false">This element is in DOM, but hidden with CSS</p>
4 Looping
<ul>
<li v-for="item in collection">{{ item }}</li>
</ul>
<template v-for="item in collections">
<h2>item.title</h2>
<p>item.text</p>
<hr/>
</template>
<ul><li v-for="(value, key) in obj">...</li></ul>
<ul><li v-for="(value, key, index) in obj">...</li></ul>
<ul><li v-for="i in 10">{{ i }}</li></ul> => 123456789
5 Components
<script>
/* global registration of a component */
Vue.component("hello", {
template: '<h1>Hello</h1>'
/* data must be a function returning a fresh object! */
data: function () {
return {
...
};
}
});
</script>
<div id="app">
<hello></hello>
<hello></hello>
</div>
/* component description */
var comp = {
template: ...,
data: function () { return { ... }; },
...
};
new Vue({
el: '#app',
/* Local registration of a component */
components: {
'my-comp': comp
}
});
6 Vue lifecycle
Vue ←watch→ Virtual DOM ←update→ DOM
new Vue();
.beforeCreate();
.created();
.beforeMount();
.updated();
.beforeUpdate();
.beforeDestroy();
.destroyed();
new Vue({
el: '#app',
beforeCreate: function () {
...;
},
created: function () {
...;
}
...
})
7 Development workflow
Code → Preprocessing → Production Server → Users
8 Vue CLI
$ npm install -g vue-cli
$ cat src/App.vue
<template>
/* there must be one root element inside a template! */
<div id="app"><h1>hello world!</h1></div>
</template>
<script>
/* a root "component" */
export default {
data: function () {
return {
data: ...
}
}
}
</script>
9 Slots
Passing some content (i.e. DOM trees) from outside into a component?
Parent.vue:
<template>
<div class="container">
<app-quote>
<h2>The Quote</h2>
<p>A wonderful quote</p>
</app-quote>
</div>
</template>
<script>
import appQuote from './components/appQuote.vue';
export default {
components: { appQuote }
}
</script>
Child component:
<template>
<div>
<slot></slot>
</div>
</template>
<script>
export default {}
</script>
In parent.vue:
<h2 slot="title">{{ quoteTitle }}</h2>
<p slot="content">A wonderful quote</p>
export default {
components: { appQuote, appAuthor, appNew },
data: { selectedComponent: 'appQoute' }
}
</script>
<template>
<div class="container">
<button @click="selectedComponent = 'appQuote'">Quote</button>
<button @click="selectedComponent = 'appAuthor'">Author</button>
<button @click="selectedComponent = 'appNew">New</button>
<hr>
<component :is="selectedComponent"></component>
</div>
</template>
11 Forms
The .lazy modifier: don't react on every little change, just on enter/blur:
<input type="password" v-model.lazy="passwd"></input>
Multiple bindings to the same name in data make an array in data automatically (useful for
checkboxes, <input type="checkbox"></input>).
<input type="radio" v-model="choice"></input>
<input type="radio" v-model="choice"></input>
<input type="radio" v-model="choice"></input>
13 Directives: v-...
Some existing directives:
<p v-text="'some text'"></p>
<p v-text="'<strong>text</strong>'"></p>
Hooks:
bind(el, binding, vnode) – Once directive is attached;
inserted(el, binding, vnode) – Inserted in parent node;
update(el, binding, vnode, oldVnode) – once component is updated (without children);
componentUpdated(el, binding, vnode, oldVnode) – (with children);
unbind(el, binding, vnode)
Vue.directive('hightlight', {
bind(el, binding, vnode) {
el.style.backgroundColor = binding.value;
},
});
Modifiers:
<div v-highlight.delayed="green"></div>
Vue.directive('highlight', {
bind(el, binding, vnode) {
if (binding.modifiers['delayed']) {
...
}
},
...
}
15 Mixins
export const fruitMixin = {
data() {
return {
fruits: ["Apple", "Banana", "Cherry"],
filterText: ""
};
},
computed: {
filteredFruits() {
return this.fruits.filter((elem) => elem.match(this.filterText));
}
}
};
import { fruitMixin } from './fruitMixin';
export default {
mixins: [fruitMixin]
data: function () {
/* mixins are merged with this: */
return { .. };
}
}
16 Transitions
<transition name="*"> elements. CSS classes:
*-enter when forward animation starts;
*-enter-active when forward animation ends;
*-leave when reverse animation starts;
*-leave-active when reverse animation ends;
<template>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<h1>Animations</h1>
<hr>
<button class="btn btn-primary" @click="show = !show">Show Alert</button>
<br/>
<!-- must contain a single element: -->
<transition name="animation-name">
<div class="alert alert-info" v-if="show">This is some info</div>
</transition>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
show: false
};
}
}
</script>
<style>
.animation-name-enter {
opacity: 0;
}
.animation-name-enter-active {
transition: opacity 1s;
}
.animation-name-leave {
/* opacity: 1; */
}
.animation-name-leave-active {
transition: opacity 1s;
opacity: 0;
}
</style>
.slide-enter {
/* transform: translateY(20px); */
}
.slide-enter-active {
animation: slide-in 1s ease-out forwards;
}
.slide-leave {
}
.slide-leave-active {
animation: slide-out 1s ease-out forwards;
}
@keyframes slide-in {
from: { transform: translateY(20px); }
to: { transform: translateY(0); }
}
@keyframes slide-out {
from: { transform: translateY(0); }
to: { transform: translateY(20px); }
}
@before-leave="beforeLeaveMethod"
@leave="leaveMethod"
@after-leave="afterLeaveMethod"
@leave-cancelled="leaveCancelledMethod"
>
<div>..</div>
</transition>
17 HTTP: vue-resource
17.1 Setup
$ npm install vue-resource
# or
$ bower install vue-resource
or
<script src="//cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
Vue.use(VueResource);
new Vue({
});
const customActions = {
saveAlt: {method: 'POST', url: 'alternative.json'}
}
this.anotherResource = this.$resource('data.json', {}, customActions);
}
};
18 Routing
18.1 Setting up
$ npm install --save vue-router
Vue.use(VueRouter);
new Vue({
el: '#app',
render: h => h(App),
router
});
App.vue:
<template>
...
<router-view></router-view>
</template>
export default {
data() {
return {
/* how to get :id value: */
id: this.$route.params.id
}
},
...
}
Author: Dmytro S.
Created: 2017-02-16 Thu 00:47
Validate