vue 封装div根据数组有序排列,并调用

一、封装Items

完整代码如下

<template>
	<div class="container">
		<div v-for="(item, index) in items" :key="index" class="item-box">{{ item }}
		</div>
	</div>
</template>

<script>
	export default {
		props: {
			items: {
				type: Array,
				// default: ,
			}
		},
	};
</script>

<style>
	.container {
		/* display: flex; */
		/* 设置容器为flex布局 */
		/* justify-content: space-between; */
		/* 水平居中显示子项 */
	}

	.item-box {
		width: 100px;
		/* 设置每个div的宽度 */
		height: 30px;
		/* 设置每个div的高度 */
		background-color: forestgreen;
		/* 设置每个div的背景色 */
		margin-top: 7px;
		margin-left: 3px;
		/* 设置每个div之间的外边距 */
		color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 22px;
		font-weight: 500;

		border-style: ridge;
		border-width: 2px;
		border-color: #0085AA;
	}
</style>

二、调用Items

<template>
	<div class="Locationmap">
		<Items :items="items1"></Items>
		<Items :items="items2"></Items>
		<Items :items="items3"></Items>
	</div>
</template>

<script>
	import Items from "./Items.vue";
	export default {
		components: {
			Items
		},
		data() {
			return {
				items1: [
					"1-1-1",
					"1-1-2",
					"1-1-3",
					"1-1-4",
					"1-2-1",
					"1-2-2"
				],
				items2: [
					"2-1-1",
					"2-1-2",
					"2-1-3",
					"2-2-1",
					"2-2-2",
					"2-2-3"
				],
				items3: [
					"3-1-1",
					"3-1-2",
					"3-1-3",
					"3-1-4",
					"3-1-5",
					"3-2-1",
					"3-2-2"
				]
			};
		},
	};
</script>


<style scoped>
	.Locationmap {
		position: relative;
		height: 763px;
		width: 100%;
		margin-top: 30px;
		margin-left: 0px;
		display: flex;
	}
</style>

效果如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在Vue项目中需要在某个方法中使用防抖函数时,可以将其封装为一个公共的工具函数,并在需要的方法中调用。下面是一个封装防抖函数并在方法内调用的示例代码: ```javascript // utils.js export function debounce(func, delay) { let timer; return function () { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, arguments); }, delay); }; } ``` 在上述示例中,我们将防抖函数封装在了一个名为`debounce`的工具函数中,并通过`export`关键字导出,以便在其他文件中使用。 接下来,我们可以在Vue组件的某个方法中调用这个封装好的防抖函数: ```html <template> <div> <button @click="handleClick">点击</button> </div> </template> <script> import { debounce } from "@/utils"; export default { methods: { handleClick() { const debounceHandleClick = debounce(() => { // 处理点击逻辑 }, 500); // 设置防抖时间间隔,单位为毫秒 debounceHandleClick(); }, }, }; </script> ``` 在上述示例中,我们通过`import`语句引入了之前封装的防抖函数,然后在`handleClick`方法中调用了`debounce`函数,并传入要执行的函数和防抖时间间隔。 这样,在用户点击按钮时,只有在一定时间内没有再次点击时,才会执行实际的处理逻辑,从而实现了防抖效果。 通过封装防抖函数并在方法内调用,可以减少代码的重复性,提高代码的可维护性和复用性。 希望以上示例对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值