A todo demo using Vue in Laravel
Mar 21, 2016 · 1 分钟阅读

html

<div id="app">
  <ul>
    <li><a v-link="'tasks'">tasks</a></li>
    <li><a v-link="'about'">about</a></li>
  </ul>

  <router-view></router-view>
</div>

<script src="js/main.js"></script>

main.js

import tasks from './components/tasks.vue';

const Vue = require('vue');
const VueRouter = require('vue-router');

Vue.use(VueRouter);
Vue.use(require('vue-resource'));
Vue.http.headers.common['X-CSRF-TOKEN'] = document.getElementById('csrf_token').getAttribute('content');

const router = new VueRouter();

router.map({
  '/tasks': {
    component: tasks
  },

  '/about': {
    component: {
      template: `
        <div>
          <h3>About Page</h3>
          <p>This is the about page</p>
        </div>
      `
    }
  }
});

router.start({}, '#app');

tasks.vue

<template>
  <h3>My Tasks <span v-show="list.length">({{ list.length }})</span></h3>
  <div class="form-group">
    <label for="">Add Task</label>
    <input v-model="task" @keyup.enter="postTask(task)" type="text" class="form-control">
  </div>
  <ul class="list-group">
    <li v-for="task in list" class="list-group-item">
      {{ task.body }}
      <strong @click="deleteTask(task)">x</strong>
    </li>
  </ul>
</template>

<script>
  export default {
    data() {
      return {
        task: '',
        list: [],
        resource: this.$resource('/api/tasks{/id}')
      };
    },

    created() {
      this.getTasks();
    },

    methods: {
      getTasks() {
        this.resource.get().then(tasks => {
          this.list = tasks.data;
        });
      },

      postTask(task) {
        if (!task.trim()) return;
        this.resource.save({
          body: task
        }).then(() => {
          this.getTasks();
          this.task = '';
        });
      },

      deleteTask(task) {
        this.resource.delete({
          id: task.id
        }).then(() => {
          this.list.$remove(task);
        });
      }
    }
  };
</script>
← Previous Post Next Post →

Ryun的博客
与其感慨路难行,不如马上出发。

atom css design git html javascript jekyll laravel life mac mobile optimization sublime tool vscode vue
mac

About

你好,❤朋友

这里是 Ryun 的博客 📝

📝记录了我学习 🔎的过程

作为一名前端攻城狮 🦁

希望能和大家一起 🔎

共同进步 🏃

🦁的特长:

  • HTML5
  • CSS3
  • JavaScript
  • Vue.js
  • 微信小程序
  • 熟练配合 Node.js、PHP 和 Laravel
  • 熟练使用各种开发和设计工具

🦁喜欢 🆒🆒的东西

对提升效率和美感的事物有兴趣 😋

欢迎 👏交流


see this hugo-theme-dream-ink