DWEC

Logo

Desenvolupament web en entorn client (DAW)

View the Project on GitHub jrodr236/DWEC

Components de Vue.js

The evolution of software architecture

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ó:

Crear un nou component

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: