javascript - 如果输入字段为空则显示错误消息,否则没有消息 Vue.js

标签 javascript vue.js vue-component

如果用户未在搜索框中输入任何内容,我想显示一条错误消息,但是当他们开始输入时,消息就会消失。到目前为止,我已经显示了错误消息,但一旦用户开始输入

,该消息就不会消失

new Vue({
  el: "#app",
  name: "Hero",
  props: {
    navLink: String
  },
  data: function() {
    return {
      title: "Simple Search",
      intro: "This is a simple hero unit, a simple jumbotron-style.",
      subintro: "It uses utility classes for typography and spacing to space content out.",
      result: [],
      errors: [],
      search: "",
      loading: ""
    };
  },

  watch: {
    search: function(val) {
      if (!val) {
        this.result = [];
      }
    }
  },

  methods: {
    getData: function() {
      this.loading = true;
      fetch(`https://itunes.apple.com/search?term=${this.search}&entity=album`)
        .then(response => response.json())
        .then(data => {
          this.result = data.results;
          this.loading = false;
          console.log(data);
        });
      if (this.search) return true;
      this.errors = [];
      if (!this.search) this.errors.push("Enter search field.");
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.js"></script>
<div id="app">
  <template>
      <div class="jumbotron">
        <h1 class="display-4">{{title}}</h1>
        <p class="lead">{{intro}}</p>
        <hr class="my-4">
        <p v-if="errors.length">
          <b>Please correct the following error(s):</b>
        </p>
        <p v-for="(error, index ) in errors" :key="index">{{ error }}</p>
        
        <input class="form-control form-control-lg mb-3" type="search" placeholder="Search"
     aria-label="Search" v-model="search" required >
    
        <div class="loading" v-if="loading"></div>
    
        <table class="table table-sm table-light table-bordered" v-if="result.length">
          <thead class="thead-dark">
            <tr class="col-8">
              <th scope="col">Name</th>
              <th scope="col">Artist</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(result, index) in result" :key="index">
              <td>{{result.collectionName}}</td>
              <td>{{result.artistName}}</td>
            </tr>
          </tbody>
        </table>
        <button
          class="btn btn-success btn-lg btn-block mb-3"
          type="submit"
          v-on:click="getData"
          v-if="result.length < 1"
        >Get data</button>
      </div>
    </template>
</div>

代码。我确信这很简单,但我看不到在哪里?我的代码是:

最佳答案

如果用户未在搜索框中输入任何内容,我想显示一条错误消息,但是当他们开始输入时,该消息就会出现。

考虑你的问题 -

您需要双向绑定(bind)来解决此问题。无需观看!

new Vue({
  el: "#app",
  data() {
  	return {
      isValidationAllowed: false,
    	searchTerm: ''
    }
  },
  computed: {
    validated() {
      return this.isValidationAllowed && !this.searchTerm
    }
  },
  methods: {
    validate() {
      this.isValidationAllowed = true
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
 <input type="text" v-model="searchTerm">
 <p v-if="validated">Hey You got the error</p>
 <button @click="validate">Submit </button>
</div>

关于javascript - 如果输入字段为空则显示错误消息,否则没有消息 Vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628852/

相关文章:

javascript - 如何让svg对象做圆周轨迹匀速运动,D3.js

javascript - 为什么没有定义 $store?

vue.js - 如何将 API 响应中的数据分配给状态?

vue.js - 如何从 Vuex 操作访问 Nuxt 上下文变量

javascript - [Vue 警告] : Error in nextTick: "NotFoundError: Failed to execute ' insertBefore' on 'Node'

javascript - 我在使用 VueJS 将数据输出到动态生成的表时遇到问题

javascript - 波斯语中的 jQuery

javascript - 刚刚在 JavaScript 中进行了分配,它也不会推出 html onclick...什么给出了?

javascript - 如何在不再次发送所有 APK 的情况下更新我的应用程序?

vue.js - 如何在Vue中使用 Elasticsearch ?如何在 Elasticsearch 中存储一些数据?