<template>
  <div>
    <form @submit.prevent='submitForm'>
      <input type='text' v-model='name'>
      <input type='email' v-model='email'>
      <button :disabled='loading' :class='{ 'loading': loading }'>提交</button>
    </form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      name: '',
      email: '',
      loading: false
    }
  },
  methods: {
    submitForm() {
      this.loading = true;
      // 提交表单代码
      // 成功或失败后恢复loading为false
    }
  }
}
</script>
<style>
.loading {
  position: relative;
}

.loading:after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid transparent;
  border-top-color: #fff;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>
<h2>使用 Vue.js 实现表单提交按钮加载效果</h2>
<p>本文将介绍如何使用 Vue.js 的 <code>v-bind</code> 指令来实现表单提交按钮的加载效果。该方法可以有效地防止用户多次提交表单,并提供更友好的用户体验。</p>
<h3>实现步骤</h3>
<ol>
<li>
<p><strong>定义布尔变量 <code>loading</code></strong>: 在 <code>data</code> 中定义一个布尔变量 <code>loading</code>,并将其初始值设置为 <code>false</code>。</p>
</li>
<li>
<p><strong>绑定 <code>disabled</code> 和 <code>class</code> 属性</strong>: 在提交按钮上使用 <code>v-bind</code> 指令绑定 <code>disabled</code> 属性和 <code>class</code> 属性。</p>
<ul>
<li><code>disabled</code> 属性用于禁用按钮,防止用户多次提交表单。</li>
<li><code>class</code> 属性用于动态改变按钮样式,让用户知道表单正在提交。</li>
</ul>
</li>
<li>
<p><strong>更新 <code>loading</code> 状态</strong>: 在提交表单时,将 <code>loading</code> 变量的值设置为 <code>true</code>,并在表单提交成功或失败后将其恢复为 <code>false</code>。</p>
</li>
</ol>
<h3>示例代码</h3>
<pre><code class="language-html">&lt;template&gt;
  &lt;div&gt;
    &lt;form @submit.prevent='submitForm'&gt;
      &lt;input type='text' v-model='name'&gt;
      &lt;input type='email' v-model='email'&gt;
      &lt;button :disabled='loading' :class='{ 'loading': loading }'&gt;提交&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data() {
    return {
      name: '',
      email: '',
      loading: false
    }
  },
  methods: {
    submitForm() {
      this.loading = true;
      // 提交表单代码
      // 成功或失败后恢复loading为false
    }
  }
}
&lt;/script&gt;

&lt;style&gt;
.loading {
  position: relative;
}

.loading:after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid transparent;
  border-top-color: #fff;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
&lt;/style&gt;
</code></pre>
<h3>代码解释</h3>
<ul>
<li><code>v-bind:disabled='loading'</code>: 当 <code>loading</code> 为 <code>true</code> 时,按钮将被禁用。</li>
<li><code>v-bind:class='{ 'loading': loading }'</code>: 当 <code>loading</code> 为 <code>true</code> 时,按钮将添加 <code>loading</code> 类,并显示加载动画。</li>
<li><code>@submit.prevent='submitForm'</code>: 阻止表单默认提交行为,并在提交时调用 <code>submitForm</code> 方法。</li>
<li><code>submitForm()</code> 方法: 将 <code>loading</code> 设置为 <code>true</code>,提交表单代码,并在提交成功或失败后将 <code>loading</code> 恢复为 <code>false</code>。</li>
</ul>
<h3>总结</h3>
<p>通过使用 <code>v-bind</code> 指令和布尔变量,可以轻松实现表单提交按钮的加载效果,提升用户体验。该方法简单易用,并且可以根据需求进行定制。</p>
Vue 提交表单按钮加载效果实现方法 - 使用 v-bind 指令

原文地址: https://www.cveoy.top/t/topic/oWG0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录