动态组件 <component > 标签基本使用


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
	<style>
		* {
     
     
			padding: 0;
			margin: 0;
			list-style: none;
		}
	</style>
</head>
<body>
	<div id="div">
		<button @click="set('comp1')">按钮1</button>
		<button @click="set('comp2')">按钮2</button>

		<component :is="curr"></component>
		<!-- 
			is 后跟组件的名字字符串
			
			<component is="comp1"></component> === <comp1></comp1>
		-->
	</div>
	<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
	<script>


		let comp1 = {
     
     
			template: `
				<div>
					comp1
				</div>
			`,
		}



		let comp2 = {
     
     
			template: `
				<div>
					comp2
				</div>
			`,
		}


		let vm = new Vue({
     
     
			components:{
     
     
				comp1,
				comp2,
			},
			el:'#div',
			data:{
     
     
				curr:'comp1',
			},
			methods:{
     
     
				set(comp){
     
     
					this.curr = comp;
				}
			},
			mounted(){
     
     
				

			}
		})
		
	</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/conggeqw12138/article/details/115339725