Advanced Vue.js Lecture 2
11. Use transition-group element to implement list animation
<style>
li{
border: 1px dashed #999;
margin: 5px;
line-height: 35px;
padding-left: 5px;
font-size: 13px;
}
.v-enter,
.v-leave-to{
opacity: 0;
transform: translateY(100px);
}
.v-enter-active,
.v-leave-active{
transition: all 0.6s ease;
}
.v-move{
transition: all 0.6s ease;
}
.v-leave-active{
position: absolute;
}
</style>
</head>
<body>
<div id="app">
<div>
<label>Id:
<input type="text" v-model="id">
</label>
<label>Name:
<input type="text" v-model="name">
</label>
<input type="button" value="Add to" @click="add">
</div>
<transition-group appear tag="ul">
<li v-for="(item,i) in list" :key="item.id" @click="del(i)">
{{item.id}}---{{item.name}}
</li>
</transition-group>
</div>
<script>
var app=new Vue({
el:"#app",
data:{
id:"",
name:"",
list:[
{id:1,name:"eunuch who conspired with Li Si to influence the succession to the First Emperor"},
{id:2,name:"Wang pan"},
{id:3,name:"Zhang Hao"},
{id:4,name:"Yuan Meng Yang"}
]
},
methods:{
add(){
this.list.push({id:this.id,name:this.name})
this.id=this.name=""
},
del(i){
this.list.splice(i,1)
}
}
});
</script>
</body>
12, why the data of the component must be a function
<div id="app">
<test></test>
</div>
<template id="temp">
<div>
<input type="button" value="add one-tenth" v-on:click="increment">
{{count}}
</div>
</template>
<script>
var dataObj={count:0}
Vue.component("test",{
template:"#temp",
data:function(){
return dataObj;
},
methods:{
increment(){
this.count+=1
}
}
})
var app=new Vue({
el:"#app"
});
</script>
13. Custom key modifier
<div id="app">
<input type="text" v-model="message" v-on:keyup.f2="add()">
</div>
<script>
var app=new Vue({
el:"#app",
data:{
message:"12345"
},
methods:{
add:function(){
alert(this.message)
}
}
});
Vue.config.keyCodes.f2=113
</script>
14. Customize global instructions for text boxes to get focus
<div id="app">
<input type="text" v-focus v-color="'yellow'">
</div>
<script>
Vue.directive("focus",{
bind:function(el){
},
inserted:function(el){
el.focus()
},
updated:function(){
}
})
Vue.directive("color",{
bind:function(el,binding){
el.style.color=binding.value
}
})
new Vue({
el:"#app"
});
</script>
15, Component - Way to Create Components 1
<div id="app">
<my-com1></my-com1>
</div>
<script>
var com1=Vue.extend({
template:"<h3>This is used. Vue.extend Components created</h3>"
})
Vue.component("myCom1",com1)
var app=new Vue({
el:"#app"
});
</script>
16. Components - Ways to Create Components 2
<div id="app">
<my></my>
</div>
<script>
Vue.component("my",{
template:"<div><h3>This is direct use. Vue.component Components created</h3><span>123</span></div>"
})
var app=new Vue({
el:"#app"
});
</script>
17. Components - Ways to Create Components 3
<div id="app">
<mycom3></mycom3>
</div>
<div id="app2">
<login></login>
</div>
<template id="temp">
<div>
<h2>This is a component.</h2>
<span>123</span>
</div>
</template>
<script>
Vue.component("mycom3",{
template:"#temp"
});
var app=new Vue({
el:"#app"
});
new Vue({
el:"#app2",
components:{
login:{
template:"<h3>Customize private components</h3>"
}
}
});
</script>
18, Component Switching - Mode 1
<div id="app">
<a href="" @click.prevent="isShow=true">Land</a>
<a href="" @click.prevent="isShow=false">register</a>
<login v-if="isShow==true"></login>
<register v-if="isShow==false"></register>
</div>
<script>
Vue.component("login",{
template:"<h3>Landing module</h3>"
});
Vue.component("register",{
template:"<h3>Registration module</h3>"
});
var app=new Vue({
el:"#app",
data:{
isShow:true
}
});
</script>
19, Component Switching - Mode 2
<div id="app">
<a href="" @click.prevent="comName='login'">Land</a>
<a href="" @click.prevent="comName='register'">register</a>
<component :is="comName">
</component>
</div>
<script>
Vue.component("login",{
template:"<h3>Landing module</h3>"
});
Vue.component("register",{
template:"<h3>Registration module</h3>"
});
var app=new Vue({
el:"#app",
data:{
comName:"login"
}
});
</script>
20, component switching animation
<style>
.v-enter,
.v-leave-to{
opacity: 0;
transform: translateX(150px);
}
.v-enter-active,
.v-leave-active{
transition: all 0.5s ease;
}
</style>
</head>
<body>
<div id="app">
<a href="" @click.prevent="comName='login'">Land</a>
<a href="" @click.prevent="comName='register'">register</a>
<transition mode="out-in">
<component :is="comName"></component>
</transition>
</div>
<script>
Vue.component("login",{
template:"<h3>Landing module</h3>"
});
Vue.component("register",{
template:"<h3>Registration module</h3>"
});
var app=new Vue({
el:"#app",
data:{
comName:"login"
}
});
</script>