【问题标题】:Vue2 component as <vue-component>value</vue-component>Vue2 组件作为 <vue-component>value</vue-component>
【发布时间】:2019-07-29 21:04:43
【问题描述】:

组件标签之间可以给组件传值吗?

示例:在一个格式化货币价值的组件中,我创建为:

<vue-component value="50" />

然后显示

$ 50,00

现在,出于好奇,这是一种调用组件的方式,如下所示:

    <vue-component>50</vue-component>

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

了解插槽:vuejs.org/v2/guide/components-slots.html

Fe:&lt;template&gt;&lt;h1&gt;&lt;slot/&gt;&lt;/h1&gt;&lt;/template&gt;

您可以通过以下方式访问 ita 值:this.$slots.default[0].text

它是可能的事实并不意味着你应该这样做。事实上,恰恰相反。 Props 具有多种优势(例如标记为必需、默认值、验证器等等),但首先它们是将数据传递给每个 vuejs 开发人员都会理解的子组件的标准化方式。

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 2021-07-03
    • 1970-01-01
    • 2020-03-10
    • 2023-03-30
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多