<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue組件詳解</title>
<script src='vue.js'></script>
</head>
<body>
<script>
// 全局組件
// Vue.component('my-hello',{
// template:'<h1>hellow word!</h1>'
// })
window.onload=function(){
new Vue({
el:"#my",
data:{
title:'子調父' ,
flag:'aa'
},
// 局部組件
components:{
'my-slot':{
template:'#myslot',
props:['name'],
data(){
return{
age:12
}
}
// directive:{
// focus:{
// inserted(el){
// el.focus();
// }
// }
// }
},
'aa':{
template:'<h1>num:{{x}}</h1>',
data(){
return{
x:Math.random()
}
}
}
}
})
}
</script>
<template id='myslot'>
<div>
<p>{{age}}</p>
<p>{{name}}</p>
<slot name='s1'></slot>
<!-- 自定義指令 -->
<input type="text" >
</div>
</template>
<div id='my'>
<!-- <my-hello/> -->
<!-- <my-hello1 :name='title'/> -->
<my-slot :name='title'>
<!-- slot內容分發套用模板時加入本身獨有的元素-->
<ul slot='s1'>
<li>dddd</li>
</ul>
</my-slot>
<button @click="flag='my-slot'">my-slot</button>
<button @click="flag='aa'">aa</button>
<!-- 切換顯示的組件隨機數據會改變 -->
<!-- <component :is="flag"></component> -->
<!-- <keep-alive>緩存非活動組件此時切換顯示的組件隨機數據不變 -->
<keep-alive>
<!-- 動態組件 表示flag==my-slot時 組件my-slot顯示 flag=aa時組件aa顯示 -->
<component :is="flag"></component>
</keep-alive>
</div>
</body>
</html>html