当前位置: 代码网 > it编程>前端脚本>Vue.js > vue如何通过插槽组件之间数据传递

vue如何通过插槽组件之间数据传递

2024年06月10日 Vue.js 我要评论
vue通过插槽组件之间数据传递1.父向子传值//--->father.vue<template> <div id="father"> <p>黄色为父组

vue通过插槽组件之间数据传递

1.父向子传值

//--->father.vue
<template>
  <div id="father">
    <p>黄色为父组件---data的值为:{{ data }}</p>
    <son>
        <template #usname>
            <p>data的值为:{{data}}</p>//将父组件的data数据传入子组件的<slot>插槽
        </template>
    </son>
  </div>
</template>
 
<script>
import son from "./son.vue";
export default {
  data() {
    return {
      data: "father",
    };
  },
  components: {
    son,
  },
};
</script>
 
<style scoped>
#father {
  width: 400px;
  height: 400px;
  background-color: yellow;
}
</style>
 
//--->son.vue
<template>
  <div id="son">
    <p>蓝色为子组件</p>
    <slot name="usname"></slot>
  </div>
</template>
 
<script>
export default {};
</script>
 
<style scoped>
#son {
  width: 200px;
  height: 200px;
  background-color: skyblue;
}
</style>

2.子向父传值

//--->father.vue
<template>
  <div id="father">
    <p>黄色为父组件---data的值为:{{ data }}</p>
    <button @click="upp">1</button>//点击查看传过来的值
    <son>
      <template #usname="obj">//通过v-slot指令接收son.vue传过来的值
        <p>data的值为:{{ data }}</p>
        <p>son_data的值为{{ obj.son }}</p>
        <button @click="son_data = obj.son">1</button
        >//通过点击按钮将传过来的值保存在father组件的son_data数据中
      </template>
    </son>
  </div>
</template>
 
<script>
import son from "./son.vue";
export default {
  data() {
    return {
      data: "father",
      son_data: "",
    };
  },
  components: {
    son,
  },
  methods: {
    upp() {
      console.log(this.son_data);
    },
  },
};
</script>
 
<style scoped>
#father {
  width: 400px;
  height: 400px;
  background-color: yellow;
}
</style>
//--->son.vue
<template>
  <div id="son">
    <p>蓝色为子组件</p>
    <slot name="usname" :son='son_data'></slot>//添加自定义属性son将son_data数据传入父组件
  </div>
</template>
 
<script>
export default {
    data(){
        return{
            son_data:'son'
        }
    }
};
</script>
 
<style scoped>
#son {
  width: 200px;
  height: 200px;
  background-color: skyblue;
}
</style>

vue跨组件动态插槽传递

在看coderwhy老师后台管理系统项目实战视频的时候发现组件封装的思路与动态插槽传递非常有意思,记录一下!

需求及基本信息说明

组件goods调用组件page-content,组件page-content调用组件hy-table。

为了方便使用,组件page-content封装公共插槽(如果将所有页面的私有的插槽都一股脑放到组件page-content中封装性会变差),需要在goods中传递私有插槽内容在hy-table中显示。

这时候就产生了跨组件插槽传递的需求,而由于编译作用域限制的原因,goods组件中的#image不能直接被hy-table接收,而需要先由page-content接收goods中的数据,再由page-content将数据传递到hy-table中。

而实际开发中不可能在page-content中将插槽名称写死,上面图例page-content中只是简单拿#image进行举例,实际开发中可以在组件page-content中动态插入私有插槽,实现如下。

代码

  • goods.vue

goods中使用组件page-content并传入配置文件contenttableconfig,并向page-content中名字为image的插槽提供内容<el-image>

<template>
  <div class="goods">
    <page-content :contenttableconfig="contenttableconfig" pagename="goods">
      <template #image="scope">
        <el-image
          style="width: 60px; height: 60px"
          :src="scope.row.imgurl"
          :preview-src-list="[scope.row.imgurl]"
        >
        </el-image>
      </template>
      <template #oldprice="scope">{{ '¥' + scope.row.oldprice }}</template>
    </page-content>
  </div>
</template>
  • contenttableconfig配置文件数据

其中slotname为status、createat、updateat、handler为公共插槽配置

export const contenttableconfig = {
  title: '商品列表',
  proplist: [
    { prop: 'name', label: '商品名称', minwidth: '80' },
    { prop: 'oldprice', label: '原价格', minwidth: '80', slotname: 'oldprice' },
    { prop: 'newprice', label: '现价格', minwidth: '80' },
    { prop: 'imgurl', label: '商品图片', minwidth: '100', slotname: 'image' },
    { prop: 'status', label: '状态', minwidth: '100', slotname: 'status' },
    {
      prop: 'createat',
      label: '创建时间',
      minwidth: '250',
      slotname: 'createat'
    },
    {
      prop: 'updateat',
      label: '更新时间',
      minwidth: '250',
      slotname: 'updateat'
    },
    { label: '操作', minwidth: '120', slotname: 'handler' }
  ],
  showindexcolumn: true,
  showselectcolumn: true
}
  • page-content.vue

定义一个函数otherpropslots对配置信息进行过滤,去掉公共的插槽名称,剩下就是私有的插槽名称了,返回一个包含私有插槽的数组,在template中对这个数组进行遍历,slot :name="私有插槽名"接收goods中的内容,“#私有插槽名”的template向子组件hy-table传递内容

<template>
  <div class="page-content">
    <hy-table
      v-bind="contenttableconfig"
    >
 
      <template #status="scope">
        <el-button
          plain
          size="mini"
          :type="scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createat="scope">
        <span>{{ $filters.formattime(scope.row.createat) }}</span>
      </template>
      <template #updateat="scope">
        <span>{{ $filters.formattime(scope.row.updateat) }}</span>
      </template>
      <template #handler>
        <div class="handle-btns">
          <el-button v-if="isupdate" icon="el-icon-edit" size="mini" type="text"
            >编辑</el-button
          >
          <el-button
            v-if="isdelete"
            icon="el-icon-delete"
            size="mini"
            type="text"
            >删除</el-button
          >
        </div>
      </template>
 
      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherpropslots"
        :key="item.prop"
        #[item.slotname]="scope"
      >
        <template v-if="item.slotname">
          <slot :name="item.slotname" :row="scope.row"></slot>
        </template>
      </template>
 
    </hy-table>
  </div>
</template>
 
<script lang="ts">
import { definecomponent } from 'vue'
import hytable from '@/base-ui/table'
 
export default definecomponent({
  components: {
    hytable
  },
  props: {
    contenttableconfig: {
      type: object,
      require: true
    },
    pagename: {
      type: string,
      required: true
    }
  },
  setup(props) {
    // 获取其他的动态插槽名称
    const otherpropslots = props.contenttableconfig?.proplist.filter(
      (item: any) => {
        if (item.slotname === 'status') return false
        if (item.slotname === 'createat') return false
        if (item.slotname === 'updateat') return false
        if (item.slotname === 'handler') return false
        return true
      }
    )
    return {
      otherpropslots
    }
  }
})
</script>

想说的话:

动态传递插槽和封装的思路绝了,需要花好多功夫和时间去消化~

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com