Kasun Vithanage
Co-Founder of SyetaLabs
Contributor at Joomla
Google Summer of Code Mentor
Member at Mozilla Sri Lanka
Member at FOSS Sri Lanka
Evening Dev Talks
Opensource ❤️
Evan You
(Vue 3)
<div id="app">
{{ message }}
</div>const App = {
data() {
return {
message: 'Hello VueJS'
}
}
}
Vue.createApp(App).mount("#app")
<div id="app">
<button v-on:click="increment">
Increment
</button>
{{ count }}
</div>const App = {
data() {
return {
count: 0
}
},
methods: {
increment() {
this.count++
}
}
}
Vue.createApp(App).mount("#app")
<div id="app">
<input type="text" v-model="message" />
{{ message }}
</div>
<div id="app">
<input type="text" v-model="message" />
{{ message }}
<div class="danger" v-if="message.length > 25">
Too long!!!
</div>
</div>
<div id="app">
<ul>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ul>
</div>const App = {
data() {
return {
todos: [
{ text: 'Bring milk' },
{ text: 'Bring eggs' },
{ text: 'Buy butter' }
]
}
}
}
Vue.createApp(App).mount("#app")
<div id="app">
<button @click="disabled = !disabled">Toggle</button>
<button v-bind:disabled="disabled">Click Me!!!</button>
</div>const App = {
data() {
return {
disabled: false
}
}
}
Vue.createApp(App).mount("#app")
<div id="app">
<button @click="count++">Increment</button>
<button @click="count--">Decrement</button>
<div class="counter" :class="{ 'red': count > 5 }">
{{ count }}
</div>
</div>const App = {
data() {
return {
count: 0
}
}
}
Vue.createApp(App).mount("#app")
Let's show completed items
<div id="app">
All:
<ul>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ul>
Completed:
<ul>
<li v-for="todo in completed">
{{ todo.text }}
</li>
</ul>
</div>const App = {
data() {
return {
todos: [
{ text: 'Bring milk', done: false },
{ text: 'Bring eggs', done: true },
{ text: 'Buy butter', done: true }
]
}
},
computed: {
completed() {
return this.todos.filter(todo => todo.done === true)
}
}
}
Vue.createApp(App).mount("#app")
Free API https://swapi.dev/
<div id="app">
<input type="text" v-model="search" placeholder="search..." />
<ul>
<li v-for="(result, i) in results" :key="i">
{{ result.name }}
</li>
</ul>
</div>const App = {
data() {
return {
search: "",
results: []
};
},
watch: {
search(newValue) {
if(newValue.trim().length > 0)
this.lookup();
}
},
methods: {
lookup() {
fetch(`https://swapi.dev/api/people/?search=${this.search}`)
.then((resp) => resp.json())
.then((data) => (this.results = data.results))
.catch((e) => console.log(e));
}
}
};
Vue.createApp(App).mount("#app");
<div id="app">
<div class="profile">
<p>Name: Yoda</p>
<p>Age: 900 years</p>
</div>
</div>
const App = {
data() {
return {
user: {
name: 'Yoda',
age: 900
}
}
}
}
const app = Vue.createApp(App)<div id="app">
<profile :user="user" />
</div>app.component("profile", {
props: ["user"],
template: `
<div class="profile">
<p>Name: {{ user.name }}</p>
<p>Age: {{ user.age }} years</p>
</div>
`
});
// finally mount it
app.mount("#app");
app.component("profile", {
props: ["user"],
template: `
<div class="profile">
<p>Name: {{ user.name }}</p>
<p>Age: {{ user.age }} years</p>
</div>
`
});
Whats next?