vue3基础知识点2中全部代码

167 阅读1分钟
[vue3知识点链接](https://blog.csdn.net/yangyangdt/article/details/122021026)### vue2写法```html<!DOCTYPE html><html>	<head>		<meta charset="utf-8" />		<title>vue2入门基础</title>		<script src="js/vue.js"></script>		<style type="text/css">			.red{				color: red;			}			.green{				color: green;			}			.bgyellow{				background-color: yellow;			}		</style>	</head>	<body>				<div id="app">															<p v-if="istrue==0">{{pop}}</p>			<p v-else-if="istrue==1">1.{{pop}}</p>			<p v-else></p>									<p v-show="show">2.我是v-show</p>									<template v-if="show">3.v-if 与v-show的另一个区别template</template><br />									<button @click="handleclick">4.点击v-on</button><br />			<button @click="handleclick1($event)">4.点击v-on</button><br />						5.v-for 遍历									<ul>				<li v-for="(item,index) in arr">{{item}}---{{index}}</li>			</ul>						<ul>				<li v-for="(value,key) in obj">{{value}}--{{key}}</li>			</ul>						<ul>				<li v-for="item in 4">{{item}}</li>			</ul>						<ul>				<li v-for="item in 'Yahoo'">{{item}}</li>			</ul>												<h2 :class="h2red">我是绑定红色的class</h2>							 			<h2 :class="{green:true}">我是绑定green的class</h2>									<h2 :class="[h2red,bg]">我绑定了两个class</h2>																											<h3 :class="h3arr[0]">class还可以用数组绑定</h3>									<h4 :style="{border:'1px solid blue'}">style</h4>									<h4 :style="[{border:'1px solid red'},color]" style="background-color: yellow;">style多个绑定</h4>									<div v-html="html"></div>									<a href=""></a>			<a :[attr1]='url1'>去百度</a>						<h3>{{returnfunc()}}</h3>			<h2>{{conbine}}</h2>		</div>	</body>	<script type="text/javascript">							data={			istrue:1,			pop:"我是显示的pop",			show:true,			arr:[1,2,3,4],			obj:{				name:'把卡卡',				age:'29'			},			h2red:"red",			bg:'bgyellow',			html:'<p>我是p标签</p>',			attr1:'href',			url1:'http://www.baidu.com',			ret:'我是调用的函数',			name:"小明",			age:"12",			conbine:'',			color:{				color:"green"			},			h3arr:['red','green']		}		const vm=new Vue({			el:"#app",			data:data,			methods:{				handleclick:function(e){					console.log("我是点击的对象"+e.target)				},				handleclick1:function(e){					console.log("我是点击的对象"+e.target)				},																				returnfunc:function(){					return this.ret;				}			},			created:function(){				this.conbine=this.name+this.age;			},			watch:{				name:function(name){					this.conbine=name+this.age;				}			}								})		vm.name="小白"			</script></html>```### vue3写法```html<!DOCTYPE html><html>	<head>		<meta charset="utf-8" />		<title>vue3基础</title>		<script src="js/vue.global.js"></script>		<style type="text/css">			h4{				color: red;			}			.blue{				color: blue;			}			.bgcolor{				background-color: yellow;			}					</style>	</head>	<body>		<div id="app">			<ul v-if="arr.length!=0">				<li v-for="value,index in arr0" >					<span v-if="index<=1">{{ value }}  {{ index }} </span> 					 					</li>			</ul>			<p v-else>数组为空</p>						<h4>1)vue的双向绑定的功能</h4>				<input type="text" v-model="doubleend"/>				<h3>{{doubleend}}</h3>  							<h4>2)vue中常用的指令 </h4>			<h4>2.1 v-if的用法</h4>				<p v-if="istrue==0">{{ pop }}</p>				<p v-else>我是else</p>							<h4>2.2 v-bind,==:</h4>			<img :src="imgUrl" :alt="name" width="100" height="100"  />									<h4>3)创建数组,并且在页面中遍历数据 显示数据</h4>				<li v-for="item in arr">{{ item }}</li>						<h4>4)创建对象,并且在页面中遍历对象 显示数据</h4>				<li v-for="(value,key) in obj">{{ value }}---{{ key }}</li>				{{ obj.name }}									<h4>6)通过vue给某个标签添加样式</h4>				 				<h5 :class="{blue:true}">添加蓝色</h5> 				<h3 :class="a">添加蓝色</h3>				<h5 :style="[{border:'1px solid green'},color1]" style="background-color: yellow;">{{ h5 }}</h5> 													<h4> v-on:click="handelclick()"   表示侦听点击事件</h4>				<button v-on:click="handelclick()" >点击事件</button>							<h4>this的指定</h4>				<button v-on:click="handleClick1 ()">点击我{{count}}</button> 						<ul>			    <li v-for="value,index in arr0" v-on:click="handleClick2 (index)">{{ value }} {{ index }} </li>			</ul> 											<h4>2.5 v-once:一次性插值</h4>			<h5><span v-once>This will never change: {{ once }}</span></h5>			<h4>2.6 v-html解释HTML</h4>			<p>未使用v-html:Using mustaches: {{ rawHtml }}</p>			<p>使用v-html: <span v-html="rawHtml"></span></p>					</div>	</body>	<script type="text/javascript">		const data = {			arr0:["小明","xiaobao","xiahua"],			doubleend:"balabalabal",			istrue:0,			pop:"我是if",			imgUrl:"https://ps.ssl.qhimg.com/sdmt/432_324_100/t01ecdff6694bf22e0c.webp",			name:"火烈鸟",			once:'执行一次,不改变',			rawHtml:'<i>我是斜体</i>',			arr:[1,2,3,4,5],			obj:{				name:'xiaomig',				age:'29',				message:"r87381",									},			a:["blue","bgcolor"],			blue:"blue",			bgcolor:"bgcolor",			h5:"我的背景黄色,字体蓝色,有绿色边框",			color1:{				color:"blue"			},			count:10					}				const vm = Vue.createApp({		  data() {		    return data		  },		  methods:{			handelclick(){				console.log("已点击")			},			handleClick1 (){				console.log(this)				this.count+=1						   			},			handleClick2 (num){							console.log("点击得到index"+num)																   						}		  }		}).mount('#app')										var arr=[1,2,3,4,5,6];				var arr2=arr.slice(2,4)						arr2=[3,4];				arr=arr=[1,2,3,4,5,6];														var arr1=[1,2,3,4,5,6];				var arr3=arr.splice(2,4);				arr3=[3,4,5,6]							</script></html>```

vue2写法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>vue2入门基础</title>
		<script src="js/vue.js"></script>
		<style type="text/css">
			.red{
				color: red;
			}
			.green{
				color: green;
			}
			.bgyellow{
				background-color: yellow;
			}
		</style>
	</head>
	<body>
		
		<div id="app">
			<!--1.使用vue开发的好处:体积小,数据双向绑定,有很多成熟的组件。-->
			
			<!--1.v-if:控制元素的显示与隐藏  ,(通过移除dom标签控制,用于只判断一次的(例如刚进页面时)-->
			<!--哪个为真显示哪个,三者中间不能有别的标签-->
			<p v-if="istrue==0">{{pop}}</p>
			<p v-else-if="istrue==1">1.{{pop}}</p>
			<p v-else></p>
			
			<!--2.v-show :控制元素的显示与隐藏(通过控制样式display:none)---用于频繁切换显示与隐藏。-->
			<p v-show="show">2.我是v-show</p>
			
			<!--3.v-if 与v-show的另一个区别:v-if可以用于虚拟的template标签里面,v-show不可以,因为虚拟标签无样式-->
			<template v-if="show">3.v-if 与v-show的另一个区别template</template><br />

			<!--4.v-on 简写@,用于触发点击事件等事件-->
			
			<button @click="handleclick">4.点击v-on</button><br />
			<button @click="handleclick1($event)">4.点击v-on</button><br />
			
			<!--5.v-for 遍历,v-for的item in arr==item of arr -->5.v-for 遍历
			
			<!--1).数组-->
			<ul>
				<li v-for="(item,index) in arr">{{item}}---{{index}}</li>
			</ul>
			<!--2).对象-->
			<ul>
				<li v-for="(value,key) in obj">{{value}}--{{key}}</li>
			</ul>
			<!--3) 数字遍历-->
			<ul>
				<li v-for="item in 4">{{item}}</li>
			</ul>
			<!--4)字符串遍历-->
			<ul>
				<li v-for="item in 'Yahoo'">{{item}}</li>
			</ul>

			<!--6.绑定属性v-bind: 简写: -->
			<!--1)class-->
			<!--直接写-->
			<h2 :class="h2red">我是绑定红色的class</h2>	
			
			<!--可以用对象判断是否给这个标签添加此样式--> 
			<h2 :class="{green:true}">我是绑定green的class</h2>
			
			<!--数组用来绑定多个属性-->
			<h2 :class="[h2red,bg]">我绑定了两个class</h2>
			
			<!--对于原来就有的class为big,绑定class后原来的big 依然存在-->
			<!--<h6 :class="[{red:h6true},h5class]" class="big">{{ name }}</h6>-->
			
			<!--绑定了多个class,再在后面写个class绑定,则不会执行后面的big,只执行第一个绑定-->
			<!--<h6 :class="[{red:h6true},h5class]" :class="big">{{ name }}</h6>-->
			
			<!--class还可以用数组绑定-->
			<h3 :class="h3arr[0]">class还可以用数组绑定</h3>
			
			<!--绑定style只能用对象,不可以直接写,冒号后面写字符串,属性之间用逗号-->
			<h4 :style="{border:'1px solid blue'}">style</h4>
			
			<!--绑定多个style用数组。原来存在的style也会生效-->
			<h4 :style="[{border:'1px solid red'},color]" style="background-color: yellow;">style多个绑定</h4>
			
			<!--7.v-html  data里面想写标签可以用,并且这个标签包含在div这个标签里面-->
			<div v-html="html"></div>
			
			<!--8.动态绑定属性-->
			<a href=""></a>
			<a :[attr1]='url1'>去百度</a>
			
			<h3>{{returnfunc()}}</h3>
			<h2>{{conbine}}</h2>

		</div>
	</body>
	<script type="text/javascript">
			//this在data里面代表window,在methods里面代表事件和data里面的数据
		
		data={
			istrue:1,
			pop:"我是显示的pop",
			show:true,
			arr:[1,2,3,4],
			obj:{
				name:'把卡卡',
				age:'29'
			},
			h2red:"red",
			bg:'bgyellow',
			html:'<p>我是p标签</p>',
			attr1:'href',
			url1:'http://www.baidu.com',
			ret:'我是调用的函数',
			name:"小明",
			age:"12",
			conbine:'',
			color:{
				color:"green"
			},
			h3arr:['red','green']
		}
		const vm=new Vue({
			el:"#app",
			data:data,
			methods:{
				handleclick:function(e){//this.value==e.target.value	
					console.log("我是点击的对象"+e.target)
				},
				handleclick1:function(e){//传参后e就$event	
					console.log("我是点击的对象"+e.target)
				},
				
				//methods的函数调用也可以不用事件,也能调用,把值return回去即可
				//调用直接写{{函数名()}}
				//包括created  computed等函数都直接调用  
				returnfunc:function(){
					return this.ret;
				}
			},
//			初始化
			created:function(){
				this.conbine=this.name+this.age;
			},
//			8.watch是侦听器,想侦听哪个值就写那个,,改变这个值会进watch里面,改变别的值就不进,比如说age
//			   同时可以传参进去,传的参就是这个想侦听的值
//			 watch可以用computed代替,并且一次可以同时更改两个值发生的变化return this.desc=this.name+this.age;
			watch:{
				name:function(name){
					this.conbine=name+this.age;
				}
			}
			
			
		})
		vm.name="小白"
		
	</script>
