Home > Software engineering >  Is there a way to achieve 'prop type => any' in Vue.js?
Is there a way to achieve 'prop type => any' in Vue.js?

Time:12-27

I have a prop in my vue component which I don't know it's type, but there are other props on the same component with defined type and I don't want to remove their type definition. Is there a way for a vue prop to accepts type any?

here is my code:

props: {
    name: String,
    inputType: String,
    value: Any,
}

I know that I can declare the props like "props: ['name', 'inputType', 'value']" without defining any type for them, but I was wondering if there is a way to define types for 'name' and 'inputType', but let 'value' accept any type?

I'm using nuxt v2.15.7

Related links:

Vue Props

CodePudding user response:

Try to give it an undefined value as mentioned in the following example :

// ignore the following two lines, they just disable warnings in "Run code snippet"
Vue.config.devtools = false;
Vue.config.productionTip = false;


Vue.component('my-component', {
template:`<div>{{name}},{{value}}</div>`,
  props:{
  name:String,
  value:undefined
  }
})
new Vue({
      el: '#app',

      data() {
        return {}
      }

    })
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>


<div id="app" >
 <my-component name="test" value="testtt" />
</div>

  • Related