我有一个输入:

<input 
  type="text" 
  id="name" 
  class="form-control" 
  name="name"  
  v-model="form.name" 
  :disabled="validated ? '' : disabled"
/>

在Vue.js组件中,我有:

..
..
ready() {
  this.form.name = this.store.name;
  this.form.validated = this.store.validated;
},
..

验证是一个布尔值,它可以是0或1,但无论数据库中存储的是什么值,我的输入总是禁用的。

我需要输入被禁用,如果为假,否则它应该是启用和可编辑的。

更新:

这样做总是启用输入(无论我在数据库中有0或1):

<input 
  type="text" 
  id="name" 
  class="form-control" 
  name="name" 
  v-model="form.name" 
  :disabled="validated ? '' : disabled"
/>

这样做总是禁用输入(无论我有0或1在数据库):

<input 
  type="text" 
  id="name" 
  class="form-control" 
  name="name" 
  v-model="form.name" 
  :disabled="validated ? disabled : ''"
/>

当前回答

有一种新发布的叫做惰性的东西,它实际上是让浏览器忽略它。

<template>
  <input 
    type="text" 
    id="name" 
    class="form-control" 
    name="name"  
    :inert="isItInert"
  />
</template>

<script setup>
const isItInert = true
</script>

这里是测试用的操场。

其他回答

试试这个

 <div id="app">
  <p>
    <label for='terms'>
      <input id='terms' type='checkbox' v-model='terms' /> Click me to enable
    </label>
  </p>
  <input :disabled='isDisabled'></input>
</div>

js角度

new Vue({
  el: '#app',
  data: {
    terms: false
  },
  computed: {
    isDisabled: function(){
        return !this.terms;
    }
  }
})

当我们想要禁用输入时,通过将禁用道具设置为条件,我们可以在Vue 3中有条件地禁用输入

例如,我们可以这样写:

<template>
  <input :disabled="disabled" />
  <button @click="disabled = !disabled">toggle disable</button>
</template>


<script>
export default {
  name: "App",
  data() {
    return {
      disabled: false,
    };
  },
};
</script>

有一种新发布的叫做惰性的东西,它实际上是让浏览器忽略它。

<template>
  <input 
    type="text" 
    id="name" 
    class="form-control" 
    name="name"  
    :inert="isItInert"
  />
</template>

<script setup>
const isItInert = true
</script>

这里是测试用的操场。

切换输入的disabled属性非常复杂。对我来说,问题有两方面:

(1)记住:输入的“disabled”属性不是一个布尔属性。 属性的存在意味着输入被禁用。

然而,Vue.js的创建者已经准备了这个… https://v2.vuejs.org/v2/guide/syntax.html#Attributes

(感谢@connexo…如何在vuejs输入文本中添加禁用属性?)

(2)此外,还有一个DOM时间重新渲染的问题,我有。当我试图切换回来时,DOM没有更新。

在某些情况下,“组件不会立即重新渲染。它将在下一个‘滴答’中更新。”

来自Vue.js文档:https://v2.vuejs.org/v2/guide/reactivity.html

解决方案是:

this.$nextTick(()=>{
    this.disableInputBool = true
})

更完整的工作流示例:

<div @click="allowInputOverwrite">
    <input
        type="text"
        :disabled="disableInputBool">
</div>

<button @click="disallowInputOverwrite">
    press me (do stuff in method, then disable input bool again)
</button>

<script>

export default {
  data() {
    return {
      disableInputBool: true
    }
  },
  methods: {
    allowInputOverwrite(){
      this.disableInputBool = false
    },
    disallowInputOverwrite(){
      // accomplish other stuff here.
      this.$nextTick(()=>{
        this.disableInputBool = true
      })
    }
  }

}
</script>

您可以创建一个计算属性,并根据其值启用/禁用任何表单类型。

<template>
    <button class="btn btn-default" :disabled="clickable">Click me</button>
</template>
<script>
     export default{
          computed: {
              clickable() {
                  // if something
                  return true;
              }
          }
     }
</script>