【问题标题】:Passing props from page layout through parent slot into child component将页面布局中的道具通过父插槽传递到子组件
【发布时间】:2018-07-28 13:31:45
【问题描述】:

我有一个Card 组件,我想在通过<Cards></Cards> 组件的插槽后,从父布局中定义的<Card /> 标记的title 属性继承title 值。

卡片:

<template>
  <div class="card">
    {{ title }}
  </div>
</template>

<script lang="ts">
import Component from 'nuxt-class-component'
import Vue from 'vue'
import { Prop } from 'vue-property-decorator'

@Component
export default class extends Vue {
  @Prop() title: string
}
</script>

卡片:

<template>
  <div class="cards">
    <slot></slot>
  </div>
</template>

页面:

<template>
  <Cards>
    <Card :title="ABC" />
  </Cards>
</template>

我不太确定如何引用 title 属性。这是用 Vue 怎么写的?

【问题讨论】:

    标签: vue.js vue-component nuxt.js


    【解决方案1】:

    您应该可以通过在Card 组件中定义title 属性来做到这一点:

    卡片:

    <template>
      <div class="card">
        {{ title }}
      </div>
    </template>
    
    <script>
    export default {
      props: ['title']
    }
    </script>
    

    【讨论】:

    • 嗯,我确实在Card 组件中定义了一个title 属性,尽管我可能用vue-property-decorator 错误地定义了它。 (更新问题以显示此打字稿代码。)编辑:知道了。我不得不用&lt;Card title="ABC" /&gt;替换&lt;Card :title="ABC" /&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多