[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 example, we have two components, Settings and Layout component:

<Settings>
    <Layout></Layout>
</Seetings>

We want Layout get data from Settings component, we can do it with slot-scope

Seetings.vue:

<template>
    <div>
        <slot :header=header :footer=footer></slot>
    </div>
</template>
<script>
import {Component, Vue} from ‘vue-property-decorator‘

export default class Settings extends Vue {
    header = ‘This is data for header‘
    footer = ‘This is the data form footer‘
}
</script>

Layout.vue:

<template>
    <div class="flex flex-col h-screen">
    <slot name="header">
      <h1 class="text-red">Please add a header!</h1>
    </slot>
    <slot name="content">
      <div class="text-red flex-grow">Please add some content</div>
    </slot>
    <slot name="footer">
      <h2 class="text-orange">Please add a footer!</h2>
    </slot>
    </div>
</template>

HelloWorld.vue:

<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
  }
})
export default class HelloWorld extends Vue {
  @Prop({
    default: ‘Default message from Hello World Component‘
  })
  message

  onClick() {
    this.message = ‘Goodbye‘
  }
}
</script>

The concept is a bit similar to Angular ngTemplateOutlet

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

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

[Vue @Component] Pass Props Between Components with Vue Slot Scope的相关文章

[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

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 @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 @Component] Simplify Vue Components with vue-class-component

While traditional Vue components require a data function which returns an object and a method object with your handlers, vue-class-componentflattens component development by allowing you to add your data properties and handlers directly as properties

使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the &quot;name&quot; option.(未注册组件)的原因

错误信息: [Vue warn]: Unknown custom element: <list> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in <Root>) 在浏览器点击开错误的详细信息: 总结: 原文地址:https://www.cnblogs.com/oukele/

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,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.mixin Vue.extend(Vue.component)的原理与区别

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