</html>


vue3写法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>vue3基础</title>
		<script src="js/vue.global.js"></script>
		<style type="text/css">
			h4{
				color: red;
			}
			.blue{
				color: blue;
			}
			.bgcolor{
				background-color: yellow;
			}
			
		</style>
	</head>
	<body>
		<div id="app">
			<ul v-if="arr.length!=0">
				<li v-for="value,index in arr0" >
					<span v-if="index<=1">{{ value }}  {{ index }} </span> 
					 	
				</li>
			</ul>
			<p v-else>数组为空</p>
			
			<h4>1)vue的双向绑定的功能</h4>
				<input type="text" v-model="doubleend"/>
				<h3>{{doubleend}}</h3>  
				
			<h4>2)vue中常用的指令 </h4>
			<h4>2.1 v-if的用法</h4>
				<p v-if="istrue==0">{{ pop }}</p>
				<p v-else>我是else</p>
				
			<h4>2.2 v-bind,==:</h4>
			<img :src="imgUrl" :alt="name" width="100" height="100"  />
			<!-- v-bind:title="'美味的'+pop" -->
			
			<h4>3)创建数组,并且在页面中遍历数据 显示数据</h4>
				<li v-for="item in arr">{{ item }}</li>
			
			<h4>4)创建对象,并且在页面中遍历对象 显示数据</h4>
				<li v-for="(value,key) in obj">{{ value }}---{{ key }}</li>
				{{ obj.name }}
			
			
			<h4>6)通过vue给某个标签添加样式</h4>
				 
				<h5 :class="{blue:true}">添加蓝色</h5> 
				<h3 :class="a">添加蓝色</h3>
				<h5 :style="[{border:'1px solid green'},color1]" style="background-color: yellow;">{{ h5 }}</h5> 
			
			
				
			<h4> v-on:click="handelclick()"   表示侦听点击事件</h4>
				<button v-on:click="handelclick()" >点击事件</button>
				
			<h4>this的指定</h4>
				<button v-on:click="handleClick1 ()">点击我{{count}}</button> 
			
			<ul>
			    <li v-for="value,index in arr0" v-on:click="handleClick2 (index)">{{ value }} {{ index }} </li>
			</ul> 
				
				
			<h4>2.5 v-once:一次性插值</h4>
			<h5><span v-once>This will never change: {{ once }}</span></h5>
			<h4>2.6 v-html解释HTML</h4>
			<p>未使用v-html:Using mustaches: {{ rawHtml }}</p>
			<p>使用v-html: <span v-html="rawHtml"></span></p>
			
		</div>
	</body>
	<script type="text/javascript">
		const data = {
			arr0:["小明","xiaobao","xiahua"],
			doubleend:"balabalabal",
			istrue:0,
			pop:"我是if",
			imgUrl:"https://ps.ssl.qhimg.com/sdmt/432_324_100/t01ecdff6694bf22e0c.webp",
			name:"火烈鸟",
			once:'执行一次,不改变',
			rawHtml:'<i>我是斜体</i>',
			arr:[1,2,3,4,5],
			obj:{
				name:'xiaomig',
				age:'29',
				message:"r87381",
						
			},
			a:["blue","bgcolor"],
			blue:"blue",
			bgcolor:"bgcolor",
			h5:"我的背景黄色,字体蓝色,有绿色边框",
			color1:{
				color:"blue"
			},
			count:10
			
		}
		
		const vm = Vue.createApp({
		  data() {
		    return data
		  },
		  methods:{
			handelclick(){
				console.log("已点击")
			},
			handleClick1 (){
				console.log(this)
				this.count+=1
						   
			},
			handleClick2 (num){
							console.log("点击得到index"+num)
							
									   
						}
		  }
		}).mount('#app')

// 注:		
		// 讲一下slice 和splice
		
		//		1.slice:  slice(index,index+1);
				var arr=[1,2,3,4,5,6];
				var arr2=arr.slice(2,4)//返回的应该是索引(2,3)之间的值
		//		返回后
				arr2=[3,4];
				arr=arr=[1,2,3,4,5,6];
				
		//		2.splice: 删除或添加  
		//		两个值:删除  splice(index,从索引处数几个)
		//		三个值:添加  splice(index,)  忘了----/一会看
				var arr1=[1,2,3,4,5,6];
				var arr3=arr.splice(2,4);//删除arr1从索引2往后数4个的值,arr1=[1,2]
				arr3=[3,4,5,6]//splice会形成新数组
		
		
		
	</script>
</html>