模板语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>初识vue</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>插值语法</h1>
<h3>hello {{name}}</h3>
<h1>指令语法</h1>
<a v-bind:href="school.url.toUpperCase()" x="hello"> 点我去{{school.name}}学习</a>
<a :href="school.url">点我去{{school.name}}</a>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
name:'aa',
school:{
name:'尚硅谷',
url:'http://www.baidu.com'
}
}
})
</script>
</body>
</html>
数据绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数据绑定</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
单向数据绑定:<input type="text" :value="name"> <br/>
双向数据绑定:<input type="text" v-model="name"> <br/>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
name:'aa'
}
})
</script>
</body>
</html>
el与data的两种写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>你好 {{name}}</h1>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
// const v = new Vue({
// //el:'#root', //第一种写法
// data:{
// name:'aa'
// }
// })
// console.log(v)
// v.$mount('#root')
new Vue({
el:'#root',
// data:function(){
// return{
// name:'sss'
// }
// }
data(){
console.log('@@@',this)
return{
name:'sss'
}
}
})
</script>
</body>
</html>
1事件的基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>你好 {{name}}</h1>
<button v-on:click = "showInfo">点我提示信息</button>
<button @click = "showInfo1">点我提示信息</button>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
name:'aa'
},
methods:{
showInfo(){
alert('22222')
},
showInfo1(){
alert('22222')
}
}
})
</script>
</body>
</html>
08计算属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓<input type="text" v-model:value="firstName"><br/>
名<input type="text" v-model:value="lastName"> <br/>
姓名: <span>{{firstName}}--{{lastName}}</span>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
firstName:'qi',
lastName:'jun'
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓<input type="text" v-model="firstName"><br/>
名<input type="text" v-model="lastName"> <br/>
姓名: <span>{{fullName()}}</span>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
firstName:'qi',
lastName:'jun'
},
methods:{
fullName(){
return this.firstName + this.lastName
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓<input type="text" v-model="firstName"><br/>
名<input type="text" v-model="lastName"> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
firstName:'qi',
lastName:'jun',
},
computed:{
fullName:{
get(){
return this.firstName + '-' + this.lastName
},
set(value){
const arr = value.split('-')
this.firstName = arr[0]
this.lastName = arr[1]
}
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
姓<input type="text" v-model="firstName"><br/>
名<input type="text" v-model="lastName"> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
姓名: <span>{{fullName}}</span> <br/>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
firstName:'qi',
lastName:'jun',
},
computed:{
fullName(){
return this.firstName + '-' + this.lastName
}
}
})
</script>
</body>
</html>
监视属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>今天的天气很{{info}}</h1>
<button @click="changeWeather">切换</button>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
isHot:true
},
computed:{
info(){
return this.isHot ? '炎热' :'凉爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>今天的天气很{{info}}</h1>
<button @click="changeWeather">切换</button>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const vm = new Vue({
el:'#root',
data:{
isHot:true
},
computed:{
info(){
return this.isHot ? '炎热' :'凉爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
// watch:{
// isHot:{
// immediate:true,//初始化时让handler调用一下
// handler(newValue,oldValue){
// console(newValue,oldValue)
// }
// },
// info:{
// immediate:true,//初始化时让handler调用一下
// handler(newValue,oldValue){
// console(newValue,oldValue)
// }
// }
// }
})
vm.$watch('isHot',{
immediate:true,
handler(newValue,oldValue){
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>两种写法</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>今天的天气很{{info}}</h1>
<button @click="changeWeather">切换</button>
<hr/>
<h3>a的值是{{numbers.a}}</h3>
<button @click="numbers.a++">点我让a++</button>
<h3>b的值是{{numbers.b}}</h3>
<button @click="numbers.b++">点我让b++</button>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const vm = new Vue({
el:'#root',
data:{
isHot:true,
numbers:{
a:1,
b:1
}
},
computed:{
info(){
return this.isHot ? '炎热' :'凉爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
numbers:{
deep:true,
handler(){
console.log('numbers改变了')
}
}
}
})
</script>
</body>
</html>
12列表渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表渲染</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h2>人员列表</h2>
<ul>
<li v-for="p in persons" :key="p.id">
{{p.name}} -- {{p.age}}
</li>
</ul>
<h2>汽车信息</h2>
<ul>
<li v-for="(value,k) in car" :key="k">
{{k}} -- {{value}}
</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el:'#root',
data:{
persons:[
{id:'1',name:'xxx',age:'15'},
{id:'2',name:'xxxx',age:'11'},
{id:'3',name:'xxxxx',age:'111'}
],
car:{
name:'xx',
price:'2222'
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表渲染</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<input v-model="keyWord" type="text" placeholder="请输入名字">
<ul>
<li v-for="(p,index) of filPersons" :key="index">
{{p.name}}--{{p.age}}-{{p.sex}}
</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
//用watch实现
// new Vue({
// el:'#root',
// data:{
// keyWord:'',
// persons:[
// {id:'001',name:'马东',age:'22',sex:'女'},
// {id:'002',name:'六三',age:'32',sex:'男'}
// ],
// filPersons:[]
// },
// watch:{
// keyWord:{
// immediate:true,
// handler(val){
// this.filPersons = this.persons.filter((p)=>{
// return p.name.indexOf(val)!== -1
// })
// }
// }
// }
// })
//用computed实现
new Vue({
el:'#root',
data:{
keyWord:'',
persons:[
{id:'001',name:'马东',age:'22',sex:'女'},
{id:'002',name:'六三',age:'32',sex:'男'}
]
},
computed:{
filPersons(){
return this.persons.filter((p)=>{
return p.name.indexOf(this.keyWord)!== -1
})
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表渲染</title>
<script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<input v-model="keyWord" type="text" placeholder="请输入名字">
<button @click="sortType =1">升序</button>
<button @click="sortType =2">降序</button>
<button @click="sortType =0">原顺序</button>
<ul>
<li v-for="(p,index) of filPersons" :key="index">
{{p.name}}--{{p.age}}-{{p.sex}}
</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
//用computed实现
new Vue({
el:'#root',
data:{
keyWord:'',
persons:[
{id:'001',name:'马东',age:'22',sex:'女'},
{id:'002',name:'六三',age:'32',sex:'男'}
],
sortType:0
},
computed:{
filPersons(){
const arr = this.persons.filter((p)=>{
return p.name.indexOf(this.keyWord)!== -1
})
if(this.sortType){
arr.sort((p1,p2) =>{
return this.sortType > 1 ? p2.age-p1.age : p1.age-p2.age
})
}
return arr
}
}
})
</script>
</body>
</html>