- Demo1——vue对象、v-cloak、v-text、v-html、v-bind、v-on
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
[v-cloak] {
display: none !important;/*加!important是为了避免被其他高优先级属性覆盖*/
}
</style>
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<!-- 使用v-vloak命令解决刷新时暴露vue代码的问题,需要额外定义css样式但可以结合标签内容 -->
<p v-cloak>{{ msg }}</p>
<!-- 使用v-text命令解决刷新时暴露vue代码的问题,不需要额外定义css样式但会覆盖标签内容 -->
<p v-text="msg"></p>
<p>{{ msg2 }}</p>
<p v-html="msg2"></p>
<!-- v-bind命令用于绑定自定义属性,且可以写合法的js表达式 -->
<input v-bind:placeholder="mytitle + '?'" type="text">
<!-- v-bind:可简写为: -->
<input :placeholder="mytitle + '?'" type="text">
<!-- v-on命令用于绑定自定义事件,且可以写合法的js表达式 -->
<button v-on:click="show"></button>
<!-- v-on:可简写为@ -->
<button @click="show"></button>
<button @click="msg='欢迎学习微优异'"></button>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
msg: '欢迎学习Vue',
msg2: '<p>奥利给</p>',
mytitle: '你瞅啥'
},
methods:{
show() {
alert('瞅你咋地');
}
}
})
</script>
</body>
</html>
- Demo2:实现滚动字幕——定时器、this、箭头函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<button @click="start">开始</button>
<button @click="pause">暂停</button>
<p>{{ msg }}</p>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
msg: '我们遇到什么困难,也不要怕!',
intervalId: null
},
methods:{
start() {
if (this.intervalId != null) return
this.intervalId = setInterval( () => {
var start = this.msg.substring(0, 1)
var end = this.msg.substring(1)
this.msg = end + start
}, 100)
},
pause() {
clearInterval(this.intervalId)
this.intervalId = null
}
}
})
</script>
</body>
</html>
- Demo3:事件修饰符——stop、self、capture、prevent、once
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
<style>
.lz{background-color: #f00;width: 300px;height: 180px;}
.lyl{background-color: #0f0;height: 100px;}
.twlz{background-color: #ff0;height: 50px;}
</style>
</head>
<body>
<div id="app">
<!-- .stop表示阻止所有事件的冒泡 -->
<div class="lz" @click="dxstop">大雄
<div class="lyl" @click="jxstop">静香
<div class="twlz" @click.stop="phstop">胖虎</div>
</div>
</div>
<br>
<!-- .self表示阻止自身事件的冒泡 -->
<div class="lz" @click="dxself">大雄
<div class="lyl" @click.self="jxself">静香
<div class="twlz" @click="phself">胖虎</div>
</div>
</div>
<br>
<!-- .capture表示由外向内捕获事件 -->
<div class="lz" @click.capture="lzcapture">旅长
<div class="lyl" @click="lylcapture">李云龙</div>
</div>
<!-- .prevent表示阻止默认事件 -->
<a href="www.jnpgc.com" @click.prevent="prevent">江南皮革厂官网</a>
<!-- .once表示只触发一次事件 -->
<p @click.once="once">自宫指南</p>
<!-- .once和.prevent连用(不分顺序先后)表示只阻止一次默认事件 -->
<a href="http://www.baidu.com" @click.once.prevent="onceprevent">葵花宝典</a>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
},
methods:{
prevent() { console.log('本厂已倒闭,产品全白送') },
once() { console.log('自宫成功,不可再宫') },
onceprevent() { console.log('自宫成功,不可再宫,趁热打铁,练就神功') },
lzcapture() { console.log('李云龙!') },
lylcapture() { console.log('哟,旅长') },
phstop() { console.log('我倒看看是谁不给我胖虎面子') },
jxstop() { console.log('...') },
dxstop() { console.log('...') },
phself() { console.log('我倒看看是谁不给我胖虎面子') },
jxself() { console.log('...') },
dxself() { console.log('我大雄不出手,你胖虎要上天不成') }
}
})
</script>
</body>
</html>
- Demo4:数据双向绑定——v-model
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<p>{{ msg }}</p>
<!-- v-bind只能实现数据从m到v的单向绑定,无法实现双向绑定 -->
<input placeholder="" :value="msg" type="text" style="width: 100%;">
<!-- v-model可以实现表单元素和Model中数据的双向绑定 -->
<!-- v-model只能运用在表单元素中 -->
<input placeholder="" v-model="msg" type="text" style="width: 100%;">
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
msg: "boy♂next♂door"
},
methods:{
}
})
</script>
</body>
</html>
- Demo5:实现简单计算器——数据双向绑定练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="n1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" v-model="n2">
<button @click="calc">=</button>
<input type="text" v-model="result">
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
n1: 0,
opt: '+',
n2: 0,
result: 0
},
methods:{
calc() {
switch (this.opt) {
case '+':
this.result = parseInt(this.n1) + parseInt(this.n2)
break
case '-':
this.result = parseInt(this.n1) - parseInt(this.n2)
break
case '*':
this.result = parseInt(this.n1) * parseInt(this.n2)
break
case '/':
this.result = parseInt(this.n1) / parseInt(this.n2)
break
}
}
}
})
</script>
</body>
</html>
- Demo6:绑定样式——css类样式、style行内样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
<style>
.red{color: red;}
.thin{font-weight: 200;}
.italic{font-style: italic;}
.active{letter-spacing: 0.5em;}
</style>
</head>
<body>
<div id="app">
<h1 :class="['red', 'thin']">使用v-bind绑定class属性,给class传递一个数组</h1>
<h1 :class="['italic', 'thin', flag?'active':'']">在数组中使用三元表达式</h1>
<h1 :class="['italic', 'thin', {'active': flag}]">在数组中使用对象来代替三元表达式,提高代码的可读性</h1>
<h1 :class="classObj">给class属性传递一个对象</h1>
<h1 :style="{ color: 'green', 'font-weight': 2000 }">直接在style属性中书写样式</h1>
<h1 :style="styleObj1">给style属性传递一个对象</h1>
<h1 :style="[styleObj1, styleObj2]">给style属性传递一个数组</h1>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
flag: true,
classObj: { red: true, thin: false, italic: true, active: false },
styleObj1: { color: 'blue', 'font-weight': 2000 },
styleObj2: { 'font-style': 'italic' }
},
methods: {
}
})
</script>
</body>
</html>
- Demo7——v-for和key属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<span v-for="item in list1">{{ item }} </span>
<p v-for="(item, i) in list1">索引值为{{ i }}的元素值为{{ item }}</p>
<p v-for="user in list2">{{ user.id }},字{{ user.name }}</p>
<p v-for="(user, i) in list2">{{ i+1 }}号选手{{ user.id }},字{{ user.name }}</p>
<p v-for="(val, key, i) in daren">{{ i }} {{ key }}: {{ val }}</p>
<p v-for="count in 10">第{{ count }}次循环(从1开始迭代)</p>
<div>
<label>ID:
<input type="text" v-model="id">
</label>
<label>Name:
<input type="text" v-model="name">
</label>
<button @click="add">添加</button>
</div>
<!-- 当使用v-for出现问题时,必须为迭代的元素指定唯一的key属性,key只能是数字或字符串 -->
<p v-for="item in list" :key="item.id">
<input type="checkbox">
{{ item.name }}
</p>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
list: [
{ id: 1, name: '刘能' },
{ id: 2, name: '赵四' },
{ id: 3, name: '广坤' },
{ id: 4, name: '玉田' }
],
list1: [1, 2, 3, 4, 5, 6],
list2: [
{ id: '赵云', name: '子龙' },
{ id: '关羽', name: '云长' },
{ id: '周瑜', name: '公瑾' }
],
daren: {
id: "同凤格鸾台平章事",
level: "当朝宰辅",
name: "内史狄仁杰"
}
},
methods: {
add() {
this.list.unshift({id: this.id, name: this.name})
}
}
})
</script>
</body>
</html>
- Demo8——v-if和v-show
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/vue.js" charset="UTF-8"></script>
</head>
<body>
<div id="app">
<button @click="toggle">toggle</button>
<h3 v-if="flag">v-if每次都重新删除或创建元素,有较高的切换性能消耗,如果元素涉及到频繁的切换,最好不要使用v-if,用v-show</h3>
<h3 v-show="flag">v-show每次不会重新进行DOM的删除创建操作,只是改变了元素的display属性,有较高的初始渲染消耗,如果元素可能永远也不会显示出来被用户看到,最好不要使用v-show,用v-if</h3>
</div>
<script type="text/javascript" charset="UTF-8">
var vm = new Vue({
el: '#app',
data: {
flag: true
},
methods: {
toggle() {
this.flag = !this.flag;
}
}
})
</script>
</body>
</html>