开发需求4-vue组件排序(插槽实现)

组件排序-插槽实现

作用及意义

可以帮助我们实现修改页面上各模块组件的显示顺序

举个例子:一个页面显示广告有A、B、C,我们默认显示客户A、B、C,但是有1/4客户对广告B诉求比较大,我们希望这部分客户看到的广告顺序是B、A、C。而我们这个组件,就能实现这样的效果。

实现方案

index.vue

<template>
	<div>
		<sortCompenent :areaId="areaId" :defaultSorts="['compA', 'compB', 'compC']">
			<template v-slot:compA>
				<compA />
			</template>
			<template v-slot:compB>
				<compB />
			</template>
			<template v-slot:compC>
				<compC />
			</template>
		</sortCompenent>
	</div>
</template>
<script>
import sortCompenent from './sort-component'
import compA from './compA'
import compB from './compB'
import compC from './compC'
export default {
	name: 'index',
	components: {
		compA,
		compB,
		compC,
		sortCompenent
	},
	props: {
		areaId: 'IOP-AREA-8722',
	}
}
</script>
<style scoped>
</style>

sort-component

<template>
  <div class="wrap">
    <div v-for="item in uiSorts" :key="item">
      <slot :name="item"></slot>
    </div>
  </div>
</template>

<script>
import { getContentDoAdInfo } from './js/useZhiTui'
import { felog } from '@pa/pa-kit/felog'
export default {
  // vue component name
  name: 'ASortComponent',
  props: {
    // 区域id
    areaId: {
      type: String,
      default: ''
    },
    defaultSorts: {
      type: Array,
      default: function () {return []}
    },
    env: {
      type: String,
      default: 'prod'
    }
  },
  data() {
    return {
      sortsCompList: [],
      apiDomain: {
        prod: 'https://rsb.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?',
        fat: 'https://rsb-stg.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?',
        uat: 'https://rsb-stg.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?',
        unit: 'https://rsb-stg.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?',
        development: 'https://rsb-stg.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?',
        dev: 'https://rsb-stg.pingan.com.cn/brcp/stp/cust/iop/rtb/iop/strategy/mrContent.do?'
      }
    }
  },
  components:{
  },
  computed: {
    uiSorts () {
      if (this.sortsCompList && this.sortsCompList.length > 0) {
        return this.sortsCompList
      }
      return this.defaultSorts
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      const urlMap = this.apiDomain[this.env]
      const {err, json = {} } = await getContentDoAdInfo(this.areaId, this.context, urlMap)
      try {
        const list = json?.data?.list
        console.log('list', list)
        if (list.length > 0) {
          const compNameStr = JSON.parse(list[0].advertInfo[0].ext).componentName
          this.sortsCompList = eval('(' + compNameStr + ')')
          console.log('this.sortsCompList', this.sortsCompList)
        }
      } catch (error) {
        felog.error('智推接口出参处理出错', error)
      }
    }
  }
}
</script>
<style scoped>
	.wrap {
	    padding: 0 10px;
	    background: #ccc;
	}
</style>

目录层级

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_LiuP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值