什么是Vue?
为什么使用Vue?
1,声明式渲染 vue.js核心就是一个允许采用简洁的模块语法来声明式的将数据渲染进DOM系统。 2.响应式数据绑定 MVVM M:Model层 V:View 层 VM: ViewModel层,数据绑定,数据监听 3.组件化开发 它是一种抽象,允许我们使用小型,独立和通常可复用的组件构建大型,独立和通常可复用的组件构建大型应用。 4.虚拟DOM 浏览器处理dom性能是会有瓶颈的,而使用虚拟dom可以减少dom操作,提升渲染性能。
Vue实例
<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>
</head>
<body>
<div id="app">{{message}}</div>
<script src="vue.js"></script>
<script>
const data = {
message:"一个vue程序",
str:null
}
const app = new Vue({
el:"#app",
data:data,
beforeCreate:function(){
console.log(this)
console.group("beforeCreate实例创建前")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
created:function(){
console.group("created实例创建完毕")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
beforeMount:function(){
console.group("beforeMount实例挂载到dom前")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
mounted:function(){
console.group("mounted实例挂载到dom完毕")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
beforeUpdate:function(){
console.group("beforeUpdate数据更新前")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
updated:function(){
console.group("updated数据更新完毕")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
beforeDestroy:function(){
console.group("beforeDestroy销毁前")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
},
destroyed:function(){
console.group("destroyed销毁完毕")
console.log("el===>",this.$el)
console.log("data====>",this.$data)
console.log("message=====>",this.message)
}
})
app.$watch("message",(newval,oldval)=>{console.log(newval,oldval)})
</script>
</body>
</html>
数据绑定
<style>
.div-class{
color:red
}
.div-class_none{
display: none
}
</style>
<body>
<div id="app">
{{ message + "-" + hoverTitle}}
<div v-html=html v-bind:id="myId" v-bind:title="hoverTitle" v-if="isShow"></div>
<h1>{{count}}</h1>
<button v-on:click="handleClick">点击+1</button>
<input type="text" placeholder="请输入内容" v-on:focus="onFoucus" v-on:blur="onBlur"/>
<div>
<div :style="[baseStyle,otherStyle]" >Class</div>
<button @click="handleChangeClick">控制class显示</button>
</div>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
message:"数据绑定",
html:"<p>html文本</p>",
myId:"myId",
hoverTitle:"title",
isShow:false,
count:0,
strClass:'div-class',
hiddenClass:"div-class_none",
flag:true,
baseStyle:{
color:'red',
backgroundColor:"yellow"
},
otherStyle:{
backgroundColor:'green',
border:"1px solid orange",
transition:'all 1s'
}
},
methods:{
handleClick(){
this.count++;
},
onFoucus(){
console.log("获取焦点")
},
onBlur(){
console.log("失去焦点")
},
handleChangeClick(){
this.hiddenClass = ""
}
}
})
</script>
</body>
计算属性与监听属性
<div id="app">
{{fullName}}
</div>
<script src="vue.js" ></script>
<script>
const app = new Vue({
el:"#app",
data:{
firstName:'王 -',
lastName:'小 哇',
msg:"改变的值",
fullName:"-------"
},
methods:{
computedName(){
console.log("方法实现")
return this.firstName+ "." + this.lastName
}
},
computed:{
endName:function(){
console.log("计算属性实现")
return this.firstName+ "." + this.lastName
}
},
watch:{
firstName(){
console.log("watch属性firstName一次")
this.fullName = this.firstName+ "." + this.lastName
this.firstName = this.firstName.split(" ").reverse()
this.lastName = this.lastName.split(" ").reverse()
},
lastName(){
console.log("watch属性lastName一次")
this.fullName = this.firstName+ "." + this.lastName
}
}
})
</script>
条件渲染
<div id="app">
<div v-show="flag">
<div>1==show</div>
<div>2==show</div>
<div>3==show</div>
<div>4==show</div>
</div>
<button @click="handleClick">控制显示隐藏</button>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
showMsg:"显示数据",
flag:true,
count:0
},
methods:{
handleClick(){
this.flag = !this.flag
}
}
})
</script>
列表渲染
<div id="app">
<template v-if="dataArr.length>2">
<ul>
<li v-for="(item,index) in dataArrFilter" :key="index" @click="handleDelete(index)">{{item.name}}---------{{item.id}}-------{{index}}</li>
</ul>
<button @click="change">改变列表项的值</button>
<ul>
<li v-for="(value,key,index) in dataObj">{{value}}======={{key}}========={{index}}</li>
</ul>
<button @click="changeObj">改变列表项的值</button>
</template>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
dataArr:[
{id:1,name:'🐘'},
{id:2,name:"🐯"}
],
dataObj:{
age:10,
name:"张三"
}
},
methods:{
handleDelete(index){
this.dataArr.splice(index,1)
},
change(){
this.$set(this.dataArr,0,{id:1,name:"🍌"})
this.dataArr.push({id:3,name:"🦊"})
this.dataArr.pop()
},
changeObj(){
this.dataObj.age = 20
this.$set(this.dataObj,"pyhone","11111111")
this.dataObj = Object.assign({},this.dataObj,{address:"xxxxxx",hobby:'学习'})
}
},
computed:{
dataArrFilter:function(){
return this.data.filter(item=>{
return item.id !== 1
})
}
}
})
</script>
事件处理
<div id="app">
<p>{{count}}</p>
<div @click="handleClick">
<button @click.stop.self="handleAdd($event)">点击+1</button>
</div>
<input type="text" placeholder="请输入内容" @keyup.vup="onSumbit"/>
</div>
<script src="vue.js"></script>
<script>
Vue.config.keyCodes.vup = 86
const app = new Vue({
el:"#app",
data:{
count:0
},
methods:{
handleAdd(){
this.count++
},
handleClick(evnet){
console.log("父级")
},
onSumbit(){
console.log("数据提交")
}
}
})
</script>
表单输入绑定
<div id="app">
<p>input输入的值:{{inputValue}}</p>
<input type='text' placeholder="请输入" value="value初始值" v-model.number="inputValue"/>
<p>input输入的值:{{textareaValue}}</p>
<textarea name="" id="" cols="30" rows="30" v-model="textareaValue">uuuuuuu</textarea>
<label for="boy">{{checkboxVaue}}</label>
<input type="checkbox" id="children" value="children" v-model="checkboxArr"/>
<label for="children">{{checkboxArr}}</label>
<input type="checkbox" id="sister" value="sister" v-model="checkboxArr"/>
<label for="sister">{{checkboxArr}}</label>
<input type="checkbox" id="brother" value="brother" v-model="checkboxArr"/>
<label for="brother">{{checkboxArr}}</label>
<input type="radio" id="girl" value="girl" v-model="sex"/>
<label for="girl">{{sex}}</label>
<input type="radio" value="boy" v-model="sex"/>
<label for="boy">{{sex}}</label>
<p>{{userinfo}}</p>
<select v-model="userinfo" multiple>
<option value="" disabled>请选择</option>
<option value="age" >年龄</option>
<option value="name" >姓名</option>
</select>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
inputValue:"",
textareaValue:"",
checkboxVaue:true,
checkboxArr:[],
sex:"boy",
userinfo:'age'
},
methods:{
}
})
</script>
|