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

原文地址:https://www.cnblogs.com/Answer1215/p/9362569.html

时间: 2024-08-24 23:53:53

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

[Vue @Component] Pass Props Between Components with Vue Slot Scope

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

Vue组件选项props

前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props 向下传递数据给子组件,子组件通过 events 给父组件发送消息.本文将详细介绍Vue组件选项props 静态props 组件实例的作用域是孤立的.这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,需要通过子组件的 props 选项 使用Prop传递数据

前端框架vue.js系列(9):Vue.extend、Vue.component与new Vue

前端框架vue.js系列(9):Vue.extend.Vue.component与new Vue 本文链接:https://blog.csdn.net/zeping891103/article/details/78133622 vue构造.vue组件和vue实例这三个是不同的概念,它们的关系有点类似于Java的继承概念: 关系:vue构造->vue组件->vue实例 也就是说不同的vue组件可以共用同一个vue构造,不同的vue实例可以共用同一个vue组件.在大型项目中,用过java开发的都知

vue.js(17)--vue的组件切换

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con

vue学习:props,scope,slot,ref,is,sync等知识点

1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user-profile ref="profile"></user-profile>(子组件)</div> var parent = new Vue({ el: '#parent' })// 访问子组件var child = parent.$refs.profile p

vue 组件属性props,特性驼峰命名,连接线使用

今天在学习vue的时候碰到了一个有趣的问题 是这样的,先来个话题引入,后面会用到 var myname={ 'first-name':'9', 'last-name':'l o n g' } console.log(myname.first-name); console.log(myname['first-name']); 打印出来是  NaN  9 解释下,之所以没有前面没有打印出来9,是因为程序走的时候,把我们认为的英文连接符当做减号看待,myname.first是undefined,nam

vue学习:props,scope,slot,ref,is,slot,sync等知识点

1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user-profile ref="profile"></user-profile>(子组件)</div> var parent = new Vue({ el: '#parent' })// 访问子组件var child = parent.$refs.profile p

[Vue @Component] Extend Vue Components in TypeScript

This lesson shows how you can extend and reuse logic in Vue components using TypeScript inheritance. It will take you through extending a component, its properties and methods, and how hooks are triggered along the inheritance tree. We can define a P

Vue.mixin Vue.extend(Vue.component)的原理与区别

1.本文将讲述 方法 Vue.extend Vue.mixin 与 new Vue({mixins:[], extend:{}})的区别与原理 先回顾一下 Vue.mixin 官网如下描述: Vue.mixin( mixin )全局注册一个混入,影响注册之后所有创建的每个 Vue 实例.插件作者可以使用混入,向组件注入自定义的行为. 既然可以影响到注册后的所有实例,那么该方法注入的方法和属性都存放在哪里呢(构造函数的options属性上),我们一起来看看该方法的定义 Vue.mixin = fu