How to disable input conditionally in vue.js

后端 未结 11 1240
余生分开走
余生分开走 2020-12-07 10:11

I have an input:



        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 10:39

    To toggle the input's disabled attribute was surprisingly complex. The issue for me was twofold:

    (1) Remember: the input's "disabled" attribute is NOT a Boolean attribute.
    The mere presence of the attribute means that the input is disabled.

    However, the Vue.js creators have prepared this... https://vuejs.org/v2/guide/syntax.html#Attributes

    (Thanks to @connexo for this... How to add disabled attribute in input text in vuejs?)

    (2) In addition, there was a DOM timing re-rendering issue that I was having. The DOM was not updating when I tried to toggle back.

    Upon certain situations, "the component will not re-render immediately. It will update in the next 'tick.'"

    From Vue.js docs: https://vuejs.org/v2/guide/reactivity.html

    The solution was to use:

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

    Fuller example workflow:

提交回复
热议问题