美文网首页Vue
99.vue使用具名插槽

99.vue使用具名插槽

作者: wo不是黄蓉 | 来源:发表于2022-04-19 17:40 被阅读0次

DAY20:
定义组件模板(PS:省略了一些布局的样式代码):

<template>
  <div>
    <slot name="form"></slot>
    <!-- <slot name="tableHeader"> -->
    <div class="table-header">
      <div class="header-line"></div>
      <div class="header-left">
        <slot name="tableTitle">列表</slot>
        <span class="header-sub"><slot name="tableSubTitle"></slot></span>
      </div>
      <div class="header-right">
        <div class="header-right-content">
          <slot name="tableOperate"></slot>
        </div>
      </div>
    </div>
    <!-- </slot> -->
    <slot name="table"></slot>
  </div>
</template>
<script>
export default {
  name: 'commonTable',
  data() {
    return {}
  }
}
</script>

注册自定义组件(PS:手动引入):

import CommonTable from '@/modules/components/common-table/index'
//注册为全局组件
Vue.component('common-table', CommonTable )

注册自定义组件(PS:自动引入):

import Vue from 'vue'
//加载component下的所有组件
const requireComponent = require.context(
  // 其组件目录的相对路径
  '../components',
  // 是否查询其子目录
  true,
  // 匹配vue后缀文件名的文件
  /.vue$/
)
requireComponent.keys().forEach((fileName) => {
  let name = fileName.split('/')
//组件目录使用-连接方式命名的
  name = name[name.length - 1].split('.')[0]
  const componentConfig = requireComponent(fileName)
  const VM = componentConfig.default || componentConfig
//组件必须声明name属性,否则无法读取到文件名
  Vue.component(VM.name || name, VM)
})

main.js中引入自定义组件

import '@/modules/components' // 自定义组件

使用上面定义common-table组件,使用form和table插槽(PS:当然也可以定义操作栏参照相应格式即可):

<template>
  <div class="page-box proxy-setup">
    <common-table>
      <template slot="form">
        <el-form :model="formData" label-width="90px">
        </el-form>
      </template>
      <template slot="table">
        <el-table  :data="tableData"></el-table>
      </template>
    </common-table>
  </div>
</template>

相关文章

  • 99.vue使用具名插槽

    DAY20:定义组件模板(PS:省略了一些布局的样式代码): 注册自定义组件(PS:手动引入): 注册自定义组件(...

  • vue 插槽 slot

    插槽使用 普通插槽 具名插槽 使用具名插槽 从插槽里面传值出来如何接收? 如: 如何判断某个插槽是否被使用 组件内...

  • Vue-使用插槽

    二。具名卡槽 作用域插槽:插槽循环时使用

  • Vue插槽的使用方法

    About 使用插槽是为了能够在页面中显示向组件中传入的新的HTML 一、具名插槽 效果图: 2. 具名插槽的使用...

  • Vue插槽:slot、slot-scope与指令v-slot使用

    不具名插槽 具名插槽 v-slotv-slot在组件中使用slot进行占位时,也是在slot标签内使用name 属...

  • 详解vue中的插槽

    1.在vue中插槽分为具名插槽和非具名插槽;而插槽的使用主要是我们在页面中存在很多个相似但却重复的部分; 首先我以...

  • Vue 具名插槽

    有时候我们需要定义多个插槽,这时候就可以使用具名插槽,利用 slot 的 name 属性。示例:在向具名插槽提供内...

  • 2.插槽

    匿名插槽 具名插槽 作用域插槽

  • vue----slot插槽

    插槽分类 匿名插槽 具名插槽 作用域插槽

  • Vue 插槽

    1.插槽的简单使用 在组件中声明一个插槽 使用组件时 可以替换插槽 如果不替换插槽就使用默认值 2.具名...

网友评论

    本文标题:99.vue使用具名插槽

    本文链接:https://www.haomeiwen.com/subject/gmzsertx.html