【问题标题】:How to conditionally render with button in Vuejs?如何在 Vuejs 中使用按钮有条件地渲染?
【发布时间】:2021-02-26 06:45:45
【问题描述】:

export default {
  name: "HelloWworld",

  data: function () {
    return {
      
      isHidden: false,
      isWelcome: false,
      isFadeout: false,
      
      }
      }

<div  v-if="!isHidden">
 //some code for screen1
 
  <button v-on:click="isHidden = true"> HELLO</button>
  </div>
  
   <div  v-else-if="isHidden && !isFadeout">
 //some code for screen 2
 
  <button v-on:click="isFadeout = true"> Hi</button>
  </div>
  
   <div  v-else-if="isFadeout && isHidden && !isWelcome">
 <input
            :type="passwordFieldType"
            v-model="user.password"
            id="password"
            name="password"
            class="input-section-three"
            :class="{ 'is-invalid': submitted && $v.user.password.$error }"
            placeholder="Enter new password"
            :maxlength="maxpassword"
            v-on:keypress="isPassword($event)"
          />

<div
            v-if="submitted && $v.user.password.$error"
            class="invalid-feedback-two"
          >
            <span v-if="!$v.user.password.required">Password is required</span>
            <span v-if="!$v.user.password.minLength"
              >Minimum 8 character with
                        alphanumeric along with 1 capital letter, 1 small letter
                        and 1 special character at least</span
            >
          </div>

   <input
            :type="passwordFieldTypetwo"
            v-model="user.confirmPassword"
            id="confirmPassword"
            name="confirmPassword"
            class="input-section-three"
            :class="{
              'is-invalid': submitted && $v.user.confirmPassword.$error
            }"
            placeholder="Confirm password"
            :maxlength="maxconfirmpassword"
            v-on:keypress="isconfirmPassword($event)"
            :disabled="user.password.length < 8"
          />
           <div
            v-if="submitted && $v.user.confirmPassword.$error"
            class="invalid-feedback-two"
          >
            <span v-if="!$v.user.confirmPassword.required"
              >Confirm Password is required</span
            >
            <span v-else-if="!$v.user.confirmPassword.sameAsPassword"
              >Password must match</span
            >
          </div>
          
 <button  v-on:click="isWelcome = true" :disabled="user.confirmPassword.length < 8" > SUBMIT </button>
  </div>
  
   <div  v-else-if="isWelcome">
 //some code for screen 4
 
  <button>Fine</button>
  </div>

条件渲染工作正常,但是,在 screen3 提交按钮代码中,我有 v-on:click="isWelcome = true",如果我删除此行,则在单击提交按钮时会发生密码验证,如果我再次放置 v-on:click="isWelcome = true" 而不检查验证它正在移动到第 4 个屏幕。

我想要做的是,检查验证并通过点击屏幕 3 中的提交按钮移至第四屏幕。我猜逻辑应该放在屏幕 3 中的提交按钮内。

【问题讨论】:

  • 嗨 Abhinav,为什么你不使用 computed 属性来检查你的验证,然后再返回 true 到 welcome
  • 有了这个答案 v-on:click="isWelcome = user.confirmPassword.length >= 8"
  • 使用此代码可以正常工作,但问题是,最初如果我单击 screen3 中的提交按钮,验证工作正常,但如果我输入的密码不匹配,但如果超过,则在密码确认字段中8 个字符。它正在重定向到第 4 个屏幕

标签: javascript vue.js vuelidate


【解决方案1】:

我假设您已经定义了用于 v-model 的 user 对象。如果不是,那可能是问题的一部分。但是这会起作用

v-on:click="[isWelcome = user.confirmPassword.length >= 8]"

如果这不起作用,那么问题将出在您的用户对象和 v-models 上。

【讨论】:

  • 使用此代码可以正常工作,但问题是,最初如果我单击 screen3 中的提交按钮,验证工作正常,但如果我输入的密码不匹配,但如果超过,则在密码确认字段中8 个字符。它正在重定向到第 4 个屏幕。
  • @taneerudhanunjay 我已经更新了答案。尝试将点击回调包装成[]
  • 试过同样的问题,可能需要在确认密码字段中更改任何内容,以保持密码相同仅重定向到另一个页面的情况。
  • 这是我在确认密码验证中的代码 confirmPassword: { required, sameAsPassword: sameAs("password") },
猜你喜欢
  • 1970-01-01
  • 2021-11-27
  • 2022-11-22
  • 1970-01-01
  • 2020-11-06
  • 2021-12-17
  • 2021-01-30
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多