Vue.js 動態元件使用

給我一瓶冰闊洛發表於2020-12-02
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 
</head>
<body>
<div id="app">	
	<span @click="coml('coma')">111111</span>
	<span @click="coml('comb')">2222</span>
	<span @click="coml('comc')">333333</span>
	<component :is="currentcom"></component>
</div>	

</body>
<script>
	 var vm = new Vue({
	  el: '#app',
	  data:{
		  currentcom:'coma'
	  },
	  methods:{
		  coml:function(e){
			  this.currentcom=e
		  }
	  },
	  components:{
		 coma:{
			  template:"<h1>11111</h1>"
		 },
		 comb: { 
		     template: "<div>222221</div>"  
		 },
		 comc:{
			  template: "<div>33333331</div>"  
		}
	 }

	})
</script>
</html>

 

相關文章