作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在 mixin 中为我的组件定义规则。
这是我的请求的一个简单例子
https://jsfiddle.net/alexisgt01/0tg4ovnz/2/
代码:
<v-text-field :rules="[nbRules, requiredRules]" outlined v-model="name" label="Nom du ticket" required></v-text-field>
...
requiredRules: [
v => !!v || 'Le champs est obligatoire',
],
nbRules: [
v => v.length <= 10 || 'Name must be less than 10 characters',
],
但是,根据文档
Accepts an array of functions that take an input value as an argument and return either true / false or a string with an error message
,我有可能传递一个数组,但我有错误:
规则应返回字符串或 bool 值,改为接收“对象”
我还尝试使用计算为的属性:
customRules(nb = 10) {
const rules = [];
if (nb) {
const rule =
v => (v || '').length <= nb ||
`A maximum of ${nb} characters is allowed`
rules.push(rule)
}
return rules
},
同样的错误
有没有办法得到我想要的东西?
谢谢
最佳答案
您现在所做的是将包含 2 个其他数组的数组传递给 rules
,而 Vuetify 需要函数数组。
您需要先合并两个数组。最简单的方法是使用扩展语法:
<v-text-field :rules="[...nbRules, ...requiredRules]" outlined v-model="name" label="Nom du ticket" required></v-text-field>
关于javascript - 验证 : Is it possible to add many rules from data on a text component with vuetify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499247/
我是一名优秀的程序员,十分优秀!