vue.js - 视觉 : need to disable all inputs on page

标签 vue.js vue-directives

我正在开发一个具有不同许可证类型的应用程序,并且根据我们需要禁用/启用输入的许可证。

一种方法是为每个输入设置条件 :disabled,但这需要大量工作并且容易出错,因为我们可能会忘记将它放在某些输入上。

我想到了使用像 v-disable-all 这样的指令来搜索容器下的所有输入并向它们添加禁用。

我在徘徊是否有更好的解决方案或者是否已经有这样的解决方案?

最佳答案

我最终创建了这个指令:

import Vue from "vue";

Vue.directive("disable-all", {
  // When all the children of the parent component have been updated
  componentUpdated: function(el, binding) {
    if (!binding.value) return;
    const tags = ["input", "button", "textarea", "select"];
    tags.forEach(tagName => {
      const nodes = el.getElementsByTagName(tagName);
      for (let i = 0; i < nodes.length; i++) {
        nodes[i].disabled = true;
        nodes[i].tabIndex = -1;
      }
    });
  }
});

关于vue.js - 视觉 : need to disable all inputs on page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55905055/

相关文章:

vue.js - vuejs 中左侧或右侧带有操作(按钮)的文本字段

javascript - vue 列表中的每个项目的 if 不同

laravel - 使用 Laravel-Websockets 向特定用户发送消息(一对一聊天)

vue.js - Vee 验证 - [Vue 警告] : Failed to resolve directive: validate

vue.js - Vue 计算不起作用

vue.js - 删除 Nuxt/Vue 中的事件监听器

vue.js - VueJS : <template> vs <div> or related for grouping elements for conditional rendering

javascript - Canvas : not all objects in an array of bouncing ball objects are showing up

vue.js - 用于 auth header 的 vue-resource 拦截器