查漏補缺!這份 VUE 學習知識總結請注意查收! | 原力計劃
el:'',
data:{
msg:"我的信息",
msg1:"<span>我是一個html</span>",
mytitle:'title',
list:[
{id:1,name:'zhangsan'},
{id:2,name:'lisi'},
{id:3,name:'wangmazi'},
],
obj:{
id:1,
name:'orange',
age;18
},
flag:'true' ,
style1:{
color:'pink',
'font-size':'30px'
},
style2:{
font-family: monospace;
}
},
methods:{
click{}
},
filters:{
filter1:function(data){
//代碼塊
}
}
})
<li v-for="(item,index) in list" :key="item.id">index----{{ index }}--{{ item.id }}--{{ item.name }}</li>
<ul>
<p>{{ msg }}</p>
bind:只調用一次,指令第一次綁定到元素時調用,用這個鉤子函數可定義一個在綁定時執行一次的初始畫動作。 inserted:被綁定元素插入父節點時調用(父節點存在時即可調用,不必存在於document中)。。 update:所在組件的VNode更新時調用,但是可能發生在其孩子的 VNode 更新之前。指令的值可能發生了改變也可能沒有。但可通過比較更新後的值來忽略不必要的模板更新 componentUpdated:所在組件的 VNode 及其孩子的 VNode 全部更新時調用。 unbind:只調用一次,指令於元素解綁時調用。
bind:function(el,binding){},
inserted:function{el.focus},
update:function{}
})
bind:function(el,binding){
el.style.color=binding.value
},
})
el:'',
data:{},
directives:{
'fontWeight':{
bind:function(el,binding){
el.style.fontWeight = binding.value
}
}
}
})
el.style.backgroundColor =binding.value
})
.stop阻止冒泡 .prevent阻止默認事件 .capture添加事件偵聽器時使用事件捕獲模式 .self只當事件在該元素本身觸發時觸發回調 .once事件只觸發一次
<h1 :class="['class1','class2',flag?class3:'']"></h1>
<!-- 三元表達式,如果flag爲true則有class3 -->
<h1 :class="['class1','class2',{class3:flag}]"></h1>
<!-- 數組中也可以嵌套對象,對象鍵爲類名,值爲布爾值-->
<!-- 也可以是對象對象鍵爲類名,值爲布爾值-->
el:'',
data:{},
//創建階段
beforeCreate{},//實例創建之前執行
created{},
mounted{}, //內存中的模板已經掛載到頁面中
//運行階段
beforeUpdate{},//數據被更新但尚未渲染到頁面中,數據更新時纔會調用
updated{},//數據已經渲染到頁面中
//銷燬階段
beforeDestory{},//data methods 過濾器都是可用的還未執行銷燬過程
destoryed{} //組件已經被銷燬,所有的東西已不可用
})
.v-enter,
.v-leave-to{
opacity:0;
transform :translateX(200px);
}
.v-enter-active,
.v-leave-active{
transition: all 0.5s ease;
}
</style>
<div>
<input type="button" @click="flag=!flag" value="動畫">
<transition>
<h1 v-if="flag">我是h1</h1>
</transition>
</div>
template: '<div><h4>這是一個h4標題</h4></div>'
})
<div>
<h4>這是一個h4標題</h4>
</div>
</template>
<script>
Vue.component('comph4', {
template: '#cmp4'
})
</script>
el:'#app1',
data:{},
methods:{},
components:{
comph3:{
template:'#cmp3'
}
}
})
<comp4><comp4>
<div id="app1">
<comp3><comp3>
</div>
<template id="cmp4">
<div>
<h4>這是一個h4標題</h4>
</div>
</template>
<template id="cmp3">
<div>
<h3>這是一個h3標題</h3>
</div>
</template>
</body>
template: '#cmp4',
data: function{
return{
msg:'msg這是組件中data中的數據'
}
},
methods:{}
})
<input type="button" value="cmph3" @click="iscom='comph3'">
<input type="button" value="cmph4" @click="iscom='comph4'">
<component :is="iscom"></component>
</div>
var vm = new Vue({
el:'#app1',
data:{
iscom:''
}
})
<comph4 :fromParent="msg" @fun="show"></comph4>
</div>
var vm = new Vue({
el:'#app1',
data:{
msg:'我是父組件中的msg',
fromSon:null
},
methods:{
show(data){ //父組件的show方法接收一個參數
this.fromeSon=data; //將從子組件接收過來的data賦值給父組件的fromSon
}
}
compents:{
comph4:{
template:'<div>
<h4>我是一個h4---{{fromParent}}</h4>
<input type="button" value="調用父組件中的方法" @click=”myclick“>
</div>',
props:['fromParent'],
data:function{
return {
data:{name:'orange',age:18}
}
},
methods:{
myclick{
this.$emit('fun',this.data) //將子組件中的data傳遞給父組件
}
}
}
}
})