【问题标题】:Todo list with Laravel and Vue.js v-model problem带有 Laravel 和 Vue.js v-model 问题的待办事项列表
【发布时间】:2020-03-18 05:56:56
【问题描述】:

我使用 Laravel 和 Vue.js 创建了一个待办事项列表:我可以添加一个类别,并且可以为每个类别添加一个待办事项列表。对于每个类别列表,都有一个用于为该类别添加新待办事项的输入。

我用一个组件制作了所有东西。我用 Laravel 创建了 api。

一切都很完美。唯一的问题是:当我在输入“添加待办事项”中写一些东西时,它也会写在其他输入中。这是合乎逻辑的,因为输入标签中有 v-model 指令。

是否可以绕过这种行为?

vue组件的内容:

<template>
<div>
    <form @submit.prevent="addCategorie()">
        <!-- Grid row -->
        <div class="form-row align-items-center" style="margin-bottom: 2rem;">
            <!-- Grid column -->
            <div class="col-auto">
                <!-- Default input -->
                <input v-model="categorie.name" type="text" class="form-control" id="name" aria-describedby="name" placeholder="Categorie">
            </div>
            <!-- Grid column -->

            <!-- Grid column -->
            <div class="col-auto">
                <button type="submit" class="btn btn-primary btn-md mt-0">Add</button>
            </div>
        </div>
        <!-- Grid row -->
    </form>
     <div class="row">
        <section v-for="(categorie) in categories" v-bind:key="categorie.id" class="col-sm-6 col-md-4">
                <div class="card" style="margin-bottom: 2rem;">
                    <div class="card-header align-items-center">
                        <h3>{{ categorie.name }}
                            <span @click="deleteCategorie(categorie.id)"  style="font-size: 24px; color: #FF3547;">
                                <i class="fas fa-trash-alt"></i>
                            </span>
                        </h3>
                        <input  v-on:keyup.enter="addTodo(categorie.id)" v-model="todo.name" type="text" class="form-control" id="name" aria-describedby="name" placeholder="Add a todo">
                    </div>
                    <ul v-for="todo in categorie.todos" v-bind:key="todo.id" class="list-group list-group-flush">
                        <li class="list-group-item">
                            {{ todo.name }}
                            <span @click="deleteTodo(todo.id)"  style="font-size: 14px; color: #FF3547;">
                                <i class="fas fa-trash-alt"></i>
                            </span>
                        </li>
                    </ul>
                </div>
        </section>
    </div>
</div>

<script>
export default {
    data() {
        return {
            categories: [],
            categorie: {
                id: '',
                name: ''
            },
            categorie_id: '',
            todos: [],
            todo: {
                id: '',
                name: '',
                categorie_id: '',
                check: ''
            },
            todo_id: ''
        }
    },

    created() {
        this.fetchTodos();
        this.fetchCategories();
    },

    methods: {
        fetchTodos() {
            fetch('api/todos')
            .then(res => res.json())
            .then(res => {
                this.categories = res.data
            })
        },
        fetchCategories() {
            fetch('api/categories')
            .then(res => res.json())
            .then(res => {
                this.categories = res.data
            })
        },
        deleteCategorie(id) {
           if (confirm('Are you sure ?')) {
                fetch(`api/categorie/${id}`, {
                    method: 'delete'
                })
                .then(res => res.json())
                .then(data => {
                    this.fetchCategories()
                    this.fetchTodos()
                })
                .catch(err => alert(err))
           }
        },
        deleteTodo(id) {
           if (confirm('Are you sure ?')) {
                fetch(`api/todo/${id}`, {
                    method: 'delete'
                })
                .then(res => res.json())
                .then(data => {
                    this.fetchTodos()
                })
                .catch(err => alert(err))
           }
        },
        addCategorie() {
            fetch('api/categorie', {
            method: 'post',
            body: JSON.stringify(this.categorie),
            headers: {
                'content-type': 'application/json'
                }
            })
            .then(res => res.json())
            .then(data => {
                this.clearForm();
                this.fetchCategories();
                this.fetchTodos();
            })
            .catch(err => console.log(err));
        },
        addTodo(categorie_id) {
            // Add
            fetch(`api/todo/${categorie_id}`, {
            method: 'post',
            body: JSON.stringify(this.todo),
            headers: {
                'content-type': 'application/json'
                }
            })
            .then(res => res.json())
            .then(data => {
                this.clearForm();
                this.fetchTodos();
                this.fetchCategories();
            })
            .catch(err => console.log(err));
        },
        clearForm() {
            this.todo.name = '';
            this.categorie.name = '';
        }
    }
}

也许有人可以帮助我 非常感谢你,祝你周末愉快

最好的问候,

西里尔

【问题讨论】:

  • 您的v-model="todo.name" 指的是您数据中的单个todo.name,所以它们当然都是一样的

标签: javascript laravel vue.js


【解决方案1】:

最后,我找到了另一个解决方案 我从输入中获取值并将参数传递给 api url

代码:

在模板中输入:

<input  v-on:keyup.enter="addTodo(categorie.id)" type="text" class="form-control" v-bind:id="categorie.id" aria-describedby="name" placeholder="Add a todo">

函数:addTodo()

addTodo(categorie_id) {
            let val = document.getElementById(categorie_id).value
            fetch(`api/todo/${categorie_id}/${val}`, {
            method: 'post',
            body: JSON.stringify(this.todo),
            headers: {
                'content-type': 'application/json'
                }
            })
            .then(res => res.json())
            .then(data => {
                this.clearInput(categorie_id);
                this.fetchTodos();
                this.fetchCategories();
            })
            .catch(err => console.log(err));
        },

以及清除输入的功能:

clearInput(categorie_id) {
            document.getElementById(categorie_id).value = '';
        }

最好的问候,

西里尔

【讨论】:

    【解决方案2】:

    您的数据中应该有另一个属性可以在您的输入字段中使用,例如 form: { name: '' } 并在您的输入 v-model="form.name" 上使用这个作为 v-model

    【讨论】:

    • 你好玛丽安谢谢你的回答,但我真的不明白。如果我添加一个属性并在我的 v-model 中使用该属性,我会遇到同样的问题,并且数据没有保存在数据库中
    • @Cyril,仅在表单输入 v-model 中使用表单对象。然后在您的 addCategorie 正文中使用它,例如 this.form.name 而不是 this.categorie。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2018-06-04
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2018-07-04
    相关资源
    最近更新 更多