<tfoot id='FUzYg'></tfoot>

  1. <legend id='FUzYg'><style id='FUzYg'><dir id='FUzYg'><q id='FUzYg'></q></dir></style></legend>
  2. <small id='FUzYg'></small><noframes id='FUzYg'>

    <i id='FUzYg'><tr id='FUzYg'><dt id='FUzYg'><q id='FUzYg'><span id='FUzYg'><b id='FUzYg'><form id='FUzYg'><ins id='FUzYg'></ins><ul id='FUzYg'></ul><sub id='FUzYg'></sub></form><legend id='FUzYg'></legend><bdo id='FUzYg'><pre id='FUzYg'><center id='FUzYg'></center></pre></bdo></b><th id='FUzYg'></th></span></q></dt></tr></i><div id='FUzYg'><tfoot id='FUzYg'></tfoot><dl id='FUzYg'><fieldset id='FUzYg'></fieldset></dl></div>

      • <bdo id='FUzYg'></bdo><ul id='FUzYg'></ul>

      VueJS - 对象道具验证

      时间:2023-09-29
        <bdo id='DVNgw'></bdo><ul id='DVNgw'></ul>

          <small id='DVNgw'></small><noframes id='DVNgw'>

          • <tfoot id='DVNgw'></tfoot>

              <tbody id='DVNgw'></tbody>
            1. <i id='DVNgw'><tr id='DVNgw'><dt id='DVNgw'><q id='DVNgw'><span id='DVNgw'><b id='DVNgw'><form id='DVNgw'><ins id='DVNgw'></ins><ul id='DVNgw'></ul><sub id='DVNgw'></sub></form><legend id='DVNgw'></legend><bdo id='DVNgw'><pre id='DVNgw'><center id='DVNgw'></center></pre></bdo></b><th id='DVNgw'></th></span></q></dt></tr></i><div id='DVNgw'><tfoot id='DVNgw'></tfoot><dl id='DVNgw'><fieldset id='DVNgw'></fieldset></dl></div>
              <legend id='DVNgw'><style id='DVNgw'><dir id='DVNgw'><q id='DVNgw'></q></dir></style></legend>
                本文介绍了VueJS - 对象道具验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 VueJS 中,如何验证 Object 类型的 props 以确保该对象定义了一些特定的字段?

                In VueJS, how can I validate Object type props in order to ensure that the object has some specific fields defined?

                例如,我想确保用户道具将具有姓名"、出生日期"等字段.

                For example, i want to ensure that the user prop will have the fields 'name', 'birthDate', and so on.

                提前致谢.

                推荐答案

                您可以为对象创建自定义验证器函数:

                You can create a custom validator function for objects:

                https://vuejs.org/v2/guide/components.html#道具验证

                  props: {
                     propF: {
                       validator: function (value) {
                       return value > 10
                     }
                   }
                }
                

                如果所有字段都存在,函数应该返回 true.

                Function should return true if all fields are present.

                示例:https://jsfiddle.net/wostex/63t082p2/27/

                <div id="app">
                <child :myprop="myObj"></child>
                </div>
                
                Vue.component('child', {
                    template: `<span>{{ myprop.id }} {{ myprop.name }}</span>`,
                    props: {
                      myprop: {
                        validator: function(obj) {
                          return (obj.id && Number.isInteger(obj.id) && obj.name && obj.name.length );
                        }
                      }
                    }
                });
                
                new Vue({
                    el: '#app',
                    data: {
                      myObj: {
                        id: 10,
                        name: 'Joe'
                      }
                    }
                });
                

                如果验证器失败,您将在浏览器控制台中看到 Vue 警告.

                If validator fails you will see a Vue warn in browser console.

                这篇关于VueJS - 对象道具验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:来自自定义组件的 React Native Change State 下一篇:如何防止外部 CSS 添加和覆盖 ReactJS 组件样式

                相关文章

                1. <tfoot id='H3tUr'></tfoot>

                    <small id='H3tUr'></small><noframes id='H3tUr'>

                  1. <i id='H3tUr'><tr id='H3tUr'><dt id='H3tUr'><q id='H3tUr'><span id='H3tUr'><b id='H3tUr'><form id='H3tUr'><ins id='H3tUr'></ins><ul id='H3tUr'></ul><sub id='H3tUr'></sub></form><legend id='H3tUr'></legend><bdo id='H3tUr'><pre id='H3tUr'><center id='H3tUr'></center></pre></bdo></b><th id='H3tUr'></th></span></q></dt></tr></i><div id='H3tUr'><tfoot id='H3tUr'></tfoot><dl id='H3tUr'><fieldset id='H3tUr'></fieldset></dl></div>
                    • <bdo id='H3tUr'></bdo><ul id='H3tUr'></ul>
                  2. <legend id='H3tUr'><style id='H3tUr'><dir id='H3tUr'><q id='H3tUr'></q></dir></style></legend>