【问题标题】:How to add multiple data types for VueJs Props?如何为 VueJs Props 添加多种数据类型?
【发布时间】:2019-05-05 14:27:32
【问题描述】:

将不同的值传递给组件时,我遇到了这个错误。

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    这是我找到的解决方案。

    props: {
       value: [Number, String, Array]
    }
    

    【讨论】:

    • 如果你不关心类型,你不必指定它。当然,如果你期望这三种,最好明确说明它们。
    • 使用第一个解决方案,eslint 给我一个错误:The "value" property should be a constructor (vue/require-prop-type-constructor)。第二种解决方案不会产生错误或警告
    • 我可以定义多种类型以及所需的字符串吗?就像下面的order: { type: [Object, null], required: true },因为这会给我带来错误。
    • 第一个提出的带有管道字符的解决方案 - 这不可能工作,因为管道字符是 XOR 运算,Number XOR String 的结果是 0。所以基本上,你设置 value : 0。我的猜测是,如果值为零,vuejs 只会忽略类型,所以它看起来它可以工作,但它实际上总是工作,无论实际传递的值如何。
    【解决方案2】:

    带有管道的语法 (Number | String),就像在接受的答案中提出的那样,实际上不起作用。这是一个更详细的示例解决方案:

    类型检查,不需要道具

    使用以下语法对一个 prop 进行类型检查:

    props: {
      username: {
        type: [ String, Number ]
      }
    }
    

    这是一个带有类型检查的属性的实例:

    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    Vue.component('test-component', {
      name: 'TestComponent',
      props: {
        username: {
          type: [ String, Number ]
        }
      },
      template: `<div>username: {{ username }}</div>`
    });
    
    new Vue({ el: '#app' });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
    
    <div id="app">
      <!-- valid: String -->
      <test-component :username="'user 38'"></test-component>
      
      <!-- valid: Number -->
      <test-component :username="59"></test-component>
      
      <!-- valid: null is valid, it is not required -->
      <test-component :username="null"></test-component>
    
      <!-- valid: missing property is valid, it is not required -->
      <test-component></test-component>
    
      <!-- invalid: Array -->
      <test-component :username="['test', 456]"></test-component>
    </div>

    类型检查、必需的道具和自定义验证器

    使用以下语法对所需属性和自定义验证器进行类型检查。

    props: {
      username: {
        type: [ String, Number ],
        required: true, // optional
        validator: item => item !== '123' // optional
      }
    }
    

    以下是必需属性和自定义验证器的实时示例:

    Vue.config.devtools = false;
    Vue.config.productionTip = false;
    
    Vue.component('test-component', {
      name: 'TestComponent',
      props: {
        username: {
          type: [ String, Number ],
          required: true,
          validator: item => item !== '123'
        }
      },
      template: `<div>username: {{ username }}</div>`
    });
    
    new Vue({ el: '#app' });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
    
    <div id="app">
      <!-- valid: String -->
      <test-component :username="'user 38'"></test-component>
      
      <!-- valid: Number -->
      <test-component :username="59"></test-component>
      
      <!-- invalid: Array -->
      <test-component :username="['test', 456]"></test-component>
      
      <!-- invalid: String, but disallowed by custom validator -->
      <test-component :username="'123'"></test-component>
      
      <!-- invalid: null property, it is required though -->
      <test-component :username="null"></test-component>
    
      <!-- invalid: missing required prop -->
      <test-component></test-component>
    </div>

    【讨论】:

      【解决方案3】:

      正如其他人所建议的,在 vuejs 中有两种定义 props 的方法:

      第一个

      //No need to define the type with this one
      props: ['myVariable', 'foo', 'something']
      

      第二个

      //With this one you can define what type the prop is and other different useful things!
      props: {
        myVariable: String, //You can define the type like this
        anyOfTheFollowing: String/Object/Array, //You can also define multiple possible types
        'kebab-case-like': Function, //Since vuejs is still javascript and the property 'props' is actually an object, you can define your props like this for kebab-case. You can also just use camelCase and use the kebab-case version in your template and it will still recognize it
        customOne: MyCustomType, //You can in theory use classes you've defined aswell
        foo: { //This is another way of defining props. Like an object
          type: Number,
          default: 1, //This is why this is mostly used, so you can easily define a default value for your prop in case it isn't defined
        },
        andAnotherOne: {
          type: Array,
          default: () => [], //With Arrays, Objects and Functions you have to return defaults like this since you need to return a new reference to it for it to be used
        },
        requiredOne: {
          type: Object,
          required: true //Another use for this. When it is marked as required and it isn't defined you'll get an error in the console telling you about it
        }
      }
      

      IMO 我喜欢第二个版本,因为它对更多内容开放,我特别喜欢默认属性。

      【讨论】:

        【解决方案4】:

        如果您对类型没有任何头疼的问题,一般将 props 列为字符串数组:

        props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
        

        如果您希望每个道具都是特定类型的值。在这些情况下,您可以将道具列为对象,其中属性的名称和值分别包含道具名称和类型:

        props: {
            title: String,
            likes: Number,
            isPublished: Boolean,
            commentIds: Array,
            author: Object
        }
        

        如果你想使用多种类型,那么如下:

        props: {
            value: [String, Number],
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-21
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多