[Vue @Component] Pass Props to Vue Functional Templates
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的更多相关文章
- [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 ...
- [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 ...
- [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组件选项props
前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props ...
- vue.js实战——props数据验证(自定义构造器检测)
Vue.component('my-component',{ props:{ //必须是数字类型 propA:Number, //必须是字符串或数字类型 propB:[String,Number], ...
- vue.js实战——props单向数据流
Vue2.x通过props传递数据是单向的了,也就是父组件数据变化时会传递给子组件,但是反过来不行. 业务中会经常遇到两种需要改变prop的情况, 一种是父组件传递初始值进来,子组件将它作为初始值保存 ...
- 【转存】Vue组件选项props
原帖地址 前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 ...
- Vue组件之props,$emit与$on以及slot分发
组件实例之间的作用域是孤立存在,要让它们之间的信息互通,就必须采用组件的通信方式 props用于父组件向子组件传达信息 1.静态方式 eg: <body> <div id=&quo ...
- element-ui + el-dialog + Vue.component 注册的富文本控件 第二次及以后打开dialog出现问题解决方法
自定控件 添加属性 v-if="dialogVisible" <el-dialog title="" :visible.sync="dialo ...
随机推荐
- 学习笔记 第五章 使用CSS美化网页文本
第五章 使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...
- Python学习日记之练习代码
# -*- coding:utf-8 -*- number = 23 test=True while test: guess=int(raw_input('输入数字')) if guess==numb ...
- 常用css属性总结
边框修饰:border------>top,bottom,left,right上下左右边框 分为:color,类型style{ groove,dashed,ridge,solid}一个值---- ...
- 没搞错吧,我只是个web前端工程师,不是manager,也不是leader...
那个时候,我只想好好的学习web前端技术,恨不得把有限的时间和精力都放在提升技术上. 然而,让自己在坑里茁壮成长,要先适应坑内的环境. 首当其冲我们要弄明白的事情有: 团队成员的技术能力和状态 Lea ...
- JavaScipt30(第六个案例)(主要知识点:给数字加千分号的正则)
承接上文,这是第6个案例: 附上项目链接: https://github.com/wesbos/JavaScript30 这个主要是要实现在给定的json里匹配出搜索框里的city or state, ...
- [Linux]正则表达式和grep使用【转载】
[Linux]正则表达式和grep使用 2018年12月05日 23:45:54 祥知道 阅读数 78 标签: 正则表达式grepLinuxegrep 更多 个人分类: Linux 所属专栏: Li ...
- list查询棚舍面积的时候,所有棚舍面积的value都是一样的
解决办法 将pickingid在查list之前set到对象中,通过id来匹配查询 具体代码: FarmHouse farmHouse=new FarmHouse(); farmHouse.setPic ...
- Getting start with dbus in systemd (03) - sd-bus.h 使用例子 (systemd version>=221)
sd-bus.h 例子 注意: sd-dbus 是systemd提供的lib,但是这个lib,只有在systemd>v221版本后才可以使用,centos 219版本太低,所以不能使用. 参考: ...
- 数塔(hdoj 2084,动态规划递推)
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目 ...
- World Cup(The 2016 ACM-ICPC Asia China-Final Contest dfs搜索)
题目: Here is World Cup again, the top 32 teams come together to fight for the World Champion. The tea ...