Components with slots can expose their data by passing it into the slot and exposing the data using slot-scope in the template. This approach allows you to pass props down from Parent components to Child components without coupling them together.

For example, we have two components, Settings and Layout component:

  1. <Settings>
  2. <Layout></Layout>
  3. </Seetings>

We want Layout get data from Settings component, we can do it with slot-scope

Seetings.vue:

  1. <template>
  2. <div>
  3. <slot :header=header :footer=footer></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import {Component, Vue} from 'vue-property-decorator'
  8.  
  9. export default class Settings extends Vue {
  10. header = 'This is data for header'
  11. footer = 'This is the data form footer'
  12. }
  13. </script>

Actually this is the same as Renderless component:

  1. // Renderless component
  2. <script>
  3. export default {
  4. render() {
  5. return this.$scopedSlot.default({header: 'xxx', footer: 'xxx'})
  6. }
  7. }
  8. </script>

Layout.vue:

  1. <template>
  2. <div class="flex flex-col h-screen">
  3. <slot name="header">
  4. <h1 class="text-red">Please add a header!</h1>
  5. </slot>
  6. <slot name="content">
  7. <div class="text-red flex-grow">Please add some content</div>
  8. </slot>
  9. <slot name="footer">
  10. <h2 class="text-orange">Please add a footer!</h2>
  11. </slot>
  12. </div>
  13. </template>

HelloWorld.vue:

  1. <template>
  2. <Settings >
  3. <Layout slot-scope="props">
  4. <header slot='header' class='p-2 bg-blue text-white'>{{props.header}}</header>
  5. <div slot="content" class="flex-grow p-3">Amazing content</div>
  6. <h2 slot="footer" class="bg-grey-light text-blue p-2 text-center">{{props.footer}}</h2>
  7. </Layout>
  8. </Settings>
  9. </template>
  10.  
  11. <script>
  12.  
  13. import {Component, Prop, Vue} from 'vue-property-decorator'
  14. import Layout from './Layout';
  15. import Settings from './Settings';
  16.  
  17. @Component({
  18. components: {
  19. Layout,
  20. Settings
  21. }
  22. })
  23. export default class HelloWorld extends Vue {
  24. @Prop({
  25. default: 'Default message from Hello World Component'
  26. })
  27. message
  28.  
  29. onClick() {
  30. this.message = 'Goodbye'
  31. }
  32. }
  33. </script>

The concept is a bit similar to Angular ngTemplateOutlet

[Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component的更多相关文章

  1. [Vue @Component] Pass Props to Vue Functional Templates

    Functional templates allow you to create components consisting of only the template tag and exposing ...

  2. [Vue @Component] Place Content in Components with Vue Slots

    Vue's slots enable you to define where content of a component should land when you define the conten ...

  3. Vue组件之props,$emit与$on以及slot分发

    组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式  props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...

  4. [Vue @Component] Define Props on a Vue Class with vue-property-decorator

    While traditional Vue components require a data function which returns an object and a method object ...

  5. vue报错[Vue warn]: Unknown custom element: <router-Link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    vue浏览器报错,如下 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <router-Link> - di ...

  6. 使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the "name" option.(未注册组件)的原因之一

    错误信息: [Vue warn]: Unknown custom element: <list> - did you register the component correctly? F ...

  7. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

  8. Vue报错之" [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component correctly? For recursive components, make sure to provide the "name" option."

    一.报错截图 [Vue warn]: Unknown custom element: <wzwzihello> - did you register the component corre ...

  9. Vue组件选项props

    前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props ...

随机推荐

  1. can't set blob value on that column

    MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...

  2. Spark学习之编程进阶——累加器与广播(5)

    Spark学习之编程进阶--累加器与广播(5) 1. Spark中两种类型的共享变量:累加器(accumulator)与广播变量(broadcast variable).累加器对信息进行聚合,而广播变 ...

  3. 解决opencv在pycharm中无代码自动提示的bug

    2018-03-0422:19:39 首先,估计这不是bug 可能是我自己误操作导致的,但是让我搞了好久才搞定,实在是苦恼 如图已实现功能,百度里有很多朋友出现了,这个无代码提示的问题 大概是这样的, ...

  4. 学习RFT之:TestObject.find方法的了解与使用

    第一部分:了解TestObject.find 一.TestObject.find方法的作用 1.测试过程中动态的找到测试对象(控件.标签等),使我们的测试用例不再依赖RFT自带的对象地图(Object ...

  5. 联想 Z5 Pro(L78031)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖ZUI 10.0.355

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  6. vscode常好用的插件以及几个快捷操作

    使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...

  7. SpringBoot中如何使用jpa和jpa的相关知识总结

    jpa常用的注解: 注解 解释 @Entity 声明类为实体或表. @Table 声明表名. @Basic 指定非约束明确的各个字段. @Embedded 指定类或它的值是一个可嵌入的类的实例的实体的 ...

  8. 10Java Server Pages 隐式对象

    Java Server Pages 隐式对象 JSP隐式对象是Web容器加载的一组类的实例,它不像一般的Java对象那样用“new”去获取实例,而是可以直接在JSP页面使用的对象.JSP提供的隐式对象 ...

  9. 反片语(Ananagrams,Uva 156)

    输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文 本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中 的大小写,按字典序进行排列(所有大写字母 ...

  10. [Algorithm] 11. Linked List Cycle

    Description Given a linked list, determine if it has a cycle in it. To represent a cycle in the give ...