doubleyong
管理员
管理员
  • 最后登录2026-05-10
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:5728回复:0

[vue]vue通过 slot-scope 在父组件接收来自子组件的数据

楼主#
更多 发布于:2019-07-05 21:56
slot-scope,跟官网一样,我个人觉得slot-scope就是作用域插槽。并且他是接收子组件传来的数据。
举个栗子:
  • 作用域插槽

父组件:
<template>
    <div class="father">
    <h3>这里是父组件</h3>
    <slot-scope-child>
        <template slot-scope="user">
            <ul>
                <li v-for="(item, index) in user.data" :key="index">pw_item</li>
            </ul>
        </template>
    </slot-scope-child>
  </div>
</template>
<script>
import SlotScopeChild from '@/common/SloteScopeChild'
export default {
    components: {
        SlotScopeChild
    }
}
</script>
<style scoped>
</style>
子组件:

<template>
  <div class="child">
    <h3>这里是子组件</h3>
    <slot :data="data"></slot>
  </div>
</template>
<script>
export default {
    data: function(){
      return {
        data: ['空空如也','悟空','成都','七月上','病变','走马']
      }
    }
}
</script>
然后你就会发现,子组件里面的数据会展示出来。这也就是slot-scope。就是带着数据跑。数据在子组件,然后绑到父组件,父组件就可以使用了。这个在elementUI中展示的很好。


参考:https://www.cnblogs.com/-wch/p/9766741.html
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号