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:

<Settings>
<Layout></Layout>
</Seetings>

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

Seetings.vue:

<template>
<div>
<slot :header=header :footer=footer></slot>
</div>
</template>
<script>
import {Component, Vue} from 'vue-property-decorator' export default class Settings extends Vue {
header = 'This is data for header'
footer = 'This is the data form footer'
}
</script>

Actually this is the same as Renderless component:

 // Renderless component
<script>
export default {
render() {
return this.$scopedSlot.default({header: 'xxx', footer: 'xxx'})
}
}
</script>

Layout.vue:

<template>
<div class="flex flex-col h-screen">
<slot name="header">
<h1 class="text-red">Please add a header!</h1>
</slot>
<slot name="content">
<div class="text-red flex-grow">Please add some content</div>
</slot>
<slot name="footer">
<h2 class="text-orange">Please add a footer!</h2>
</slot>
</div>
</template>

HelloWorld.vue:

<template>
<Settings >
<Layout slot-scope="props">
<header slot='header' class='p-2 bg-blue text-white'>{{props.header}}</header>
<div slot="content" class="flex-grow p-3">Amazing content</div>
<h2 slot="footer" class="bg-grey-light text-blue p-2 text-center">{{props.footer}}</h2>
</Layout>
</Settings>
</template> <script> import {Component, Prop, Vue} from 'vue-property-decorator'
import Layout from './Layout';
import Settings from './Settings'; @Component({
components: {
Layout,
Settings
}
})
export default class HelloWorld extends Vue {
@Prop({
default: 'Default message from Hello World Component'
})
message onClick() {
this.message = 'Goodbye'
}
}
</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. Jquery 全选、反选问题的记录

    <div id="list"> <ul id="choseList" > <li><input type=" ...

  2. Winform学习知识汇总

    引用博客 http://www.cnblogs.com/peterzb/archive/2009/06/14/1502918.html

  3. log4j建立propertie后要建立log4j2.xml

    log4j.properties ### \u8BBE\u7F6E### log4j.rootLogger = debug,stdout,D,E ### \u8F93\u51FA\u4FE1\u606 ...

  4. 解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    今天发布web API,调用接口报错了:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31b ...

  5. Concurrent control in SQLite

    This document describes the technologies to concurrent access to a SQLite database. There are also s ...

  6. Exception Information

    https://developer.apple.com/library/content/technotes/tn2004/tn2123.html Exception Information The t ...

  7. 【Linux】Tomcat安装及端口配置

    安装环境 :Linux(CentOS 64位) 安装软件 : apache-tomcat-9.0.20.tar.gz(下载地址http://tomcat.apache.org/) 一:JDK安装配置 ...

  8. Oracle 数据库启动与关闭 各种方式详解整理

    概述 只有具备sysdba和sysoper系统特权的用户才能启动和关闭数据库. 在启动数据库之前应该启动监听程序,否则就不能利用命令方式来管理数据库,包括启动和关闭数据库. 虽然数据库正常运行,但如果 ...

  9. LINUX-SWAP文件系统

    mkswap /dev/hda3 创建一个swap文件系统 swapon /dev/hda3 启用一个新的swap文件系统 swapon /dev/hda2 /dev/hdb3 启用两个swap分区

  10. Linux 复习四

    第四章 shell程序设计I-入门 一.shell脚本的基本概念 shell脚本(script)是一个可执行的纯文本文件,有多个shell命令组成. 命令的执行时从上而下.从左而右的分析和执行 命令. ...