Attribute Binding (ok)

Trong Vue, chúng ta có thể liên kết các thuộc tính HTML với dữ liệu Vue bằng chỉ thị v-bind. Nghĩa là, chúng ta có thể sử dụng các giá trị động thay vì các giá trị được mã hóa cứng cho các thuộc tính của mình.

C:\xampp\htdocs\html\src\App.vue

<template>
  <ul>
    <h2>{{title}}</h2>
    <div>
      <input type="text" v-model="newTask" placeholder="Add a new task" >
      <button @click="addTask" :disabled="newTask.length < 1">Add task</button>
    </div>
    <div v-if="newTask.length > 0">
      <h3>New task preview</h3>
      <p>{{ newTask }}</p>
    </div>
    <li v-for="task in tasks" :key="task.id">
      {{task.id}}. {{ task.name }}
    </li>
    <div>You have {{ allTasks }} {{ allTasks > 1 ? 'tasks' : 'task' }} at the moment</div>
    <img :src="logoURL" :alt="logoCaption" width="200" height="200" />
  </ul>
</template>
<script>
export default {
  data() {
    return {
      title: 'My To Do App',
      newTask: '',
      logoURL: 'https://images.unsplash.com/photo-1507925921958-8a62f3d1a50d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1955&q=80',
      logoCaption: 'A photo by Kelly Sikkema on Unsplash showing post-it notes',
      tasks: [
        { id: 1, name: 'Learn Vue JS', finished: false },
        { id: 2, name: 'Build a Vue application', finished: false },
        { id: 3, name: 'Write an article about Vue JS', finished: false }
      ]
    }
  },
  methods: {
    addTask() {
      if (this.newTask.length < 1) return;
      this.tasks.push({
        id: this.tasks.length + 1,
        name: this.newTask,
        finished: false
      });
      this.newTask = '';
    }
  },
  computed: {
    allTasks() {
      return this.tasks.length
    },
    latest() {
      return [...this.tasks].reverse()
    }
  }
}
</script>

Last updated