Functional templates allow you to create components consisting of only the template tag and exposing the props passed into the template with the props object off of the template context. This approach allows you to build simple configurable templates without having to write any backing code.

From the code in previous post:

<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
}
})

We create two functional template component 'Header' and 'Footer':

<!-- components/Heade.vuer -->

<template functional>
<header slot='header' class='p-2 bg-blue text-white'>{{props.header}}</header>
</template>
<!-- components/Footer.vue -->

<template functional>
<h2 slot="footer" class="bg-grey-light text-blue p-2 text-center">{{props.footer}}</h2>
</template>

Functional template works pretty much like React functional component:

const header = props => <header>{{props.header}}</header>

Just in Vue, you just need to add 'functional' directive to the <template>, don't need to add any js code.

exports those componets in components/index.js file:

export { default as Header } from "./Header"
export { default as Footer } from "./Footer"

Using those component to refactor the code:

<template>
<Settings >
<Layout slot-scope="{header, footer}">
<Header :header="header"></Header>
<div slot="content" class="flex-grow p-3">Amazing content</div>
<Footer :footer="footer"></Footer>
</Layout>
</Settings>
</template> <script> import {Component, Prop, Vue} from 'vue-property-decorator'
import Layout from './Layout';
import Settings from './Settings'; import {Header, Footer} from './components'; @Component({
components: {
Layout,
Settings,
Header,
Footer
}
})

[Vue @Component] Pass Props to Vue Functional Templates的更多相关文章

  1. [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  ...

  2. [Vue @Component] Pass Vue Render Functions as Props for Powerful Patterns

    Render functions open up a world of customization and control by using pure JavaScript rather than V ...

  3. [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 ...

  4. Vue组件选项props

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

  5. vue.js实战——props数据验证(自定义构造器检测)

    Vue.component('my-component',{ props:{ //必须是数字类型 propA:Number, //必须是字符串或数字类型 propB:[String,Number], ...

  6. vue.js实战——props单向数据流

    Vue2.x通过props传递数据是单向的了,也就是父组件数据变化时会传递给子组件,但是反过来不行. 业务中会经常遇到两种需要改变prop的情况, 一种是父组件传递初始值进来,子组件将它作为初始值保存 ...

  7. 【转存】Vue组件选项props

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

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

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

  9. element-ui + el-dialog + Vue.component 注册的富文本控件 第二次及以后打开dialog出现问题解决方法

    自定控件 添加属性  v-if="dialogVisible" <el-dialog title="" :visible.sync="dialo ...

随机推荐

  1. (2)左右值初探与auto类型说明符

    这篇文章的起因是下面这两段代码,出自<C++ primer 5th>中文版P62页: auto &h =42;//错误,不能为非常量引用绑定字面值 const auto & ...

  2. mongo 3.4分片集群系列之一:浅谈分片集群

    这篇为理论篇,稍后会有实践篇. 这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mong ...

  3. C# HttpWebRequest Post Get 请求数据

    Post请求 1 //data 2 string cookieStr = "Cookie信息"; 3 string postData = string.Format("u ...

  4. CAD得到ImageMark数据(com接口VB语言)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. 牛客多校Round 5

    Solved:3 rank:195 F. take 官方题解:小 A 在打开第 i 个箱子后会交换手中的钻石和第 i 个箱子中的钻石 当且仅当第 i个箱子的钻石是前 i 个箱子打开后出现的所有钻石里最 ...

  6. (C/C++学习)14.C语言字符串处理函数(二)

    说明:上节着重解释了字符串处理的库函数处理,这节将针对一些常用的需求,进行非库函数的处理. 一.去除某一个字符串中的某个字符 1.去除字符串右边的空格 void trimStrRightSpace(c ...

  7. [Luogu] P3701 「伪模板」主席树

    题目背景 byx和手气君都非常都非常喜欢种树.有一天,他们得到了两颗奇怪的树种,于是各自取了一颗回家种树,并约定几年后比一比谁种出来的树更加牛x. 题目描述 很快,这棵树就开花结果了.byx和手气君惊 ...

  8. 【IDEA】【Git】pull代码始终无法pull到最新的代码或者提示pull no items 【解决方式】

    最近pull代码老是提示pull no items,但是本地并不是最新的代码,看了各种博客始终无法解决,最后靠自己的方式解决.下面是解决方法. 方法:1.首先git --> repository ...

  9. Django-Rest framework中文翻译-Request

    REST framework的Request类扩展自标准的HttpRequest,增加了REST framework灵活的请求解析和请求验证支持. 请求解析 REST framework的Reques ...

  10. Wind rotor states

    test test Table of Contents 1. Wind rotor states 1.1. Turbulent Wake State 1.2. Vortex Ring State 1. ...