有没有办法在 Vue.js 2 中为道具定义“任何”类型?
Is there a way to define `any` type for a prop in in Vue.js 2?
我的 vue 组件中有一个道具,我不知道它的类型,但同一组件上还有其他具有定义类型的道具,我不想删除它们的类型定义。有没有办法让 vue 道具接受任何类型?
这是我的代码:
props: {
name: String,
inputType: String,
value: Any,
}
我知道我可以声明像“props: ['name', 'inputType', 'value']”这样的道具而不为它们定义任何类型,但我想知道是否有是为 'name' 和 'inputType' 定义类型的方法,但让 'value' 接受任何类型?
我正在使用 nuxt v2.15.7
相关链接:
尝试给它一个 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>
我的 vue 组件中有一个道具,我不知道它的类型,但同一组件上还有其他具有定义类型的道具,我不想删除它们的类型定义。有没有办法让 vue 道具接受任何类型?
这是我的代码:
props: {
name: String,
inputType: String,
value: Any,
}
我知道我可以声明像“props: ['name', 'inputType', 'value']”这样的道具而不为它们定义任何类型,但我想知道是否有是为 'name' 和 'inputType' 定义类型的方法,但让 'value' 接受任何类型?
我正在使用 nuxt v2.15.7
相关链接:
尝试给它一个 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>