PyWeb-IL 69, Oct 2, 2017
<script src="https://unpkg.com/vue"></script>
<div id="app">
<p>{{ message }}</p>
</div>
<script>
new Vue({
el: '#app', // <- Element to mount the app on
data: { // <- Reactive data
message: 'Hello Vue.js!'
}
})
</script>
For reactive HTML attributes use v-bind
<img src="{{ imgURL }}"> <-- Won't work -->
<img v-bind:src="imgURL"> <-- That's it -->
or shorthand syntax:
<img :src="imgURL"> <-- Nice -->
Use v-on
<button v-on:click="message = 'yo'">Change message</button>
or shorthand syntax:
<button @click="message = 'yo'">Change message</button>
Control DOM events without writing code. Some modifiers from the docs:
<!-- the click event's propagation will be stopped -->
<a v-on:click.stop="doThis"></a>
<!-- the submit event will no longer reload the page -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- modifiers can be chained -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- just the modifier -->
<form v-on:submit.prevent></form>
<!-- only call vm.submit() when the keyCode is 13 -->
<input v-on:keyup.enter="submit">
<!-- Alt + C -->
<input @keyup.alt.67="clear">
<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">Do something</div>
<div id="app">
<p>{{ message }}</p>
<button @click="changeMessage"></button>
</div>
<script>
new Vue({
el: '#app', // <- Element to mount the app on
data: { // <- Reactive data
message: 'Hello Vue.js!'
},
methods: {
changeMessage: function() {
this.message = 'Yo';
}
}
})
</script>
Note the usage of this
and the way the template uses
data
and methods
without their keys.
Vue proxies them for us, some with getter/setters
to track data changes. It also handles binding this
.
Sometimes Vue can't detect property changes, e.g.:
arr[10] = 12;
or when adding keys dynamically to data
.
In those case use Vue.set
:
Vue.set(arr, 10, 12);
See also Reactivity in Depth.
<div id="example">
<p>Original message: "{{ message }}"</p>
<p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
// a computed getter
reversedMessage: function () {
// `this` points to the vm instance
return this.message.split('').reverse().join('')
}
}
})
Unlike methods, computed properties are cached based on their dependencies.
Enough theory, let's have some fun with JSFiddle.
v-if
v-else
Add/Remove items from DOM:
<div v-if="Math.random() > 0.5">
Now you see me
</div>
<div v-else>
Now you don't
</div>
Since 2.1.0
<div v-if="type === 'A'">
A
</div>
<div v-else-if="type === 'B'">
B
</div>
<div v-else-if="type === 'C'">
C
</div>
<div v-else>
Not A/B/C
</div>
Always rendered in DOM, toggles display
style.
v-for
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
<ul id="example-2">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul>
<div v-for="(value, key) in object">
{{ key }}: {{ value }}
</div>
Two way data binding with v-model
:
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
Modifiers: .lazy
, .number
, .trim
.
Let's look at the docs.
Pretty much like the Vue
instance.
Vue.component('panel', { // registered globally
props: ['message'], // declare the props the component accepts
data: function() { // Must, otherwise will be shared by instances
return {
counter: 0
}
},
template: '<div class="panel">{{ message }}</div>'
});
<panel message="hello!"></panel>
this.$emit()
to parent changes.Follows HTML template standards.
Like Angular 1's transclude
.
<-- component template -->
<div>
<h2>I'm the child title</h2>
<slot>
This will only be displayed if there is no content
</slot>
</div>
<!-- parent template -->
<div>
<h1>I'm the parent title</h1>
<my-component>
<p>This is some original content</p>
<p>Will be displayed inside the slot</p>
</my-component>
</div>
Play with build tools, single file components, browser dev tools, and ES2015 goodness.
$ # install vue-cli
$ npm install --global vue-cli
$ # create a new project using the "webpack" template
$ vue init webpack my-project
# install dependencies and go!
$ cd my-project
$ npm install
$ npm run dev
Awesome Vue.js - A curated list of Vue.js related things. .
Currently @Vast Data