目录
01_hello-vue.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>hello-vue</title>
<script type="text/javascript" src="../js/vue.js"></script>
<script>
Vue.config.devtools = false;
Vue.config.productionTip = false;//这两行代码可以消除插件提示
var app = null;
window.onload = function() {
app = new Vue({
el: '#sikiedu',
data: {
msg: 'Hello - Vue!'
}
});
}
</script>
</head>
<body>
<div id="sikiedu">
{{msg}}
</div>
</body>
</html>
运行结果:
02_v-model.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-model</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () => {
new Vue({
el : 'div',
data : {
username : '',
num : 123,
arr : [11, 22 ,33],
user : {
name : 'Joey',
age : 123
}
}
});
}
</script>
</head>
<body>
<div>
用户名:<input type="text" v-model="username" />
{{username}}<br />
{{num}}<br />
{{arr}}<br />
{{user}}<br />
</div>
</body>
</html>
03_v-model_1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-model</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () => {
new Vue({
el : '#sikiedu',
data : {
checked : false,
users : [],
selected : '',
msg : ''
}
});
}
</script>
</head>
<body>
<div id="sikiedu">
<div id="checkbox">
<input type="checkbox" v-model="checked" />{{checked}}
</div>
<div id="multi-checkbox">
<input type="checkbox" value="siki" v-model="users" />siki
<input type="checkbox" value="lain" v-model="users" />lain
<input type="checkbox" value="joey" v-model="users" />joey
<br />
选中的老师 : {{users}}
</div>
<div id="select">
<select v-model="selected">
<option disabled="disabled" value="">请选择</option>
<option>JavaEE</option>
<option>Unity3D</option>
<option>UE4</option>
<option>VUE</option>
</select>
<span>已选择:{{selected}}</span>
</div>
<div id="textarea">
<textarea v-model="msg"></textarea>
<p style="white-space: pre-line;">{{msg}}</p>
</div>
</div>
</body>
</html>
04_v-text.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-text</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () => {
new Vue({
el : '#sikiedu',
data : {
msg : 'siki学院<br>asfd'
},
created : function(){
alert(1);
}
});
}
</script>
<style type="text/css">
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="sikiedu">
<input type="text" v-model="msg"/>
<h2 v-pre>{{msg}}</h2> <!--还是输出{{msg}}-->
<h2 v-cloak>{{msg}}</h2>
<h2 v-text="msg"></h2> <!--如果有<br>,它不会换行,而是会输出<br>-->
<h2 v-html="msg"></h2> <!--如果有<br>,则会有换行效果-->
<h2 v-html="msg" v-once></h2>
</div>
</body>
</html>
05_v-for.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-for</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () => {
new Vue({
el : '#sikiedu',
data : {
arr : [11, 22, 33, 44],
user : {
id : '01',
name : 'joey'
},
users : [
{id : '01', name : 'joey'},
{id : '04', name : 'xxxx'},
{id : '02', name : 'siki'},
{id : '03', name : 'lain'}
]
}
});
}
</script>
</head>
<body>
<div id="sikiedu">
<ul>
<li v-for="value in arr">{{value}}</li><br />
<li v-for="(value, key, index) in user">{{index}} - {{key}} - {{value}}</li><br />
<li v-for="(value, index) in users">{{index}} - {{value.id}} - {{value.name}}</li>
</ul>
</div>
</body>
</html>
06_v-on.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-on</title>
<script type="text/javascript" src="../js/vue.js" ></script>
<script>
window.onload = () => {
let vm = new Vue({
el : '#sikiedu',
data : {
result : 0
},
methods : {
show : function(){
console.log("show");
},
add (a, b){
console.log("add");
console.log(this === vm);
this.result += a + b;
}
}
});
}
</script>
</head>
<body>
<div id="sikiedu">
<button v-on:click="show">click!</button>
<button @click="show">click!</button>
<button v-on:click="add(1, 2)">click add!</button>result : {{result}}
<button v-on:mouseenter="add(10, 20)">click mouseenter!</button>result : {{result}}
</div>
</body>
</html>
07_event.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>v-event</title>
<script type="text/javascript" src="js/vue.js" ></script>
<script>
window.onload=()=>{
var vm=new Vue({
el:'#sikiedu',
data:{
result:0,
result_once:0
},
methods:{
show(e){
console.log(e);
console.log(e.target.innerHTML);
},
add(a,b){
console.log("add");
this.result+=a+b;
},
add_once(a,b){
console.log("add_once");
this.result_once+=a+b;
},
showA(){
console.log("showA");
},
showB(){
console.log("showB");
}
}
});
}
</script>
</head>
<body>
<div id="sikiedu">
<button @click="show($event)">click A</button><br />
<button @click="add(1,2)">click add</button>result:{{result}}<br />
<button @click.once="add_once(1,2)">click.once add</button>result_once:{{result_once}}<br />
<div @click="showA()">
<div @click="showB()">
<button @click="show($event)">事件冒泡</button>
</div>
</div>
<!--阻止事件冒泡-->
<div @click="showA()">
<div @click="showB()">
<button @click.stop="show($event)">阻止事件冒泡</button>
</div>
</div>
<a href="https://www.baidu.com">链接到baidu.com</a><br />
<!--阻止事件默认行为-->
<a href="https://www.baidu.com" @click.prevent>阻止链接到baidu.com</a>
</div>
</body>
</html>
运行结果如下: