切换组件component

子给父组件传递数据,KeepAlive缓存

component切换组件

1.父组件

<script setup>
import coma from './components/coma.vue'
import comb from './components/comb.vue'
import comc from './components/comc.vue'
import comd from './components/comd.vue'
import { shallowRef, ref } from 'vue'
// 变化的序号
const active = ref('0')
// 响应式组件
const names = shallowRef(coma)
const arr = [coma, comb, comc, comd]


const fun = (item) => {
	names.value = arr[item]
	console.log(names.value)
	// 从子组件传过来的数据
	console.log(item)
	active.value = item

}
</script>

<template>
	<div>
		<p>
			<span v-for="(item, index) in 4" :style="{ 'color': index == active ? '#60a7a7' : '#787878' }">第{{ item }}步</span>
		</p>
		<!-- 缓存 -->
		<KeepAlive>
			<component :is="names" @add="fun"></component>
		</KeepAlive>

	</div>
</template>

<style scoped>
span {
	padding: 0px 10px;
	border-bottom: 1px solid #4ea5c3;
}

;
</style>

2.子组件

(1)

<template>
	<div class="box">
		输入姓名: <input type="text">
		<button @click="$emit('add', '1')">下一步</button>
	</div>
</template>

<script setup>



const emit = defineEmits(['add'])
</script>

<style scoped="scoped">
.box {
	display: block;
	border: 2px solid rgb(255, 155, 155);
	width: 244px;
	color: rgb(255, 174, 174);

}

button:focus {
	outline: 0;
}

button:hover {
	color: rgb(255, 255, 255);
	background-color: rgb(255, 169, 169);
	border: none;
}

input {
	outline: none;
	border: 1px rgb(105, 208, 228) solid;
}
</style>

(2)

<template>
	<div class="box">
		输入密码: <input type="text">
		<button @click="$emit('add', '2')">下一步</button>
	</div>
</template>

<script setup>



const emit = defineEmits(['add'])
</script>

<style scoped="scoped">
.box {
	display: block;
	border: 2px solid rgb(99, 124, 234);
	width: 244px;
	color: rgb(174, 201, 255);

}

button:focus {
	outline: 0;
}

button:hover {
	color: rgb(255, 255, 255);
	background-color: rgb(127, 118, 255);
	border: none;
}

input {
	outline: none;
	border: 1px rgb(105, 208, 228) solid;
}
</style>

(3)

<template>
	<div class="box">
		重复密码: <input type="text">
		<button @click="$emit('add', '1')">上一步</button>
		<button @click="$emit('add', '3')">下一步</button>
	</div>
</template>

<script setup>



const emit = defineEmits(['add'])
</script>

<style scoped="scoped">
.box {
	display: block;
	border: 2px solid rgb(155, 255, 201);
	width: 244px;
	color: rgb(47, 136, 84);

}

button:focus {
	outline: 0;
}

button:hover {
	color: rgb(255, 255, 255);
	background-color: rgb(141, 243, 209);
	border: none;
}

input {
	outline: none;
	border: 1px rgb(105, 208, 228) solid;
}
</style>

(4)

<template>
	<div class="box">
		<button @click="$emit('add', '2')">上一步</button>

		<button>完成</button>
	</div>
</template>

<script setup>



const emit = defineEmits(['add'])
</script>

<style scoped="scoped">
.box {
	display: block;
	border: 2px solid rgb(255, 229, 156);
	width: 244px;
	color: rgb(255, 179, 80);

}

button:focus {
	outline: 0;
}

button:hover {
	color: rgb(255, 255, 255);
	background-color: rgb(255, 195, 44);
	border: none;
}
</style>

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态组件componentVue内置的一个组件,它的主要作用是根据指定的组件名称动态渲染组件。基本用法是通过`:is`属性来控制组件的渲染,例如: ```html <component :is="componentName"></component> ``` 其中,`componentName`是一个在Vue实例中定义的属性,用来指定要渲染的组件的名称。这样就可以根据不同的条件动态地渲染不同的组件。 动态组件的传值遵循基本组件传值规则,可以使用`v-bind`来传递数据给动态组件。例如: ```html <component :is="currentTabComponent" class="tab" :data="propsData" :dataA="propsDataA" :dataB="propsDataB"></component> ``` 在这个例子中,`currentTabComponent`是一个在Vue实例中定义的属性,用来指定要渲染的组件的名称。同时,使用`v-bind`来传递数据给动态组件,`propsData`、`propsDataA`、`propsDataB`是在Vue实例中定义的属性,用来传递给动态组件的数据。 在Vue 2.1版本中,如果数据需要动态渲染,组件切换之后会导致之前获得的数据丢失。为了避免重复渲染导致性能问题,可以使用`keep-alive`来缓存组件中的数据。例如: ```html <keep-alive> <component :is="currentTabComponent" class="tab"></component> </keep-alive> ``` 在这个例子中,`keep-alive`标签用来包裹动态组件,这样在组件切换过程中可以保持组件的状态,避免重复渲染。 #### 引用[.reference_title] - *1* *2* *3* [vue内置动态组件component使用详解](https://blog.csdn.net/m0_46309087/article/details/123303640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值