简体   繁体   English

传递表单值以在Vue.js中构建Rest API URL

[英]Passing a form value to build a Rest API url in Vue.js

I created this component in vue : 我在vue中创建了这个组件:

<template>
  <div>
    <h2>Find User By ID</h2>
    <input v-model="userId" type="number" placeholder="modifiez-moi" />
    <br />
    <p v-if="userId.length != 0">
      L'Utilisateur est : {{ getUser(userId) }}
      <!-- returns nothing...-->
      <br />
      {{ user["id"] }} {{ user["email"] }} {{ user["username"]
      }}<!-- returns object-->
    </p>
  </div>
</template>

<script>
  import axios from "axios";
  export default {
    name: "FindUser",
    data() {
      return {
        user: null,
        userId: ""
      };
    },
    methods: {
      getUser(userId) {
        axios
          .get("http://localhost:4000/api/users/" + this.userId)
          .then(response => {
            console.log(response);
            this.user = response.data.data;
          });
      }
    },
    mounted() {
      // this.getUser();
    }
  };
</script>

This code is working... But i have several issues : 这段代码正在工作...但是我有几个问题:

  • if i type an id that not fit a user, the previous result is not removed 如果我输入的ID不适合用户,则不会删除先前的结果
  • Ideally i'd like to use a button to send the request 理想情况下,我想使用按钮发送请求
  • My third issue is that the request is repeated for exemple, the console.log is displayed more than one time... 我的第三个问题是,例如,重复了该请求,console.log显示了不止一次...

控制台执行的时间不同,因为在代码之上,您正在使用if语句if userId.length!= 0,然后在每个键上输入它将发送请求。请不要使用此if语句

  • Button to send the request is a good idea, use that .(other options could be keyup event, debounce if a button is too much). 使用按钮发送请求是一个好主意, 使用它 。(其他选项可以是keyup事件,如果按钮太多,则反跳)。
  • Change the v-if to something more concrete like test for user !== null since user is default as null . v-if更改为更具体的内容,例如测试user !== null因为默认情况下usernull
  • Check for a validation for a number input only when calling the method from the button that makes sure that you intended the input to be number and api is called only with a valid input. 仅当从按钮调用方法时检查number输入的有效性,以确保您希望输入为数字,并且仅使用有效输入来调用api。

Can try this: 可以尝试一下:

<template>
  <div>
    <h2>Find User By ID</h2>
    <input v-model="userId" type="number" placeholder="modifiez-moi" />
    <!--@click is shorthand for v-on:click-->
    <button @click="getUser($event)">Get User</button>
    <br />
    <p v-if="user !== null">
      L'Utilisateur est : {{ displayWhateverHere }}
      <br />
      {{ user["id"] }} {{ user["email"] }} {{ user["username"] }}
    </p>
  </div>
</template>

<script>
  import axios from "axios";
  export default {
    name: "FindUser",
    data() {
      return {
        user: null,
        userId: ""
      };
    },
    methods: {
      getUser(event) {
        /*Check for validations on input and then call the api*/
        let validFlag = !isNaN(this.userId);
        if (validFlag) {
          axios
            .get("http://localhost:4000/api/users/" + this.userId)
            .then(response => {
              console.log(response);
              this.user = response.data.data;
              /*If needed to clear the userId*/
              this.userId = "";
            });
        } else {
          /*input value is not a number -do something about it (alert/notify)*/
          this.userId = "";
        }
      }
    },
    mounted() {
      // this.getUser();
    }
  };
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM