vue小例子

**1、快速入门**

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>快速入门</title>
		<script src="./vuejs-2.5.16.js"></script>
	</head>
	<body>
		<div id="app">
			{{message}}
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				message: "Hello Vue!"
			}
		});
	</script>
</html>

在这里插入图片描述

2、v-on:click
可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-on:click</title>
		<script src="./vuejs-2.5.16.js"></script>
	</head>
	<body>
		<div id="app">
			{{message}}  
			<button v-on:click="fun('这是使用vue绑定的点击事件')">vue的onclick</button>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				message:"hello world"//model
			},
			methods:{
				fun:function(msg){
					//alert(msg);
					this.message = msg;
				}
			}
		});
	</script>
</html> 

原来:
在这里插入图片描述
点击后:
在这里插入图片描述
3、v-on:keydown

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-on:keydown</title>
		<script src="./vuejs-2.5.16.js"></script>
	</head>

	<body>
		<div id="app">
			<input type="text" v-on:keydown="fun($event)">
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			methods:{
				fun:function(event){
					//alert(event.keyCode);
					var keyCode = event.keyCode;
					if(!(keyCode >= 48 && keyCode <= 57)) {
						//2.阻止默认行为执行
						event.preventDefault();
					}
				}
			}
		})
		
		function showKeyCode(){
			var code = event.keyCode;
			alert(code);
		}
	</script>

</html>

在这里插入图片描述
在这里插入图片描述

4、 v-on:mouseove

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-on:mouseover</title>
		<style>
			#div {
				background-color: red;
			}
		</style>
			<script src="./vuejs-2.5.16.js"></script>
	</head>

	<body>
		<div id="app">
			<div @mouseover="fun1" id="div">
				<textarea @mouseover="fun2($event)">这是一个文件域</textarea>
			</div>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el: "#app", //表示当前vue对象接管了div区域   
			methods:{
				fun1:function(){
					alert("div......");
				},
				fun2:function(e){
					alert("textarea......");
					e.stopPropagation();//阻止冒泡 
				}
			}
		});
	</script>

</html>

在这里插入图片描述

5、v-on:事件修饰符
在这里插入图片描述

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-on:事件修饰符</title>

		<script src="./vuejs-2.5.16.js"></script>
	</head>

	<body>
		<div id="app">
			<!-- <form action="http://www.itcast.cn" onsubmit="return checkform()">
				<input type="submit" value="Go"> 
			</form> -->
			<form  v-on:submit.prevent action="http://www.itcast.cn" >
				<input type="submit" value="Go"> 
			</form>
			
			<div v-on:click="fun">
				<a v-on:click.stop href="http://www.itcast.cn">itcast</a>
			</div>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			methods:{
				fun:function(){
					alert("haha");
				}
			}
		});
		
		/* function checkform(){
			return false;
		} */
		
	</script>

</html>

在这里插入图片描述
6、v-on:按键修饰符

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-on:按键修饰符</title>

		<script src="./vue.js"></script>
	</head>

	<body>
		<div id="app">
			<input type="text" @keydown.enter="fun"/>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			methods:{
				fun:function(){
					alert("aaa");
				}
			}
		});
	</script>

</html>

在这里插入图片描述

在这里插入图片描述
7、v-text与v-html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-text与v-html</title>
		<script src="./vue.js"></script>
	</head>
	<body>
		<div id="app">
			<div v-text="message"></div>
			<div v-html="message"></div>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				message:"<h1>Hello Vue!</h1>"
			}
		});
	</script>
</html>

在这里插入图片描述
8、v-model

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-model</title>
		<script src="./vue.js"></script>
	</head>

	<body>
		<div id="app">
			用户名:<input type="text" v-model="user.username"><br/>
			密码:<input type="text" v-model="user.password"><br/>
			<input type="button" v-on:click="fun" value="按钮">
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				user:{
					username:"test",
					password:"123"
				}
			},
			methods:{
				fun:function(){
					this.user.username="haha";
					this.user.password="1234";
				}
			}
		});
	</script>

</html>

原来:
在这里插入图片描述

