vue slot使用

定义一个父组件

1.如果指定了 slot="slotA"

<template>
  <div class="home">
    我是父组件
    <Child>
      <p slot="slotA">hello world</p>
    </Child>
  </div>
</template>
<template>
  <div class="child">
    <p>我是子组件</p>
    <slot name="slotA">slot1</slot>
  </div>
</template>

2.如果没有指定slot,页面一样会显示

<template>
  <div class="home">
    我是父组件
    <Child>
      <p>hello world</p>
    </Child>
  </div>
</template>
<template>
  <div class="child">
    <p>我是子组件</p>
    <slot>slot1</slot>
  </div>
</template>

如果存在多个slot用第一种方法 指定name

父组件如果没有指定name,子组件却定义了name,是找不到对应的slot

原文地址:https://www.cnblogs.com/zhangYaRan/p/11075678.html

时间: 2024-08-30 18:03:24

vue slot使用的相关文章

vue slot

一般我发现slot都是用在子组件 不知道对不对,不对的请留言指教 ,谢谢谢谢 使用slot场景一: 子组件Minput.vue <input type='text'/> 父组件 Minput <Minput>可以显示吗</Minput> 这种情况下  Minput标签内的文字是不会渲染出来的 如果现在想在里面把文字渲染出来怎么办 好 用slot 子组件 <input type='text'/> <slot></slot> 这样的话,父

[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 slot插槽的使用方法

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://unpkg.com/[email protected]/dist/vue.js"></script> </head> <

vue slot介绍

slot(插槽)属性是vue中比较常用的功能,主要分为:匿名插槽,具名插槽,作用域插槽.下面分别简单介绍下 一.匿名插槽 child.vue: <div> <h3>标题</h3> <slot></slot> </div> parent.vue: <child> <p>插槽内容</p> </child> 渲染结果: <div> <h3>标题</h3> &

vue slot的使用

slot的说明就看vue的官方文档 第一次使用slot  练习的一个小例子 最先编码的时候没有做到上下收缩,只能是列表式的,如下图 代码: <p class="title" style="text-align:left;font-weight:bold">特有属性</p> <div class="param-gap"> <label class="left" for="&qu

Vue slot 插槽

Vue  的插槽感觉上是一个组件函数,和函数一样进行参数的传递来改变组件的据题内容. 使用指令:v-slot //插口的基本使用 <body> <div id="app"> <!-- 传入元素参数 --> <login><span>demo</span></login> </div> </body> <template id="login"> &l

vue Slot理解

简介 插槽:简单理解就是组件内部留一个或多个的插槽位置,可供组件传对应的模板代码进去.插槽的出现,让组件变的更加灵活. 一.匿名插槽 // 组件(父) <my-component> <p>hello,world!</p> </my-component> // 组件内部(子) <div class="child-page"> <h1>子页面</h1> <slot></slot> /

vue自学入门-4(vue slot)

好长时间没有用vue了,从新安装vue脚手架. 1.从新安装webpack cnpm install --save-dev webpack 2.vue init webpack my-project-slot 3.进入目录 cnpm install 4.cnmp run dev 启动成功 5.router-view 部分会被替换成HelloWorld.vue内容 6.修改helloworld.vue内容如下 <template> <div class="hello"&

436 vue slot:插槽基本使用,具名插槽,作用域插槽

1. 插槽 : 替换内容 / 分发内容 (1)占位,像出口<router-view></router-view>. (2)没有新的内容放进来,就用默认的. (3)<slot></slot>将被替换成组件内的对应子节点. 2. 基本使用 <el-car> <div>宝马发动机</div> </el-car> 组件的内部 02-插槽的基本使用.html <!DOCTYPE html> <html

vue slot slot-scope

https://segmentfault.com/a/1190000012996217 插槽,也就是slot,是组件的一块HTML模板,这块模板显示不显示.以及怎样显示由父组件来决定. 实际上,一个slot最核心的两个问题这里就点出来了,是显示不显示和怎样显示. 由于插槽是一块模板,所以,对于任何一个组件,从模板种类的角度来分,其实都可以分为非插槽模板和插槽模板两大类.非插槽模板指的是html模板,指的是‘div.span.ul.table’这些,非插槽模板的显示与隐藏以及怎样显示由插件自身控制