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. Spring Boot (29) 定时任务

    使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...

  2. mysql若干问题

    一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...

  3. Set,Map与Array,Object对比

    Map与Array 数据结构横向对比,用Map和Array分别实现最基本的增删改查: //增 { let theMap=new Map(); let theArray=[]; theMap.set(' ...

  4. mongo 3.4分片集群系列之七:配置数据库管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  5. MySQL详解(18)-----------分页方法总结

    ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...

  6. Android(java)学习笔记205:JNI之编写jni程序适配所有处理器型号

    1. 还是以"02_两个数相加"为例,你会发现这个jni程序只能在ARM处理器下运行,如下:  如果我们让上面的程序运行在x86模拟器上,处理平台不对应,报如下错误: 03-29 ...

  7. 音视频】5.ffmpeg命令分类与使用

    GT其实平时也有一些处理音视频的个人或者亲人需求,熟练使用ffmpeg之后也不要借助图示化软件,一个命令基本可以搞定 G: 熟练使用ffmpeg命令!T :不要死记硬背,看一遍,自己找下规律,敲一遍, ...

  8. 并发编程学习笔记(5)----AbstractQueuedSynchronizer(AQS)原理及使用

    (一)什么是AQS? 阅读java文档可以知道,AbstractQueuedSynchronizer是实现依赖于先进先出 (FIFO) 等待队列的阻塞锁和相关同步器(信号量.事件,等等)提供一个框架, ...

  9. CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)

    由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

  10. The following packages have unmet dependencies:

    root@ubuntu:~# apt-get install open-iscsiReading package lists... DoneBuilding dependency treeReadin ...