【问题标题】:Input field required Vue js需要输入字段Vue js
【发布时间】:2021-07-11 05:12:15
【问题描述】:

我有一个 Vue.js 组件输入字段,我在不同的文件中使用这个组件。我想要的是在一些文件中我希望这些文件是必需的。

这是我的组件:测试输入

 <template> 
        <input type="text" value="Some value...">
    </template>
    export default {
    name:'test-input'
    }

Test1.vue:

<template> 
    <test-input required/>
</template>

Test2.vue:

<template> 
    <test-input/>
</template>

我尝试使用你在 test1.vue 的测试输入中看到的 required 只是我希望这是必需的

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    您应该明确地执行 &lt;input v-bind="$props"&gt;&lt;input v-bind="$attrs"&gt;

    ($attrs vs $props)

    medium article

    【讨论】:

      【解决方案2】:

      可能是因为您设置了默认值吗?这样,除非您删除字段中的文本,否则它永远不会为空。你不是说“占位符”而不是“价值”吗?

      <template>
        <input type="text" placeholder="Some value..."/>
      </template>
      <script>
        export default {
          name:'test-input'
        }
      </script>
      

      这样对我有用:

          <form action="#">
            <test-input required/>
            <button type="submit">Send</button>
          </form>
      

      【讨论】:

      • 试过了,但没用,我发现有些人使用 :rules:required 你知道吗
      • 不知道...但是将props: { req: { Boolean, default: false } 添加到组件然后像&lt;TestInput :req="true" /&gt; 一样使用它呢?我没有尝试过@CosimoChellini 的&lt;input v-bind="$props"&gt;&lt;input v-bind="$attrs"&gt;
      猜你喜欢
      • 2021-08-15
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2018-06-05
      • 2013-05-12
      • 1970-01-01
      相关资源
      最近更新 更多