Vue中的slot(占坑,预留位置)

  1. 子模板不使用slot
  2. 子模板使用slot
  3. 子模板使用使用name属性,且传递data
  • 文件名:Slots.vue

//slot组件

<template>

<div class="Slots">

<div class="myd">

在子组件中不使用slot时SlotChildwithnoslots下的内容不会显示

<SlotChildwithnoslots>

<div class="no-name">111111111111111111我是嵌在子组件内不具有属性名的标签</div>

<div >2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>

</SlotChildwithnoslots>

</div>

<div class="myd">‘

在子组件中使用slot时SlotChildwithslots下的内容会显示

<SlotChildwithslots>

<div class="no-name">111111111111111111我是嵌在子组件内不具有属性名的标签</div>

<div >2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>

</SlotChildwithslots>

</div>

‘.

<div class="myd">‘

在子组件中使用slot时且包含数据时下的内容会显示

<SlotChildwithslotsanddata>

<div slot="header">111111111111111111我是嵌在子组件内不具有属性名的标签</div>

<div slot="header">2222222222222222222222222222我是嵌在子组件内不具有属性名的标签</div>

<template slot-scope="user">

<div v-for="item in user.data" :key="item.id">

{{item}}

</div>

</template>

</SlotChildwithslotsanddata>

</div>

</div>

</template>

<script>

import SlotChildwithnoslots from ‘@/components/SlotChildwithnoslots‘

import SlotChildwithslots from ‘@/components/SlotChildwithslots‘

import SlotChildwithslotsanddata from ‘@/components/SlotChildwithslotsanddata‘

export default {

name: ‘Slots‘,

components:{

SlotChildwithnoslots,

SlotChildwithslots,

SlotChildwithslotsanddata

},

data () {

return {

}

}

}

</script>

<style>

.myd

{

background-color:yellow;

height: 300px;

border:1px solid red;

}

</style>

  • 文件名:SlotChildwithslots.vue
//slot的子组件
<template>
  <div class="SlotChildwithslots">
      <slot></slot>
      我是slot的子组件
  </div>
</template>

<script>
export default {
  name: ‘SlotChildwithslots‘,
  data () {
    return {

    }
  }
}
</script>
  • 文件名:SlotChildwithnoslots.vue
//slot的子组件
<template>
  <div class="SlotChildwithnoslots">
      我是slot的子组件
  </div>
</template>

<script>
export default {
  name: ‘SlotChildwithnoslots‘,
  data () {
    return {

    }
  }
}
</script>
  • 文件名:SlotChildwithslotsanddata.vue
//slot的子组件
<template>
  <div class="SlotChildwithslotsanddata">
      <slot name="header" ></slot>

      我是slot的子组件
       <slot :data="user"></slot>
  </div>
</template>

<script>
export default {
  name: ‘SlotChildwithslotsanddata‘,
  data () {
    return {
      user: [
        {name: ‘Jack‘, sex: ‘boy‘},
        {name: ‘Jone‘, sex: ‘girl‘},
        {name: ‘Tom‘, sex: ‘boy‘}
      ]
    }
  }
}
</script>

原文地址:https://www.cnblogs.com/li9club/p/11522087.html

时间: 2024-07-31 03:24:43

Vue中的slot(占坑,预留位置)的相关文章

vue中的slot理解和使用

最近被vue 搞得一塌糊涂,理解的比较慢,工作进度进度要求太快,需求理解不明,造成了很大的压力. 在理解Vue中的Slot的时候看了网上的相关内容,看了半天没看到明白说的是什么,然后自己就安装了vue的相关环境,创建了一个项目,实际动手看看是什么东西, 现理解为: 用父组件的内容去替换掉子组件的内容: 根据父组件中的 <div slot="slot1">slottest</div> 如果引入的子组件中有 <slot name="slot1&quo

vue中的slot与slot-scope

深入理解vue中的slot与slot-scope vue+element-ui+slot-scope或原生实现可编辑表格 原文地址:https://www.cnblogs.com/knuzy/p/9485951.html

深入理解vue中的slot与slot-scope

<template> <div class="father"> <h3>这里是父组件</h3> <child> <div class="tmpl"> <span>菜单1</span> <span>菜单2</span> <span>菜单3</span> <span>菜单4</span> <span

Vue中插槽slot的使用

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

在Vue中遇到的各种坑 及性能提升

Vue: (1)    没有再模板里引用data数据,会不会引起update.beforeUpdate生命周期函数的执行? 不会 (2)组件改成异步 (3)v-once (4)如果不用template属性,直接在页面上写组件名,有些浏览器会把这些非法命名的组件解析错误,这是需要在标签内写正常的标签名,写is属性 is='组件名' 提高性能: 如果不需要响应式,直接在Vue实例里挂载一个属性就可以 例如: This.y=2 这样修改数据,操作是异步的,是为了提高性能,所以数据更新之后的渲染dom是

vue中的锚链接跳转问题

vue中的锚链接跳转问题 在vue中的锚链接和普通的html不同,关于vue中的锚链接可以参考vue 中的  scrollBehavior 滚动行为 在router.js中 //创建 router 实例 const router = new VueRouter({ routes, mode: 'history', scrollBehavior(to, from, savedPosition) { if (to.hash) { return { selector: to.hash } } } })

better-scroll在vue中的坑

在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了,以滴滴为例,可以是这样竖向滚动的列表,如图所示: 也可以是横向滚动的导航栏,如图所示: 可以打开"微信 -> 钱包->滴滴出行"体验效果. 我们在实现这类滚动功能的时候,会用到我写的第三方库,better-scroll. 什么是 better-scroll better-scroll 是一个移动端滚动的解决方案,它是基于 iscroll 的重写,它和 iscroll 的主要区别在这里.better-scroll

vue中的坑 --- 锚点与查询字符

在vue中,由于是单页面SPA,所以需要使用锚点来定位,在vue的官方文档中提到过也可以不使用锚点的情况,就是在vue-router中使用history模式,这样,在url中就不会出现丑陋的#了,但是这样的缺点在于不能再页面中再使用自己设定的锚点(利用href.name或id)并且需要后台的支持,所以一般我们使用带#的形式就可以了. 而在这之中一种比较特殊的情况在于,如果查询字符串和vue中的锚点同时出现,那么他们的位置关系是怎样的问题, 通过测试可以发现,vue自身的锚点在查询字符串之前或者在

vue中使用keepAlive组件缓存遇到的坑

项目开发中在用户由分类页category进入detail需保存用户状态,查阅了Vue官网后,发现vue2.0提供了一个keep-alive组件. 上一篇讲了keep-alive的基本用法,现在说说遇到的坑. 先说项目中的配置 在App.vue中的设置 在router中增加配置meta 上面这个设置后发现问题了,从category进入detail页后,状态被保存了,返回的时候保存了用户状态,达到了预期效果 但问题在于但从category返回到index后,再由index进入category时依然显