点击后:
在这里插入图片描述
在这里插入图片描述

9、v-text与v-html在v-bind上的使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-text与v-html在v-bind上的使用</title>
		<script src="./vue.js"></script>
	<body>
		<div id="app">
			<font v-bind:color="rs1">hello world</font>
			<font :color="rs2">hello itcast</font>
			
			<a href="http://www.itcast.cn/index/id">itcast1</a>	
			<a v-bind="{href:'http://www.itcast.cn/index/'+id}">itcast</a>					
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				rs1:"red",
				rs2:"green",
				id:1
		
			}
		});
	</script>
</html>

在这里插入图片描述

10、v-for遍历数组

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-for遍历数组</title>
		<script src="./vue.js"></script>
	</head>
	<body>
		<div id="app">
			<ul>
				<li v-for="(value,index) in arr">{{value}} and {{index}}</li>
			</ul>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				arr:['a','b','c','d']
			}
		});
	</script>
</html>

在这里插入图片描述
11、v-for遍历对象

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-for遍历对象</title>
		<script src="./vue.js"></script>
	</head>
	<body>
		<div id="app">
			<ul>
				<li v-for="(value,key) in product">{{key}} and {{value}}</li>
			</ul>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				product:{id:1,name:"手机",price:1000}
			}
		});
	</script>
</html>

在这里插入图片描述
12、v-for遍历对象数组
数组:[]
对象:{}

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>v-for遍历对象</title>
		<script src="./vue.js"></script>
	</head>

	<body>
		<div id="app">
			<table border="1">
				<tr>
					<td>序号号</td>
					<td>编号</td>
					<td>名称</td>
					<td>价格</td>
				</tr>
				<tr v-for="(p,index) in products">
					<td>{{index+1}}</td>
					<td>{{p.id}}</td>
					<td>{{p.name}}</td>
					<td>{{p.price}}</td>
				</tr>
			</table>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el: "#app",
			data: {
				products: [{
					id: 'itcast001',
					name: "电视机",
					price: 1000
				}, {
					id: "itcast002",
					name: "洗衣机",
					price: 2000
				}] //model
			}
		});
	</script>

</html>

在这里插入图片描述
13、v-if与v-show

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>v-if与v-show</title>
		<script src="./vue.js"></script>
	</head>
	<body>
		<div id="app">
			<span v-if="flag">hello itcast</span>			
			<span v-show="flag">hello ithemai</span>
			<button @click="fun">切换</button>
		</div>
	</body>
	<script>
		//view model
		new Vue({
			el:"#app",
			data:{
				flag:true //model
			},
			methods:{
				fun:function(){
					
					this.flag=!this.flag;
				}
			}
		});
	</script>
</html>

在这里插入图片描述
点击后:

在这里插入图片描述

14、vuejs生命周期

在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>vuejs生命周期</title>
		<script src="./vue.js"></script>
	</head>

	<body>
		<div id="app">
			{{message}}
		</div>
	</body>
	<script>
		var vm = new Vue({
			el: "#app",
			data: {
				message: 'hello world'
			},
			beforeCreate: function() {
				console.log(this);
				showData('创建vue实例前', this);
			},
			created: function() {
				showData('创建vue实例后', this);
			},
			beforeMount: function() {
				showData('挂载到dom前', this);
			},
			mounted: function() {
				showData('挂载到dom后', this);
			},
			beforeUpdate: function() {
				showData('数据变化更新前', this);
			},
			updated: function() {
				showData('数据变化更新后', this);
			},
			beforeDestroy: function() {
				vm.test = "3333";
				showData('vue实例销毁前', this);
			},
			destroyed: function() {
				showData('vue实例销毁后', this);
			}
		});

		function realDom() {
			console.log('真实dom结构:' + document.getElementById('app').innerHTML);
		}

		function showData(process, obj) {
			console.log(process);
			console.log('data 数据:' + obj.message)
			console.log('挂载的对象:')
			console.log(obj.$el)
			realDom();
			console.log('------------------')
			console.log('------------------')
		}
		//vm.message = "good...";
		vm.$destroy(); 
	</script>

</html>

console:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值