Vue
HTML
<div id="app">
<h2>Todos:</h2>
<ol>
<todo-item v-for="(todo, index) in todos" :index="index" :title="todo.title" :done="todo.done" @cross-clic="removeTask(index)" ></todo-item>
</ol>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
a {
text-decoration: none;
}
Vue
// Définition du composant "enfant" <todo-item>
const TodoItem = {
props: {
index: Number,
title: String,
done: Boolean
},
template: `<li>
<label>
<input type="checkbox" v-model="done">
{{ title }}
</label>
<a href="#" @click.prevent="removeMe">❌</a>
</li>`,
methods: {
removeMe : function() {
this.$emit("cross-clic", this.index);
}
}
}
// Instance de Vue (composant "parent" principal)
new Vue({
el: "#app",
components : { TodoItem },
data: {
todos: [
{ title: "Nourrir le chat", done: true },
{ title: "Faire les courses", done: true },
{ title: "Apprendre Vue.js", done: false }
]
}
})