首页 > 其他分享 >vue slot的一点理解

vue slot的一点理解

时间:2022-11-08 17:24:16浏览次数:36  
标签:slot vue 这里 插槽 理解 组件 默认值 options

先来一个实例,方便解释:
子组件:

<template>
  <div>
    <button></button>
    <slot>这里是默认值</slot>

    <slot name="one">这里是默认值one</slot>

    <slot name="two">这里是默认值two</slot>

    <slot name="three">这里是默认值three</slot>

    <slot name="dongdong" :value1="child1">这里是默认值1</slot>
  </div>
</template>

父组件:

<template>
  <div>
    <p>这里是普通插槽,一个组件中只能有一个这种插槽</p>
    <etbutton>{{ favalue }}</etbutton>

    <p>这里是具名插槽,这里的v-slot可以简写为#one</p>
    <etbutton>
      <template>这里是插入到默认插槽的内容</template>
      <template #one>这里是插入到one插槽的内容</template>
      <template v-slot:two>这里是插入到two插槽的内容</template>
      <template v-slot:three>这里是插入到three插槽的内容</template>
    </etbutton>

    <p>这里是作用域插槽</p>
    <!-- 作用域插槽的实现步骤如下:
    1.首先在子组件的的slot上面绑定一个值(:key = value)
    2.然后在父组件中使用v-slot:name=变量,吧slot的值存到这变量中
     -->
    <etbutton>
      <template v-slot:dongdong="slotdongdongval">{{
        slotdongdongval.value1
      }}</template>
    </etbutton>
  </div>
</template>

1.可以看到etbutton组件,相对父组件,他是一个子元素
2.其实,etbutton中的slot,他被当做etbutton组件的子元素
在vue渲染etbutton的时候,发现他不是一个常规的标签,认定他是一个组件之后,就会当做一个组件去解析。
父组件的vnode类似这样:

{

    tag:'div',

    children:[

       {
          tag:'test',
          children:['我是放在组件的 slot 11'] 
       }
    ]
}

当自定义组件渲染时候

if(如果是组件){
1initInternalComponent (vm, options);
}
initRender(vm);

走这里1initInternalComponent,然后吧test组件的插槽节点给了组件的选项的【_renderChildren】

function initInternalComponent(vm, options) {    



// 这个options是全局选项和组件设置选项的合集

    var opts = vm.$options = 

            Object.create(vm.constructor.options);  



    var componentOptions = parentVnode.componentOptions;



    // 传给组件选项_renderChildren

    opts._renderChildren = componentOptions.children;

}

源码我暂时研究不懂,知道有这么回事,不细说了。

注意一个过程:自定义组件,首先是需要实例初始化,然后用组件的模板(我理解就是vue文件吧),
去构建他的渲染函数。
什么是渲染函数:

with(this) {
    return _c('main', [
        "我在子组件里面", 
        _t("default")
    ], 2)
}

image

标签:slot,vue,这里,插槽,理解,组件,默认值,options
From: https://www.cnblogs.com/1998Archer/p/16870416.html

相关文章

  • 快速理解ASP.NET Core的认证与授权
      原文网址:https://blog.csdn.net/helendemeng/article/details/122352963ASP.NETCore的认证与授权已经不是什么新鲜事了,微软官方的文档对于如何在ASP.NETCore中......
  • vue中使用Element
    官网https://element.eleme.cn/#/zh-CN/component/installation安装npm安装elementnpmielement-ui-S快速上手引入Element在main.js中写入以下内容:importVu......
  • 用命令行创建vue项目
    准备打开命令行安装 vue-cilnpminstall-g@vue/cli安装完成就可以使用 vuecreate 创建项目1.基于命令行的方式创建vue项目vuecaeatevue2.基于图形化界面的方......
  • vue export和import
    export点击查看代码//m1.js//分别暴露exportletschool="XX学校";exportfunctionteach(){ console.log("教学");}//m2.js//统一暴露letschool="......
  • vue.js3:div上添加右键菜单([email protected])
    一,js代码:<template><div><divstyle="width:800px;margin:auto;display:flex;flex-direction:column;"><div>请选择上传图片:<inputtype="......
  • Vue3+Taro+NutUI 微信小程序使用canvas组件完成电子签名功能
    使用Taro小程序开发框架中的canvas组件实现电子签名功能,实现签名时屏幕自动横屏,可清空签名重签,可保存导出图片的64位码。请将手机屏幕横向在区域内进行签名重签完......
  • antdv (Ant Design of Vue) 复杂表单验证问题解决方法
    我们知道,在简单的表单中,都是一项一项往下排列的,验证的时候也按照字段一一对把规则写好就能验证,如下图  但是遇到了复杂场景的表单验证,比如一项由多个input、checkbox......
  • VUE2 实现一个页面 调用 另一个页面的函数
    今天我在Vue 需要实现 一个这样的功能在App.vue页面中 要实现 调用 另一个页(ExtensionMonitor.vue)中的函数 并将参数 传过去下面将实现的步骤写下来,供大家参考1.......
  • JeecgBoot 3.4.3-Vue2 版本发布,Vue2版前端UI专项升级
    JeecgBootvue2版前端UI代码更新到3.4.3最新版,兼容最新版的后台(3.4.3、3.4.3-GA)。发版日期:2022-11-08源码下载前端:https://github.com/jeecgboot/ant-design-vue-jee......
  • 写过vue自定义指令吗,原理是什么?.m
    背景看了一些自定义指令的文章,但是探究其原理的文章却不多见,所以我决定水一篇。如何自定义指令?其实关于这个问题官方文档上已经有了很好的示例的,我们先来温故一下。除......