如何解决输入值的问题?

3

我有一个post.titlepost.body的值,我需要在更改文本输入框中的值后将其保存在数据中,以便稍后为用户编写(PUT请求)API时使用这些新值。我该如何实现?

应用程序截图

以下是我的代码 -

<template>
    <div id="app">
        <input type="text" v-model="createTitle" />
        <input type="text" v-model="createBody" />
        <button @click="addPost()">AddPost</button>
        <ul>
            <li v-for="(post, index) of posts">
                <p>{{ post.title }}</p>
                <p>{{ post.body }}</p>
                <button @click="deleteData(index, post.id)">Delete</button>
                <button @click="visiblePostID = post.id">
                    Изменить
                </button>
                <transition v-if="visiblePostID === post.id">
                    <p><input :value="post.title"><br><input :value="post.body">
                        <button type="button" @click="changePost(post.id, post.title, post.body)">Применить</button></p>
                </transition>
            </li>
        </ul>
    </div>
</template>
<script>
import axios from 'axios';

export default {
    name: 'app',
    data() {

        return {
            posts: [],
            createTitle: '',
            createBody: '',
            visiblePostID: '',

        }
    },
    changePost(id, title, body) {
        axios.put('http://jsonplaceholder.typicode.com/posts/' + id, {
            title: title,
            body: body
        })
    }
}
</script>
2个回答

2

如果要进行双向数据绑定,您应该使用v-model。在这里阅读更多信息


<transition v-if="visiblePostID === post.id">
  <p>
    <input v-model="post.title">
    <br>
    <input v-model="post.body">
    
    <button type="button" @click="changePost(post.id, post.title, post.body)">Применить</button>
  </p>
</transition>

1
为了补充@Riddhi的回答,您可以在这些输入上使用v-model和临时变量,这样只有在PUT请求成功确认后,模型才会被修改:
  1. Add temporary data properties to hold the <input> values from the template:

    // template
    <transition v-if="visiblePostID === post.id">
      <input v-model="tmpTitle" />
      <input v-model="tmpBody" />
    </transition>
    
    // script
    data() {
      return {
        tmpTitle: '',
        tmpBody: ''
      }
    }
    
  2. Replace the edit-button's handler with a method (named editPost()), and pass to the method the current post's ID, title, and body, which will be stored in the temporary data properties declared above:

    // template
    <button @click="editPost(post.id, post.title, post.body)">
      Изменить
    </button>
    
    // script
    methods: {
      editPost(id, title, body) {
        this.tmpTitle = title;
        this.tmpBody = body;
        this.visiblePostID = id;
      }
    }
    
  3. Update changePost() to also take the current post, which will be updated with the temporary data properties once the PUT request is successful.

    // template
    <button type="button" @click="changePost(post, post.id, tmpTitle, tmpBody)">
      Применить
    </button>
    
    // script
    methods: {
      async changePost(post, id, title, body) {
        const { status } = await axios.put("https://jsonplaceholder.typicode.com/posts/" + id, { title: title, body: body });
        if (status === 200 /* HTTP OK */) {
          post.title = title;
          post.body = body;
        }
      }
    }
    

demo

可以翻译为:在HTML中插入链接,链接的名称为“demo”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接