【问题标题】:Vue component define some of the props as data type, leave others as-isVue 组件将一些 props 定义为数据类型,其他保持原样
【发布时间】:2021-03-23 15:05:44
【问题描述】:

我真的很喜欢 Vue 组件中的 props: ['title', 'someInt', 'description'] 语法。我如何只需要这些道具中的 1 个,即 someInt,作为整数?但不必显式定义其他道具的数据类型(它们可以保持默认为字符串)。

我的父组件正在执行<my-component title='some title' some-int='100' description='desc'></my-component>,后来当我执行this.someInt 时,它的数据类型是字符串而不是整数(数字)。谢谢!

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    如果您更新了<my-component 以将您想要的属性绑定为数字,则可以保留props: ['title', 'someInt', 'description'] 语法。示例:

    <my-component title='some title' :some-int='100' description='desc'></my-component> - 这将使someInt 成为一个数字,因为您通过: 字符绑定它。 Vue 按字面意思解析表达式。

    【讨论】:

    • 很好的问题和很好的解决方案。您是否考虑过使用slots
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 2020-12-19
    • 2018-09-18
    • 1970-01-01
    • 2023-03-21
    • 2019-04-03
    • 2019-01-16
    • 2020-08-30
    相关资源
    最近更新 更多