[Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component
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的更多相关文章
- [Vue @Component] Pass Props to Vue Functional Templates
Functional templates allow you to create components consisting of only the template tag and exposing ...
- [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 ...
- Vue组件之props,$emit与$on以及slot分发
组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式 props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...
- [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 ...
- 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 ...
- 使用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 ...
- [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 ...
- 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 ...
- Vue组件选项props
前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props ...
随机推荐
- java简单打印金字塔(案例)
学习编程语言,打印简单的金字塔是基础的案例,在这里就简单的写了个案例,希望能帮助到各位 代码 效果 class kinTa{ public static void main(String[] ...
- CF864D Make a Permutation!
思路: 贪心,构造,模拟. 实现: #include <bits/stdc++.h> using namespace std; ], a[], vis[], n; int main() { ...
- div常用效果方法-transform
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- CSS 潜藏着的BFC
在写样式时,往往是添加了一个样式,又或者是修改了某个属性,就达到了我们的预期. 而BFC就潜藏在其中,当你修改样式时,一不小心就能触发它而毫无察觉,因此没有意识到BFC的神奇之处. 什么是BFC(Bl ...
- 图片选择器ImageEditContainer
1. 简介 本次demo中一共封装了两个组件:ImageEditButton 和 ImageEditContainer.其中ImageEditContainer 是在 ImageEditButton, ...
- 字符编码方式ASCII、Unicode、UTF-8
一.ASCII 1.介绍 即American Standard Code for Information Interchange(美国信息交换标准代码),是基于拉丁字母的,主要用于显示现代英语和其他西 ...
- android framework 图解分享
android framework 图解 个人网站:http://www.51pansou.com Android视频下载:Android视频 Android源码下载:Android源码 Androi ...
- POJ_1050_(dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48232 Accepted: 25534 Desc ...
- POJ_2387_最短路
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46859 Accepted ...
- codeforces_305C_STLset
C. Ivan and Powers of Two time limit per test 0.5 seconds memory limit per test 256 megabytes input ...