[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
  }
})
复制代码

 

posted @   Zhentiw  阅读(459)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-07-24 [Nuxt] Navigate with nuxt-link and Customize isClient Behavior in Nuxt and Vue.js
2017-07-24 [Nuxt] Load Data from APIs with Nuxt and Vuex
2017-07-24 [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
2017-07-24 [Nuxt] Add CSS Libraries to Nuxt
2017-07-24 [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js
2017-07-24 [Nuxt] Setup a "Hello World" Server-Rendered Vue.js Application with the Vue-CLI and Nuxt
2017-07-24 [TypeScript] Define a function type
点击右上角即可分享
微信分享提示