【问题标题】:Is there a way to define `any` type for a prop in in Vue.js 2?有没有办法在 Vue.js 中实现“prop type => any”?
【发布时间】:2021-12-26 07:48:32
【问题描述】:

我的 vue 组件中有一个道具,我不知道它的类型,但同一组件上还有其他道具具有已定义的类型,我不想删除它们的类型定义。有没有办法让 vue 道具接受任何类型?

这是我的代码:

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

我知道我可以声明像“props: ['name', 'inputType', 'value']”这样的道具而不为它们定义任何类型,但我想知道是否有一种方法可以为 ' name' 和 'inputType',但让 'value' 接受任何类型?

我正在使用 nuxt v2.15.7

相关链接:

Vue Props

【问题讨论】:

    标签: vue.js vuejs2 nuxt.js


    【解决方案1】:

    尝试给它一个undefined 值,如以下示例中所述:

    // 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" class="container">
     <my-component name="test" value="testtt" />
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2012-12-18
      • 2020-03-19
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多