Desenvolupament web en entorn client (DAW)
Quan hem de treballar amb projectes grans, es recomana utilitzar components.
Els components son instàncies de Vue reutilitzables.
Fem un cop d’ull a l’estructura de la nova aplicació:
Hi ha un enllaç al component app
<div id="app"></div>
<template>
: codi html<script>
: codi javascript/vue<style>
: estils CSSAquest component conté, a dins un altre component: ./components/HelloWorld.vue
A partir de l’exemple que ja vam veure:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="(world, i) in worlds">
{{world}}
<button @click="worlds.splice(i, 1)">Zap!</button>
</li>
</ul>
<input v-model="newWorld"/>
<button @click="worlds.push(newWorld)">Conquer</button>
</div>
<script>
new Vue({
el: '#app',
data: {
worlds: ['Terran', 'L24-D', 'Ares', 'New Kroy', 'Sebek', 'Vestra']
}
})
</script>
</body>
</html>
Crearem un component (fitxer App.vue
)
<template>
<div id="app">
<ul>
<li v-for="(world, i) in worlds" v-bind:key="i">
{{world}}
<button @click="worlds.splice(i, 1)">Zap!</button>
</li>
</ul>
<input v-model="newWorld"/>
<button @click="worlds.push(newWorld)">Conquer</button>
</div>
</template>
<script>
export default {
name: 'app',
data: function() {
return {
worlds: ['Terran', 'L24-D', 'Ares', 'New Kroy', 'Sebek', 'Vestra'],
newWorld: ''
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
}
</style>
Canvis:
v-for
(v-bind:key
).data
ha de ser una funció.