最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue js - Define prop as a specific type - Stack Overflow

programmeradmin4浏览0评论

I'm building a vuejs ponent which accepts a prop by the name of idFieldType

Now I want this prop to accept only a Number Type or a String Type

So I wrote it as follows

idFieldType: {
    Type: Function,
    default: function() {
        return Number;
    },
    validator: function() {
         if(value == String || value == Number) {
               return true;
         }

         return false;    
    }
}

I also tried replacing the type to Object.

My question is how can I write a prop that accepts only specific types ?

I'm building a vuejs ponent which accepts a prop by the name of idFieldType

Now I want this prop to accept only a Number Type or a String Type

So I wrote it as follows

idFieldType: {
    Type: Function,
    default: function() {
        return Number;
    },
    validator: function() {
         if(value == String || value == Number) {
               return true;
         }

         return false;    
    }
}

I also tried replacing the type to Object.

My question is how can I write a prop that accepts only specific types ?

Share Improve this question asked Aug 5, 2017 at 10:10 StormStorm 4,45511 gold badges42 silver badges57 bronze badges 4
  • you can. try typeOf – rashidnk Commented Aug 5, 2017 at 10:12
  • Maybe I didn't get your question correctly, but if you want one prop to be either Number or String, why not use my-prop: [String, Number]? – choasia Commented Aug 5, 2017 at 10:59
  • cos I don't want the value to be a string or number, I want the value to be the Type String or Number :) – Storm Commented Aug 7, 2017 at 8:37
  • It seems Narek-T answered your question, yet you didn't pick it as the top answer. – Pedro Amaral Couto Commented Jul 15, 2020 at 21:38
Add a ment  | 

2 Answers 2

Reset to default 14

Vue.JS has built in props validation, you can set type, required or not, default value... For example:

Vue.ponent('example', {
  props: {
    // basic type check (`null` means accept any type)
    propA: Number,
    // multiple possible types
    propB: [String, Number],
    // a required string
    propC: {
      type: String,
      required: true
    },
    // a number with default value
    propD: {
      type: Number,
      default: 100
    },
    // object/array defaults should be returned from a
    // factory function
    propE: {
      type: Object,
      default: function () {
        return { message: 'hello' }
      }
    },
    // custom validator function
    propF: {
      validator: function (value) {
        return value > 10
      }
    }
  }
})

note, that Vue.js also accepts multiple possible types propB: [String, Number],, that's what you need. And you can read more about that in official wiki

Ok, found the solution..

idFieldType: {
                type: Function,
                default: Number,
                validator: function (value) {
                    if (value() == String || value() == Number) {
                        return true;
                    }

                    return false;
                }
            }

The problem was that when specifying type as Function the default expects a function, I was returning a function returning a function :)

发布评论

评论列表(0)

  1. 暂无评论