Vue_00000

Vue核心

介绍

中文官网地址:https://cn.vuejs.org
英文官网地址:https://vuejs.org

在官网地址可以下载vue.js文件,可以查看Vue文档。

动态构建用户界面的渐进式JavaScript框架。

Vue提供了两个版本:
开发版:vue.js
生产版:vue.min.js

Visual Studio Code官网地址:https://code.visualstudio.com/

初始Vue

在这里插入图片描述

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>
            初始Vue
        </title>

        <script type="text/javascript" src="../../../js/vue.js"></script>
        
    </head>

    <body>

    </body>

</html>

运行:
在这里插入图片描述解决:
1、安装Vue开发者工具:在官网下载安装或者将提前准备好的名称为:vue_dev_tools.crx(Chrome浏览器,Vue:version:2)的文件添加到谷歌浏览器的扩展程序中。
2、关闭生产环境提示。

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>
            初始Vue
        </title>

        <script type="text/javascript" src="../js/vue.js"></script>
        
    </head>

    <body>

        <script type="text/javascript">
            Vue.config.productionTip = false //设置为false以阻止vue在启动时生成生产提示。默认为true。
        </script>
    </body>

</html>

在控制台输入Vue
在这里插入图片描述
在控制台输入Vue.config
在这里插入图片描述

入门程序

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>
            初始Vue
        </title>

        <script type="text/javascript" src="../../../js/vue.js"></script>
        
    </head>

    <body>

        <!--准备一个容器-->
        <div id="root">
            <h1>Hello,宋江</h1>
        </div>

        <script type="text/javascript">
            Vue.config.productionTip = false //设置为false以阻止vue在启动时生成生产提示。默认为true。
        </script>
    </body>

</html>

按住Shift+F5,强制刷新浏览器。
在这里插入图片描述
在浏览器地址栏中输入:http://127.0.0.1:5500/
在这里插入图片描述
在根目录放置一个图标文件即可。

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>
            初始Vue
        </title>

        <script type="text/javascript" src="../../../js/vue.js"></script>
        
    </head>

    <body>

        <!--准备一个容器-->
        <div id="root">
            <h1>Hello,宋江。{{name}}</h1>
        </div>

        <div id="id_demo">

            <h2>Hello,哈哈,{{name+1}}, {{name1.toUpperCase()}}</h2>
        </div>

        <script type="text/javascript">
            
            Vue.config.productionTip = false //设置为false以阻止vue在启动时生成生产提示。默认为true。

            new Vue({

                el:'#root',

                data:{

                    name: '宋江'
                    
                }
                
            });

            new Vue({

                el: '#id_demo',

                data: {

                    name: 50,
                    name1: 'hello1'
                }

            });

        </script>

    </body>

</html>
<!DOCTYPE html>

<html>

	<head>

		<meta charset="UTF-8" />

		<title>初识Vue</title>

		<!-- 引入Vue -->
		<script type="text/javascript" src="../../../js/vue.js"></script>

	</head>

	<body>
		
		<!-- 
		初识Vue:
			1.想让Vue工作,就必须创建一个Vue实例,且要传入一个配置对象;
			2.root容器里的代码依然符合html规范,只不过混入了一些特殊的Vue语法;
			3.root容器里的代码被称为【Vue模板】;
			4.Vue实例和容器是一一对应的;
			5.真实开发中只有一个Vue实例,并且会配合着组件一起使用;
			6.{{xxx}}中的xxx要写js表达式,且xxx可以自动读取到data中的所有属性;
			7.一旦data中的数据发生改变,那么页面中用到该数据的地方也会自动更新;

			注意区分:js表达式 和 js代码(语句)
				
			1.表达式:一个表达式会产生一个值,可以放在任何一个需要值的地方:
				(1). a
				(2). a+b
				(3). demo(1)
				(4). x === y ? 'a' : 'b'

			2.js代码(语句)
				(1). if(){}
				(2). for(){}
		-->

		<!-- 准备好一个容器 -->
		<div id="demo">
			<h1>Hello,{{name.toUpperCase()}},{{address}}</h1>
		</div>

		<script type="text/javascript" >
			
			Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

			//创建Vue实例
			new Vue({

				el:'#demo', //el用于指定当前Vue实例为哪个容器服务,值通常为css选择器字符串。

				data:{ //data中用于存储数据,数据供el所指定的容器去使用,值我们暂时先写成一个对象。
					
					name:'hello',
					address:'北京'

				}

			})

		</script>

	</body>

</html>

模版语法

<!DOCTYPE html>

<html>

	<head>
		<meta charset="UTF-8" />

		<title>模板语法</title>
	
		<script type="text/javascript" src="../../../js/vue.js"></script>

	</head>

	<body>

		<!-- 
		Vue模板语法有2大类:
			1.插值语法:
				功能:用于解析标签体内容。
				写法:{{xxx}},xxx是js表达式,且可以直接读取到data中的所有属性。

			2.指令语法:
				功能:用于解析标签(包括:标签属性、标签体内容、绑定事件......)。
				举例:v-bind:href="xxx" 或  简写为 :href="xxx",xxx同样要写js表达式,且可以直接读取到data中的所有属性。
				备注:Vue中有很多的指令,且形式都是:v-?。

		 -->
		<div id="root">

			<h1>插值语法</h1>
			<h3>你好,{{name}}</h3>

			<hr/>

			<h1>指令语法</h1>
			<a v-bind:href="school.url.toUpperCase()" x="hello">点我去{{school.name}}学习1</a>
			<a :href="school.url" x="hello">点我去{{school.name}}学习2</a>

		</div>
		
	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false; //阻止 vue 在启动时生成生产提示。

		new Vue({

			el:'#root',

			data:{

				name:'jack',

				school:{
					name:'百度',
					url:'http://www.baidu.com',
				}
				
			}

		})

	</script>

</html>
<!OCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>

        <title>模版语法</title>

        <script type="text/javascript" src="../../../js/vue.js"></script>

    </head>

    <body>

        <div id="id_root">
            <h1>插值语法</h1>
            {{name}}

            <hr>

            <h1>指令语法</h1>
            <a v-bind:href="url">点击去百度</a> <br>
            <a :href="url">百度</a> <br>

            <a :href="school.url">点击去{{school.name}}</a>
            <a :href="school.url.toUpperCase()">点击去{{school.name}}</a>
        </div>

        <script type="text/javascript">

            Vue.config.productionTip = false;

            new Vue({

                el: '#id_root',

                data:{
                    name:'宋江',
                    url:'http://www.baidu.com',
                    school:{
                        name:'京东',
                        url:'http://www.jd.com',
                    }
                }

            });

        </script>

    </body>

</html>

数据绑定

<!DOCTYPE html>

<html>

	<head>
		<meta charset="UTF-8" />

		<title>数据绑定</title>

		<script type="text/javascript" src="../js/vue.js"></script>

	</head>

	<body>
		
		<!-- 
		Vue中有2种数据绑定方式:
			1.单向绑定(v-bind):数据只能从data流向页面。
			2.双向绑定(v-model):数据不仅能从data流向页面,还可以从页面流向data。
				
				备注:
					1.双向绑定一般都应用在表单类元素上(如:input、select等)。
					2.v-model:value 可以简写为 v-model,因为v-model默认收集的就是value值。
		 -->
		<div id="root">

			<!-- 普通写法 --> 
			单向数据绑定:<input type="text" v-bind:value="name"> <br/>
			双向数据绑定:<input type="text" v-model:value="name"> <br/> 
			
			<hr/>

			<!-- 简写 -->
			单向数据绑定:<input type="text" :value="name"> <br/>
			双向数据绑定:<input type="text" v-model="name"> <br/>

		</div>

	</body>

	<script type="text/javascript">
		
		Vue.config.productionTip = false

		new Vue({

			el:'#root',

			data:{
				name:'宋江'
			}

		})

	</script>

</html>
<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8"/>
        <title>数据绑定</title>

        <script type="text/javascript" src="../js/vue.js"></script>

    </head>

    <body>

        <div id="id_root">
            <input type="text" name="username" v-bind:value="name" /> <br>
            <input type="text" name="username" v-model:value="name" /> <br>
            <input type="text" name="username" :value="name" /> <br>
            <input type="text" name="username" v-model="name" />
        </div>

    </body>

    <script type="text/javascript">

        Vue.config.productionTip = false;

        new Vue({

            el:'#id_root',
            data:{
                name:'宋江'
            }
        });
    </script>

</html>

el与data的两种写法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>el与data的两种写法</title>
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
	</head>

	<body>
		<!-- 
		data与el的2种写法
			1.el有2种写法
				(1)new Vue时配置el属性。
				(2)先创建Vue实例,随后再通过vm.$mount('#root')指定el的值。
			2.data有2种写法
				(1)对象式
				(2)函数式
				如何选择:目前哪种写法都可以,以后学习到组件时,data必须使用函数式,否则会报错。
			3.一个重要的原则:
				由Vue管理的函数,一定不要写箭头函数,一旦写了箭头函数,this就不再是Vue实例了。
		-->
		<div id="root">
			<h1>你好,{{name}}</h1>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		//el的两种写法
		//第一种写法
		/*new Vue({
			
			el:'#root', 

			data:{
				name:'宋江'
			}

		})*/

		//第二种写法
		/*const vm = new Vue({

			data:{
				name:'宋江'
			}

		})

		console.log(vm);

		vm.$mount('#root')*/ 

		//data的两种写法
		//data的第一种写法:对象式
		/*new Vue({
			el:'#root',
			data:{
		 		name:'卢俊义'
		 	}
		})*/

		/*new Vue({
			el:'#root',
			//data的第二种写法:函数式
			data(){
		 	//console.log('@@@',this) //此处的this是Vue实例对象
		 	return{
		 		name:'吴用'
		 	}
		} 
		})*/

		//利用el的函数式写法可以设置定时器。
		//3秒钟后显示name值。
		const vm = new Vue({
			
			data:{
				name:'李逵'
			}
		})

		setTimeout(()=>{
			vm.$mount('#root')
		}, 3000)

	</script>

</html>

MVVM模型

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>理解MVVM</title>
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>

	<body>
		<!-- 
			MVVM模型
				1. M:模型(Model) :data中的数据
				2. V:视图(View) :模板代码
				3. VM:视图模型(ViewModel):Vue实例
			观察发现:
				1.data中所有的属性,最后都出现在了vm身上。
				2.vm身上所有的属性 及 Vue原型上所有属性,在Vue模板中都可以直接使用。
		-->
		<div id="root">
			
			学校名称:{{name}} <br>
			学校地址:{{address}}
			
			<hr>

			测试1:{{1+1}} <br>
			测试2:{{$options}} <br>
			测试3:{{$emit}} <br>
			测试4:{{_c}}

		</div>

	</body>

	<script type="text/javascript">
		
		Vue.config.productionTip = false;

		const vm = new Vue({

			el:'#root',

			data:{
				name:'宋江',
				address:'北京',
			}

		})

		console.log(vm)

	</script>

</html>

数据代理

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>回顾Object.defineproperty方法</title>
	</head>

	<body>
		
		<script type="text/javascript">
			
			let number = 18;

			let person = {
				name:'宋江',
				sex:'男',
			};

			Object.defineProperty(person, 'age', {
				
				value:18,
				// enumerable:true, //控制属性是否可以枚举,默认值是false
				// writable:true, //控制属性是否可以被修改,默认值是false
				// configurable:true //控制属性是否可以被删除,默认值是false

				//当有人读取person的age属性时,get函数(getter)就会被调用,且返回值就是age的值
				get(){
					console.log('有人读取age属性了')
					return number
				},

				//当有人修改person的age属性时,set函数(setter)就会被调用,且会收到修改的具体值
				set(value){
					console.log('有人修改了age属性,且值是', value)
					number = value
				}

			})

			// console.log(Object.keys(person))

			console.log(person);

		</script>

	</body>

</html>
<html>

    <head>
        <meta charset="utf-8"/>
        <title>Object.defineProperty</title>

        <script type="text/javascript" src="../js/vue.js"></script>

    </head>

    <body>

        <script type="text/javascript">
            
            Vue.config.productionTip = false;

            let number = 18;

            let person = {

                name: '宋江',
                gender: '男',
                //age: 20

            };

            Object.defineProperty(person, 'age', {
                value: 20,
                enumerable: true,
                writable: true,
                configurable: true
            })

            console.log(person);

            console.log(Object.keys(person));

            for(let key in person){
                console.log(person[key]);
            }

        </script>

    </body>

</html>
<!DOCTYPE html>

<html>

	<head>
		<meta charset="UTF-8" />
		<title>何为数据代理</title>
	</head>

	<body>

		<!-- 数据代理:通过一个对象代理对另一个对象中属性的操作(读/写)-->
		<script type="text/javascript" >

			let obj = {x:100}
			let obj2 = {y:200}

			Object.defineProperty(obj2,'x',{

				get(){
					return obj.x
				},
				
				set(value){
					obj.x = value
				}

			})

		</script>

	</body>

</html>
<!DOCTYPE html>

<html>

	<head>
		<meta charset="UTF-8" />
		<title>Vue中的数据代理</title>

		<script type="text/javascript" src="../js/vue.js"></script>

	</head>

	<body>
		<!-- 
		1.Vue中的数据代理:
			通过vm对象来代理data对象中属性的操作(读/写)
		2.Vue中数据代理的好处:
			更加方便的操作data中的数据
		3.基本原理:
			通过Object.defineProperty()把data对象中所有属性添加到vm上。
			为每一个添加到vm上的属性,都指定一个getter/setter。
			在getter/setter内部去操作(读/写)data中对应的属性。
		 -->
		<div id="root">
			<h2>学校名称:{{name}}</h2>
			<h2>学校地址:{{address}}</h2>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;
		
		const vm = new Vue({

			el:'#root',

			data:{
				name:'宋江',
				address:'合肥市'
			}

		})

	</script>

</html>

事件处理

事件的基本使用

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>事件的基本使用</title>
	
		<script type="text/javascript" src="../js/vue.js"></script>

	</head>

	<body>
		<!-- 
		事件的基本使用:
			1.使用v-on:xxx 或 @xxx 绑定事件,其中xxx是事件名;
			2.事件的回调需要配置在methods对象中,最终会在vm上;
			3.methods中配置的函数,不要用箭头函数!否则this就不是vm了;
			4.methods中配置的函数,都是被Vue所管理的函数,this的指向是vm 或 组件实例对象;
			5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以传参;
		-->
		<div id="root">

			<h2>欢迎来到{{name}}学习</h2>
			<!-- <button v-on:click="showInfo">点我提示信息</button> -->
			<button @click="showInfo1">点我提示信息1(不传参)</button>
			<button @click="showInfo2($event, 66)">点我提示信息2(传参)</button>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;

		const vm = new Vue({

			el:'#root',

			data:{
				name:'name',
			},

			methods:{

				showInfo1(event){
					// console.log(event.target.innerText)
					// console.log(this) //此处的this是vm
					alert('同学你好!')
				},

				showInfo2(event, number){
					console.log(event, number)
					// console.log(event.target.innerText)
					// console.log(this) //此处的this是vm
					alert('同学你好!!')
				}

			}

		})

	</script>

</html>

事件修饰符

prevent : 阻止事件的默认行为 event.preventDefault()
stop : 停止事件冒泡 event.stopPropagation()

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>事件修饰符</title>
		
		<script type="text/javascript" src="../js/vue.js"></script>

		<style>

			*{
				margin-top: 20px;
			}
			.demo1{
				height: 50px;
				background-color: skyblue;
			}
			.box1{
				padding: 5px;
				background-color: skyblue;
			}
			.box2{
				padding: 5px;
				background-color: orange;
			}
			.list{
				width: 200px;
				height: 200px;
				background-color: peru;
				overflow: auto;
			}
			li{
				height: 100px;
			}
		</style>

	</head>

	<body>
		<!-- 
		Vue中的事件修饰符:
			1.prevent:阻止默认事件(常用);
			2.stop:阻止事件冒泡(常用);
			3.once:事件只触发一次(常用);
			4.capture:使用事件的捕获模式;
			5.self:只有event.target是当前操作的元素时才触发事件;
			6.passive:事件的默认行为立即执行,无需等待事件回调执行完毕;
		-->
		
		<div id="root">

			<h2>欢迎来到{{name}}学习</h2>
			<!-- 阻止默认事件(常用) -->
			<a href="http://www.baidu.com" @click.prevent="showInfo">点我提示信息</a>

			<!-- 阻止事件冒泡(常用) -->
			<div class="demo1" @click="showInfo">
				<button @click.stop="showInfo">点我提示信息</button>
				<!-- 修饰符可以连续写 -->
				<!-- <a href="http://www.baidu.com" @click.prevent.stop="showInfo">点我提示信息</a> -->
			</div>

			<!-- 事件只触发一次(常用) -->
			<button @click.once="showInfo">点我提示信息</button>

			<!-- 使用事件的捕获模式 -->
			<div class="box1" @click.capture="showMsg(1)">
				div1
				<div class="box2" @click="showMsg(2)">
					div2
				</div>
			</div>

			<!-- 只有event.target是当前操作的元素时才触发事件; -->
			<div class="demo1" @click.self="showInfo">
				<button @click="showInfo">点我提示信息</button>
			</div>

			<!-- 事件的默认行为立即执行,无需等待事件回调执行完毕; -->
			<ul @wheel.passive="demo" class="list">
				<li>1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
			</ul>

		</div>
	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;

		new Vue({

			el:'#root',

			data:{
				name:'宋江'
			},

			methods:{

				showInfo(e){
					alert('同学你好!')
					// console.log(e.target)
				},

				showMsg(msg){
					console.log(msg)
				},

				demo(){
					for (let i = 0; i < 100000; i++) {
						console.log('#')
					}
					console.log('累坏了')
				}

			}

		})

	</script>
	
</html>

键盘事件

<!DOCTYPE html>

<html>

	<head>
		<meta charset="UTF-8" />
		<title>键盘事件</title>
		
		<script type="text/javascript" src="../js/vue.js"></script>

	</head>

	<body>
		<!-- 
		1.Vue中常用的按键别名:
			回车 => enter
			删除 => delete (捕获“删除”和“退格”键)
			退出 => esc
			空格 => space
			换行 => tab (特殊,必须配合keydown去使用)
			上 => up
			下 => down
			左 => left
			右 => right

		2.Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意要转为kebab-case(短横线命名)

		3.系统修饰键(用法特殊):ctrl、alt、shift、meta
			(1).配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键,事件才被触发。
			(2).配合keydown使用:正常触发事件。

		4.也可以使用keyCode去指定具体的按键(不推荐)

		5.Vue.config.keyCodes.自定义键名 = 键码,可以去定制按键别名
		-->
		<div id="root">

			<h2>欢迎来到{{name}}学习</h2>

			<input type="text" placeholder="按下回车提示输入" @keydown.huiche="showInfo">

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;

		Vue.config.keyCodes.huiche = 13 //定义了一个别名按键

		new Vue({
			el:'#root',
			data:{
				name:'宋江'
			},
			methods: {
				showInfo(e){
					// console.log(e.key,e.keyCode)
					console.log(e.target.value)
				}
			},
		})
	</script>
</html>

计算属性

<!DOCTYPE html>
<html>
	<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>{{firstName}}-{{lastName}}</span>
			<!--全名:<span>{{firstName.slice(0, 3)}}-{{lastName}}</span>-->

		</div>

	</body>

	<script type="text/javascript">
		
		Vue.config.productionTip = false;

		new Vue({

			el:'#root',

			data:{
				firstName:'宋',
				lastName:'江'
			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_methods实现</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>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;

		new Vue({

			el:'#root',

			data:{
				firstName:'宋',
				lastName:'江'
			},

			methods: {

				fullName(){

					console.log('@---fullName')
					return this.firstName + '-' + this.lastName;

				}

			},

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_计算属性实现</title>

		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
		计算属性:
				1.定义:要用的属性不存在,要通过已有属性计算得来。
				2.原理:底层借助了Objcet.defineproperty方法提供的getter和setter。
				3.get函数什么时候执行?
							(1).初次读取时会执行一次。
							(2).当依赖的数据发生改变时会被再次调用。
				4.优势:与methods实现相比,内部有缓存机制(复用),效率更高,调试方便。
				5.备注:
						1.计算属性最终会出现在vm上,直接读取使用即可。
						2.如果计算属性要被修改,那必须写set函数去响应修改,且set中要引起计算时依赖的数据发生改变。
		-->
		<div id="root">

			姓:<input type="text" v-model="firstName"> <br/>
			名:<input type="text" v-model="lastName"> <br/>
			测试:<input type="text" v-model="x"> <br/>
			全名:<span>{{fullName}}</span> <br/>
			<!-- 全名:<span>{{fullName}}</span> <br/>
			全名:<span>{{fullName}}</span> <br/>
			全名:<span>{{fullName}}</span> -->

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false;

		const vm = new Vue({

			el:'#root',

			data:{
				firstName: '宋',
				lastName: '江',
				x: '你好'
			},

			methods: {
				demo(){
					
				}
			},

			computed:{

				fullName:{

					//get有什么作用?当有人读取fullName时,get就会被调用,且返回值就作为fullName的值。
					//get什么时候调用?1.初次读取fullName时。2.所依赖的数据发生变化时。
					get(){

						console.log('get被调用了')
						// console.log(this) //此处的this是vm
						return this.firstName + '-' + this.lastName

					},

					//set什么时候调用? 当fullName被修改时。
					set(value){

						console.log('set',value)
						const arr = value.split('-')
						this.firstName = arr[0]
						this.lastName = arr[1]

					}

				}

			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<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>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		const vm = new Vue({

			el:'#root',

			data:{

				firstName:'张',
				lastName:'三',
				
			},

			computed:{

				//完整写法
				/* fullName:{
					get(){
						console.log('get被调用了')
						return this.firstName + '-' + this.lastName
					},
					set(value){
						console.log('set',value)
						const arr = value.split('-')
						this.firstName = arr[0]
						this.lastName = arr[1]
					}
				} */

				//简写
				fullName(){

					console.log('get被调用了')
					return this.firstName + '-' + this.lastName

				}

			}

		})

	</script>

</html>

监视属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例</title>
	
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<div id="root">
			
			<!--这样写不太规范-->
			今天天气很{{isHot?'炎热':'凉爽'}}

			<!--利用计算属性-->
			<h4>今天天气很{{info}}</h4>

			<!-- 绑定事件的时候:@xxx="yyy" yyy可以写一些简单的语句 -->
			<button @click="isHot = !isHot">切换天气</button> <br>

			<button @click="changeWeather">切换天气</button>

		</div>

	</body>

	<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
				}
				
			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例_监视属性</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		监视属性watch:
		1.当被监视的属性变化时, 回调函数自动调用, 进行相关操作。
		2.监视的属性必须存在,才能进行监视。
		3.监视的两种写法:
			(1).new Vue时传入watch配置。
			(2).通过vm.$watch监视。
		-->
		<div id="root">

			<h2>今天天气很{{info}}</h2>
			<button @click="changeWeather">切换天气</button>

		</div>

	</body>

	<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什么时候调用?当isHot发生改变时。
					//handler(){
					//	console.log('isHot被修改了')
					//}

					handler(newValue, oldValue){
						console.log('isHot被修改了',newValue,oldValue)
					}

				}

			}*/

		})

		vm.$watch('isHot',{
			
			immediate:true, 
			
			handler(newValue,oldValue){
				console.log('isHot被修改了',newValue,oldValue)
			}

		})

	</script>

</html>

深度监视

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例_深度监视</title>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		深度监视:
			(1).Vue中的watch默认不监测对象内部值的改变(一层)。
			(2).配置deep:true可以监测对象内部值改变(多层)。
		备注:
			(1).Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以!
			(2).使用watch时根据数据的具体结构,决定是否采用深度监视。
		 -->
		<div id="root">

			<h5>今天天气很{{info}}</h5>

			<button @click="changeWeather">切换天气</button>

			<hr/>

			<h5>a的值是:{{numbers.a}}</h5>
			<button @click="numbers.a++">点我让a+1</button>

			<h5>b的值是:{{numbers.b}}</h5>
			<button @click="numbers.b++">点我让b+1</button>

			<hr>

			<button @click="numbers = {a:6,b:8}">彻底替换掉numbers</button>
			{{numbers.c.d.e}}
			{{numbers}}

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		
		const vm = new Vue({

			el:'#root',

			data:{

				isHot:true,

				numbers:{

					a:1,
					b:1,

					c:{
						d:{
							e:8
						}
					}

				}

			},

			computed:{
				info(){
					return this.isHot ? '炎热' : '凉爽'
				}
			},

			methods: {
				changeWeather(){
					this.isHot = !this.isHot
				}
			},

			watch:{
				isHot:{
					//immediate:true, //初始化时让handler调用一下
					//handler什么时候调用?当isHot发生改变时。
					handler(newValue,oldValue){
						console.log('isHot被修改了',newValue,oldValue)
					}
				},

				//监视多级结构中某个属性的变化
				'numbers.a':{
					handler(){
						console.log('a被改变了。')
					}
				},
				'numbers.b':{
					handler(){
						console.log('b被改变了。')
					}
				},

				numbers:{

					deep:true, //监视多级结构中所有属性的变化

					handler(){
						console.log('numbers被改变了。')
					}

				}

			}

		})

	</script>

</html>

监视属性的简写形式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例_监视属性_简写</title>
	
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>
	
		<div id="root">

			<h5>今天天气很{{info}}</h5>
			<button @click="changeWeather">切换天气</button>

		</div>

	</body>

	<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调用一下
					// deep:true,//深度监视
					handler(newValue,oldValue){
						console.log('isHot被修改了',newValue,oldValue)
					}
				},*/

				//简写
				/*isHot(newValue,oldValue){
					console.log('isHot被修改了',newValue,oldValue,this)
				}*/

			}

		})

		//正常写法
		/* vm.$watch('isHot',{
			immediate:true, //初始化时让handler调用一下
			deep:true,//深度监视
			handler(newValue,oldValue){
				console.log('isHot被修改了',newValue,oldValue)
			}
		}) */

		//简写
		vm.$watch('isHot', function(newValue,oldValue){
			console.log('isHot被修改了',newValue,oldValue,this)
		})

		//下面的this是window。
		/*vm.$watch('isHot',(newValue,oldValue)=>{
			console.log('isHot被修改了',newValue,oldValue,this) 
		})*/

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_watch实现</title>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		computed和watch之间的区别:
			1.computed能完成的功能,watch都可以完成。
			2.watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作。
		
		两个重要的原则:
			1.所被Vue管理的函数,最好写成普通函数,这样this的指向才是vm 或 组件实例对象。
			2.所有不被Vue所管理的函数(定时器的回调函数、ajax的回调函数等、Promise的回调函数),最好写成箭头函数,
			  这样this的指向才是vm 或 组件实例对象。
		-->
		<div id="root">

			<h5>计算属性:{{result}}</h5>

			姓:<input type="text" v-model="firstName"> <br/>
			名:<input type="text" v-model="lastName"> <br/>
			姓名:<span>{{name}}</span> 

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		const vm = new Vue({

			el:'#root',

			data:{

				a:1,
				b:2,

				firstName:'宋',
				lastName:'江',
				name:'宋-江'

			},

			computed:{

				result(){
					return this.a + this.b
				}

			},

			watch:{

				firstName(val){
					setTimeout(()=>{
						console.log(this)
						this.name = val + '-' + this.lastName
					},3000);
				},

				lastName(val){
					this.name = this.firstName + '-' + val
				}

			}

		})

	</script>

</html>

绑定样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>绑定样式</title>
		
		<style>

			.basic{
				width: 400px;
				height: 100px;
				border: 1px solid black;
			}
			
			.happy{
				border: 4px solid red;;
				background-color: rgba(255, 255, 0, 0.644);
				background: linear-gradient(30deg,yellow,pink,orange,yellow);
			}

			.sad{
				border: 4px dashed rgb(2, 197, 2);
				background-color: gray;
			}

			.normal{
				background-color: skyblue;
			}

			.one{
				background-color: yellowgreen;
			}

			.two{
				font-size: 30px;
				text-shadow:2px 2px 10px red;
			}

			.three{
				border-radius: 20px;
			}

		</style>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		绑定样式:
			1. class样式
				写法:class="xxx" xxx可以是字符串、对象、数组。
					字符串写法适用于:类名不确定,要动态获取。
					对象写法适用于:要绑定多个样式,个数不确定,名字也不确定。
					数组写法适用于:要绑定多个样式,个数确定,名字也确定,但不确定用不用。
			
			2. style样式
				:style="{fontSize: xxx}"其中xxx是动态值。
				:style="[a,b]"其中a、b是样式对象。
		-->

		<div id="root">

			<!-- 绑定class样式--字符串写法,适用于:样式的类名不确定,需要动态指定 -->
			<div class="basic" :class="mood" @click="changeMood">{{name}}</div> <br/>

			<!-- 绑定class样式--数组写法,适用于:要绑定的样式个数不确定、名字也不确定 -->
			<div class="basic" :class="classArr">{{name}}</div> <br/>

			<!-- 绑定class样式--对象写法,适用于:要绑定的样式个数确定、名字也确定,但要动态决定用不用 -->
			<div class="basic" :class="classObj">{{name}}</div> <br/>

			<!-- 绑定style样式--对象写法 -->
			<div class="basic" :style="styleObj">{{name}}</div> <br/>

			<!-- 绑定style样式--数组写法 -->
			<div class="basic" :style="styleArr">{{name}}</div>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		
		const vm = new Vue({

			el:'#root',

			data:{

				name:'宋江',
				mood:'normal',

				classArr:['one','two','three'],

				classObj:{
					one:true,
					two:false,
				},

				styleObj:{
					fontSize: '40px',
					color:'red',
				},

				styleObj1:{
					backgroundColor:'orange'
				},

				styleArr:[
					{
						fontSize: '40px',
						color:'blue',
					},
					{
						backgroundColor:'gray'
					}

				]

			},

			methods: {

				changeMood(){
					const arr = ['happy','sad','normal']
					const index = Math.floor(Math.random()*3)
					this.mood = arr[index]
				}

			},

		})

	</script>
	
</html>

条件渲染

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>条件渲染</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		条件渲染:
		1.v-if
			写法:
				(1).v-if="表达式" 
				(2).v-else-if="表达式"
				(3).v-else="表达式"
			适用于:切换频率较低的场景。
			特点:不展示的DOM元素直接被移除。
			注意:v-if可以和:v-else-if、v-else一起使用,但要求结构完整。

		2.v-show
			写法:v-show="表达式"
			适用于:切换频率较高的场景。
			特点:不展示的DOM元素未被移除,仅仅是使用样式隐藏掉。
			
		3.备注:使用v-if时,元素可能无法获取到,而使用v-show一定可以获取到。
		-->
		<div id="root">

			<!-- 使用v-show做条件渲染 -->
			<h5 v-show="true">欢迎来到{{name}}</h5> 
			<h5 v-show="false">欢迎来到{{name}}</h5> 
			<h5 v-show="1 === 1">欢迎来到{{name}}</h5> 
			<h5 v-show="isDisplay">欢迎来到{{name}}</h5> 

			<h5>当前的n值是:{{n}}</h5>
			<button @click="n++">点我n+1</button>		

			<hr>

			<!-- 使用v-if做条件渲染 -->
			<h5 v-if="true">欢迎来到{{name}}</h5> 
			<h5 v-if="1 === 1">欢迎来到{{name}}</h5> 
			<h5 v-if="isDisplay">欢迎来到{{name}}</h5>

			<!-- v-else和v-else-if -->
			<div v-if="n === 1">Angular</div>
			<div v-else-if="n === 2">React</div>
			<div v-else-if="n === 3">Vue</div>
			<div v-else>hi</div>

			<!-- v-if与template配合使用 -->
			<template v-if="n === 1">
				<h5>你好</h5>
				<h5>卢俊义</h5>
				<h5>梁山泊</h5>
			</template>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		const vm = new Vue({
			
			el:'#root',

			data:{
				isDisplay:true,
				name:'梁山泊',
				n:0
			}
		})

	</script>

</html>

列表渲染

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>基本列表</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		v-for指令:
			1.用于展示列表数据
			2.语法:v-for="(item, index) in xxx" :key="yyy"
			3.可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)
		-->
		<div id="root">

			<!-- 遍历数组 -->
			<h5>人员列表(遍历数组)</h5>
			<ul>
				<li v-for="(person,index) of persons" :key="index">
					{{person.name}}-{{person.age}}
				</li>
			</ul>

			<!-- 遍历对象 -->
			<h5>汽车信息(遍历对象)</h5>
			<ul>
				<li v-for="(value,k) of car" :key="k">
					{{k}}-{{value}}
				</li>
			</ul>

			<!-- 遍历字符串 -->
			<h5>测试遍历字符串(用得少)</h5>
			<ul>
				<li v-for="(char,index) of str" :key="index">
					{{char}}-{{index}}
				</li>
			</ul>
			
			<!-- 遍历指定次数 -->
			<h2>测试遍历指定次数(用得少)</h2>
			<ul>
				<li v-for="(number,index) of 5" :key="index">
					{{index}}-{{number}}
				</li>
			</ul>
		</div>

		<script type="text/javascript">
			 
			Vue.config.productionTip = false
			
			new Vue({

				el:'#root',

				data:{

					persons:[
						{id:'001',name:'宋江',age:18},
						{id:'002',name:'卢俊义',age:19},
						{id:'003',name:'林冲',age:20}
					],

					car:{
						name:'奥迪',
						price:8,
						color:'黑色'
					},

					str:'hi hello'

				}

			})

		</script>

</html>

key的原理

面试题:react、vue中的key有什么作用(key的内部原理)?

1、虚拟DOM中key的作用:
     key是虚拟DOM对象的标识,当数据发生变化时,Vue会根据新数据生成新的虚拟DOM, 随后Vue进行新虚拟DOM旧虚拟DOM的差异比较,比较规则如下:
(1)旧虚拟DOM中找到了与新虚拟DOM相同的key:
①若虚拟DOM中的内容没变, 直接使用之前的真实DOM。
②若虚拟DOM中的内容变了, 则生成新的真实DOM,随后替换掉页面中之前的真实DOM。
(2)旧虚拟DOM中未找到与新虚拟DOM相同的key:
创建新的真实DOM,随后渲染到到页面。

2、用index作为key可能会引发的问题:
(1)若对数据进行:逆序添加、逆序删除等破坏顺序操作:
会产生没有必要的真实DOM更新,界面效果没问题, 但效率低。
(2)如果结构中还包含输入类的DOM:
会产生错误DOM更新,界面有问题。

3、开发中如何选择key?
(1)最好使用每条数据的唯一标识作为key, 比如id、手机号、身份证号、学号等唯一值。
(2)如果不存在对数据的逆序添加、逆序删除等破坏顺序操作,仅用于渲染列表用于展示,使用index作为key是没有问题的。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>key的原理</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>
	
		<div id="root">
			
			<!-- 遍历数组 -->
			<h5>人员列表(遍历数组)</h5>
			<button @click.once="add">添加</button>

			<ul>
				<li v-for="(person, index) of persons" :key="person.id">
					{{person.name}}-{{person.age}}
					<input type="text">
				</li>
			</ul>

		</div>

		<script type="text/javascript">

			Vue.config.productionTip = false
			
			new Vue({
				el:'#root',
				data:{
					persons:[
						{id:'001',name:'宋江',age:18},
						{id:'002',name:'卢俊义',age:19},
						{id:'003',name:'林冲',age:20}
					]
				},

				methods: {
					add(){
						const person = {id:'004',name:'武松',age:21}
						this.persons.unshift(person)
					}
				}

			})

		</script>

</html>

列表过滤

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>列表过滤</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<div id="root">

			<h5>人员列表</h5>
			<input type="text" placeholder="请输入姓名" v-model="keyInputWord">
			
			<ul>
				<li v-for="(person,index) of personsNewArray" :key="person.id">
					{{person.name}}-{{person.age}}-{{person.sex}}
				</li>
			</ul>

		</div>

		<script type="text/javascript">

			Vue.config.productionTip = false
			
			//用watch实现			
			/*new Vue({

				el:'#root',

				data:{

					keyInputWord:'',

					persons:[
						{id:'001',name:'马冬梅',age:18,sex:'女'},
						{id:'002',name:'周冬雨',age:19,sex:'女'},
						{id:'003',name:'周杰伦',age:20,sex:'男'},
						{id:'004',name:'温兆伦',age:21,sex:'男'}
					],

					personsNewArray:[]

				},

				watch:{

					keyInputWord:{

						immediate:true,

						handler(val){
							this.peronsNewArray = this.persons.filter((person)=>{
								return person.name.indexOf(val) !== -1
							})
						}

					}

				}

			})*/
						
			//用computed实现
			new Vue({

				el:'#root',

				data:{

					keyInputWord:'',

					persons:[
						{id:'001',name:'马冬梅',age:18,sex:'女'},
						{id:'002',name:'周冬雨',age:19,sex:'女'},
						{id:'003',name:'周杰伦',age:20,sex:'男'},
						{id:'004',name:'温兆伦',age:21,sex:'男'}
					]

				},

				computed:{

					personsNewArray(){

						return this.persons.filter((person)=>{
							return person.name.indexOf(this.keyInputWord) !== -1
						})

					}

				}

			})

		</script>

</html>

列表排序

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>列表排序</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>
	
		<div id="root">

			<h5>人员列表</h5>
			
			<input type="text" placeholder="请输入姓名" v-model="keyInputWord">
			
			<button @click="sortType = 2">年龄升序</button>
			<button @click="sortType = 1">年龄降序</button>
			<button @click="sortType = 0">原顺序</button>

			<ul>
				<li v-for="(person,index) of personsNewArray" :key="person.id">
					{{person.name}}-{{person.age}}-{{person.sex}}
					<input type="text">
				</li>
			</ul>

		</div>

		<script type="text/javascript">

			Vue.config.productionTip = false
			
			new Vue({

				el:'#root',

				data:{

					keyInputWord:'',
					sortType:0, //0原顺序 1降序 2升序

					persons:[
						{id:'001',name:'马冬梅',age:30,sex:'女'},
						{id:'002',name:'周冬雨',age:31,sex:'女'},
						{id:'003',name:'周杰伦',age:18,sex:'男'},
						{id:'004',name:'温兆伦',age:19,sex:'男'}
					]

				},

				computed:{

					personsNewArray(){

						const personFilterNewArray = this.persons.filter((person)=>{
							return person.name.indexOf(this.keyInputWord) !== -1
						})

						//判断是否需要排序
						if(this.sortType){
							personFilterNewArray.sort((person,person1)=>{
								return this.sortType === 1 ? person1.age-person.age : person.age-person1.age
							})
						}

						return personFilterNewArray

					}

				}

			}) 

			//#region
			let arr = [1, 3, 2, 6, 4, 5]

			arr.sort((a,b)=>{
				return b-a
			})

			for(let i=0; i<arr.length; i++){
				console.log(arr[i]);
			}
			//#endregion
			
		</script>

</html>

数据监测

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>更新时的一个问题</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<div id="root">
			
			<h5>人员列表</h5>

			<button @click="updateMei">更新马冬梅的信息</button>

			<ul>
				<li v-for="(person,index) of persons" :key="person.id">
					{{person.name}}-{{person.age}}-{{person.sex}}
				</li>
			</ul>

		</div>

		<script type="text/javascript">

			Vue.config.productionTip = false
			
			const vm = new Vue({

				el:'#root',

				data:{

					persons:[
						{id:'001',name:'马冬梅',age:30,sex:'女'},
						{id:'002',name:'周冬雨',age:31,sex:'女'},
						{id:'003',name:'周杰伦',age:18,sex:'男'},
						{id:'004',name:'温兆伦',age:19,sex:'男'}
					]

				},

				methods: {

					updateMei(){

						// this.persons[0].name = '马老师' //奏效
						// this.persons[0].age = 50 //奏效
						// this.persons[0].sex = '男' //奏效
						//this.persons[0] = {id:'001',name:'马老师',age:50,sex:'男'} //不奏效

						this.persons.splice(0,1,{id:'001',name:'马老师',age:50,sex:'男'})

					}
				}

			}) 

		</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Vue监测数据改变的原理</title>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>
	
	<body>
	
		<div id="root">
			<h5>学校名称:{{name}}</h5>
			<h5>学校地址:{{address}}</h5>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		const vm = new Vue({

			el:'#root',

			data:{

				name:'梁山泊替天行道',
				address:'梁山泊',

				student:{

					name:'宋江',

					age:{
						rAge:40,
						sAge:29,
					},

					friends:[
						{name:'卢俊义',age:35}
					]

				}

			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>

	<body>

		<script type="text/javascript" >

			let data = {
				name:'宋江',
				address:'梁山泊',
			}

			//创建一个监视的实例对象,用于监视data中属性的变化
			const obs = new Observer(data)		
			console.log(obs)	

			//准备一个vm实例对象
			let vm = {}
			vm._data = data = obs

			function Observer(obj){
				
				//汇总对象中所有的属性形成一个数组
				const keys = Object.keys(obj)

				//遍历
				keys.forEach((k)=>{

					Object.defineProperty(this,k,{

						get(){
							return obj[k]
						},

						set(val){
							console.log(`${k}被改了,我要去解析模板,生成虚拟DOM.....我要开始忙了`)
							obj[k] = val
						}

					})

				})

			}
			
		</script>

	</body>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Vue监测数据改变的原理</title>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>

	<body>
		
		<div id="root">

			<h5>学校名称:{{school.name}}</h5>
			<h5>学校地址:{{school.address}}</h5>
			<h5>校长是:{{school.leader}}</h5>

			<hr/>


			<button @click="addSex">添加一个性别属性,默认值是男</button>
			<h5>姓名:{{student.name}}</h5>
			<h5 v-if="student.sex">性别:{{student.sex}}</h5>
			<h5>年龄:真实{{student.age.rAge}},对外{{student.age.sAge}}</h5>
			
			<h6>朋友们</h6>
			<ul>
				<li v-for="(friend,index) in student.friends" :key="index">
					{{friend.name}}--{{friend.age}}
				</li>
			</ul>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		const vm = new Vue({

			el:'#root',

			data:{

				school:{
					name:'梁山好汉学校',
					address:'梁山泊',
				},

				student:{

					name:'宋江',
					age:{
						rAge:20,
						sAge:18,
					},
					friends:[
						{name:'卢俊义',age:50},
						{name:'林冲',age:51}
					]

				}

			},

			methods: {

				addSex(){
					// Vue.set(this.student,'sex','男')
					this.$set(this.student,'sex','男')
				}

			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />

		<title>Vue监测数据改变的原理_数组</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>

	<body>
	
		<div id="root">
			
			<h6>学校名称:{{school.name}}</h6>
			<h6>学校地址:{{school.address}}</h6>
			<h6>校长是:{{school.leader}}</h6>
			
			<hr/>

			<button @click="addGender">添加一个性别属性,默认值:男</button>
			<h6>姓名:{{student.name}}</h6>
			<h6 v-if="student.gender">性别:{{student.gender}}</h6>
			<h6>年龄:真实{{student.age.realAge}},对外{{student.age.speakAge}}</h6>
			<h6>爱好</h6>
			<ul>
				<li v-for="(hobby,index) in student.hobbies" :key="index">
					{{hobby}}
				</li>
			</ul>
			<h2>朋友:</h2>
			<ul>
				<li v-for="(friend,index) in student.friends" :key="index">
					{{friend.name}}--{{friend.age}}
				</li>
			</ul>

		</div>
		
	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		const vm = new Vue({

			el:'#root',

			data:{

				school:{
					name:'替天行道学校',
					address:'梁山',
				},

				student:{
					name:'宋江',
					age:{
						realAge:50,
						speakAge:18,
					},
					hobbies:['运动','美食','养生'],
					friends:[
						{name:'卢俊义',age:51},
						{name:'林冲',age:52}
					]
				}

			},

			methods: {

				addGender(){

					Vue.set(this.student,'gender','男')
					//this.$set(this.student,'sex','男')

				}
			}

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>总结数据监视</title>
		<style>
			button{
				margin-top: 10px;
			}
		</style>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!--
		Vue监视数据的原理:
			1.vue会监视data中所有层次的数据。
			2.如何监测对象中的数据?
				通过setter实现监视,且要在new Vue时就传入要监测的数据。
				(1).对象中后追加的属性,Vue默认不做响应式处理
				(2).如需给后添加的属性做响应式,请使用如下API:
					Vue.set(target,propertyName/index,value) 或 
					vm.$set(target,propertyName/index,value)
			3. 如何监测数组中的数据?
				通过包裹数组更新元素的方法实现,本质就是做了两件事:
				(1).调用原生对应的方法对数组进行更新。
				(2).重新解析模板,进而更新页面。
			4.在Vue修改数组中的某个元素一定要用如下方法:
				(1).使用这些API:push()、pop()、shift()、unshift()、splice()、sort()、reverse()
				(2).Vue.set() 或 vm.$set()
			
			特别注意:Vue.set() 和 vm.$set() 不能给vm或vm的根数据对象添加属性。
		-->
		<div id="root">
			
			<h6>姓名:{{student.name}}</h6>
			<h6>年龄:{{student.age}}</h6>
			<h6 v-if="student.gender">性别:{{student.gender}}</h6>
			<h6>爱好:</h6>
			<ul>
				<li v-for="(hobby,index) in student.hobbies" :key="index">
					{{hobby}}
				</li>
			</ul>

			<h6>朋友:</h6>
			<ul>
				<li v-for="(friend,index) in student.friends" :key="index">
					{{friend.name}}--{{friend.age}}
				</li>
			</ul>

			<hr>

			<button @click="student.age++">年龄+1岁</button> <br/>
			<button @click="addGender">添加性别属性,默认值:男</button>
			<button @click="student.gender = '女' ">修改性别</button> <br/>
			<button @click="addFriend">在列表首位添加一个朋友</button> <br/>
			<button @click="updateNameFriendFirst">修改第一个朋友的名字为:潘金莲</button> <br/>
			<button @click="addHobby">添加一个爱好</button> <br/>
			<button @click="updateHobbyFirst">修改第一个爱好为:编程</button> <br/>
			<button @click="removeHobbiesFood">过滤掉爱好中的美食</button> <br/>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		const vm = new Vue({

			el:'#root',

			data:{

				student:{

					name:'宋江',
					age:50,

					hobbies:['运动','美食','学习'],

					friends:[

						{name:'卢俊义',age:51},
						{name:'林冲',age:52}
					]

				}

			},

			methods:{

				addGender(){
					 //Vue.set(this.student,'gender','男')
					this.$set(this.student,'gender','男')
				},

				addFriend(){
					this.student.friends.unshift({name:'李逵',age:53})
				},

				updateNameFriendFirst(){
					this.student.friends[0].name = '潘金莲'
				},

				addHobby(){
					this.student.hobbies.push('爬山')
				},

				updateHobbyFirst(){
					
					//this.student.hobbies.splice(0,1,'编程')
					//Vue.set(this.student.hobbies,0,'编程')
					this.$set(this.student.hobbies,0,'编程')
				},

				removeHobbiesFood(){
					this.student.hobbies = this.student.hobbies.filter((hobby)=>{
						return hobby !== '美食'
					})
				}

			}

		})

	</script>

</html>

收集表单数据

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>收集表单数据</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>

	<body>

		<!-- 
		收集表单数据:
			若:<input type="text"/>,则v-model收集的是value值,用户输入的就是value值。
			若:<input type="radio"/>,则v-model收集的是value值,且要给标签配置value值。
			若:<input type="checkbox"/>
				1.没有配置input的value属性,那么收集的就是checked(勾选 or 未勾选,是布尔值)
				2.配置input的value属性:
					(1)v-model的初始值是非数组,那么收集的就是checked(勾选 or 未勾选,是布尔值)
					(2)v-model的初始值是数组,那么收集的就是value组成的数组。
				
		备注:v-model的三个修饰符:
			lazy:失去焦点再收集数据。
			number:输入字符串转为有效的数字。
			trim:输入首尾空格过滤。
		-->
		<div id="root">

			<form @submit.prevent="demo">

				账号:<input type="text" v-model.trim="userInformation.account"> <br/>
				密码:<input type="password" v-model="userInformation.password"> <br/>
				年龄:<input type="number" v-model.number="userInformation.age"> <br/>
				
				性别:
				<input type="radio" name="gender" v-model="userInformation.gender" value="male"><input type="radio" name="gender" v-model="userInformation.gender" value="female"><br/>
				
				爱好:
				<input type="checkbox" v-model="userInformation.hobbies" value="study"> 学习
				<input type="checkbox" v-model="userInformation.hobbies" value="game"> 打游戏
				<input type="checkbox" v-model="userInformation.hobbies" value="eat"> 吃饭 <br/>
				
				所属校区
				<select v-model="userInformation.city">
					<option value="">请选择校区</option>
					<option value="beijing">北京</option>
					<option value="shanghai">上海</option>
					<option value="shenzhen">深圳</option>
					<option value="wuhan">武汉</option>
				</select> <br/>
				
				其他信息:
				<textarea v-model.lazy="userInformation.other"></textarea> <br/>
				<input type="checkbox" v-model="userInformation.agree">阅读并接受<a href="http://www.baidu.com">《用户协议》</a>
				<button>提交</button>

			</form>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		new Vue({

			el:'#root',

			data:{

				userInformation:{

					account:'',
					password:'',

					age:18,

					gender:'female',

					hobbies:[],

					city:'beijing',

					other:'',

					agree:''

				}

			},

			methods: {

				demo(){

					console.log(JSON.stringify(this.userInformation))

				}

			}

		})

	</script>

</html>

过滤器

dayjs

从该官网地址搜索:https://www.bootcdn.cn

Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样. 如果您曾经用过 Moment.js, 那么您已经知道如何使用 Day.js

过滤器

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>过滤器</title>

		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>
		<script type="text/javascript" src="../../../reference/dayjs/00000/dayjs.min.js"></script>

	</head>

	<body>

		<!-- 
		过滤器:
			定义:对要显示的数据进行特定格式化后再显示(适用于一些简单逻辑的处理)。
			语法:
				1.注册过滤器:Vue.filter(name,callback) 或 new Vue{filters:{}}
				2.使用过滤器:{{ xxx | 过滤器名}}  或  v-bind:属性 = "xxx | 过滤器名"
			备注:
				1.过滤器也可以接收额外参数、多个过滤器也可以串联
				2.并没有改变原本的数据, 是产生新的对应的数据
		-->
		<div id="root">

			<!-- 计算属性实现 -->
			<h6>现在是:{{formatTime}}</h6>

			<!-- methods实现 -->
			<h6>现在是:{{getFormatTime()}}</h6>

			<!-- 过滤器实现 -->
			<h6>现在是:{{time | timeFormater}}</h6>
			
			<!-- 过滤器实现(传参) -->
			<h6>现在是:{{time | timeFormater('YYYY_MM_DD') | mySlice}}</h6>
			<h6 :x="message | mySlice">梁山泊</h6>

		</div>

		<div id="root1">
			<h6>{{message | mySlice}}</h6>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		//全局过滤器
		Vue.filter('mySlice',function(value){

			return value.slice(0,4)

		})
		
		new Vue({

			el:'#root',

			data:{

				time:1621561377603, //时间戳
				message:'你好,宋江,在梁山泊'

			},

			computed:{

				formatTime(){

					return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')

				}

			},

			methods:{

				getFormatTime(){

					return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')

				}

			},

			//局部过滤器
			filters:{

				timeFormater(value,str='YYYY年MM月DD日 HH:mm:ss'){
					//console.log('@',value)
					return dayjs(value).format(str)
				}

			}

		})

		new Vue({

			el:'#root1',

			data:{

				message:'hello'

			}
		})

	</script>

</html>

内置指令

v-text

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>v-text指令</title>
		
		<script type="text/javascript" src="../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>
		
		<!-- 
		我们学过的指令:
			v-bind	: 单向绑定解析表达式, 可简写为 :xxx
			v-model	: 双向数据绑定
			v-for  	: 遍历数组/对象/字符串
			v-on   	: 绑定事件监听, 可简写为@
			v-if 	: 条件渲染(动态控制节点是否存存在)
			v-else 	: 条件渲染(动态控制节点是否存在)
			v-show 	: 条件渲染 (动态控制节点是否展示)
		v-text指令:
			1.作用:向其所在的节点中渲染文本内容。
			2.与插值语法的区别:v-text会替换掉节点中的内容,{{xx}}则不会。
		-->
		<div id="root">

			<div>你好,{{name}}</div>
			<div v-text="name"></div>
			<div v-text="str"></div>

		</div>

	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false 
		
		new Vue({

			el:'#root',

			data:{
				name:'替天行道',
				str:'<h3>你好啊!</h3>'
			}

		})

	</script>

</html>

v-html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>v-html指令</title>
		
		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		v-html指令:
			1.作用:向指定节点中渲染包含html结构的内容。
			2.与插值语法的区别:
				(1).v-html会替换掉节点中所有的内容,{{xx}}则不会。
				(2).v-html可以识别html结构。
			3.严重注意:v-html有安全性问题!
				(1).在网站上动态渲染任意HTML是非常危险的,容易导致XSS攻击。
				(2).一定要在可信的内容上使用v-html,永远不要用在用户提交的内容上!
		-->
		<div id="root">

			<div>你好,{{name}}</div>
			<div v-html="str"></div>
			<div v-html="str1"></div>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		new Vue({

			el:'#root',
			
			data:{

				name:'宋江',
				str:'<h6>你好啊!</h6>',
				str1:'<a href=javascript:location.href="http://www.baidu.com?"+document.cookie>点击</a>'
			
			}

		})

	</script>

</html>

v-cloak

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>v-cloak指令</title>

		<style>

			[v-cloak]{

				display:none;

			}

		</style>
		
	</head>

	<body>
		
		<!-- 
		v-cloak指令(没有值):
		1.本质是一个特殊属性,Vue实例创建完毕并接管容器后,会删掉v-cloak属性。
		2.使用css配合v-cloak可以解决网速慢时页面展示出{{xxx}}的问题。
		-->
		<div id="root">
			<h6 v-cloak>{{name}}</h6>
		</div>

		<script type="text/javascript" src="http://localhost:8080/resource/3s/vue.js"></script>
	
	</body>
	
	<script type="text/javascript">

		console.log(1)
		Vue.config.productionTip = false
		
		new Vue({

			el:'#root',
			
			data:{
				name:'宋江'
			}

		})

	</script>

</html>

v-once

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>v-once指令</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>

	<body>

		<!-- 
		v-once指令:
			1.v-once所在节点在初次动态渲染后,就视为静态内容了。
			2.以后数据的改变不会引起v-once所在结构的更新,可以用于优化性能。
		-->
		<div id="root">

			<h6 v-once>初始化的n值是:{{n}}</h6>
			<h6>当前的n值是:{{n}}</h6>

			<button @click="n++">点我n+1</button>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		
		new Vue({

			el:'#root',

			data:{
				n:1
			}

		})

	</script>

</html>

v-pre

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>v-pre指令</title>
		
		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		v-pre指令:
			1.跳过其所在节点的编译过程。
	    	2.可利用它跳过:没有使用指令语法、没有使用插值语法的节点,会加快编译。
		-->
		<div id="root">

			<h2 v-pre>宋江</h2>
			<h2 >当前的n值是:{{n}}</h2>
			<button @click="n++">点我n+1</button>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		new Vue({

			el:'#root',

			data:{
				n:1
			}

		})

	</script>

</html>

自定义属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>

		<style>

			.demo{

				background-color: orange;

			}

		</style>

	</head>

	<body>

		<button id="btn">点我创建一个输入框</button>
		
		<script type="text/javascript" >

			const btn = document.getElementById('btn')

			btn.onclick = ()=>{
				
				const input = document.createElement('input')

				input.className = 'demo'
				input.value = 99
				input.onclick = ()=>{alert(1)}
				
				document.body.appendChild(input)

				input.focus()
				// input.parentElement.style.backgroundColor = 'skyblue'
				console.log(input.parentElement)
				
			}

		</script>

	</body>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>自定义指令</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。
		需求2:定义一个v-fbind指令,和v-bind功能类似,但可以让其所绑定的input元素默认获取焦点。
		
		自定义指令总结:
				一、定义语法:
							(1).局部指令:
										new Vue({															new Vue({
											directives:{指令名:配置对象}   或   		directives{指令名:回调函数}
										}) 																		})
							(2).全局指令:
											Vue.directive(指令名,配置对象) 或   Vue.directive(指令名,回调函数)

				二、配置对象中常用的3个回调:
							(1).bind:指令与元素成功绑定时调用。
							(2).inserted:指令所在元素被插入页面时调用。
							(3).update:指令所在模板结构被重新解析时调用。

				三、备注:
							1.指令定义时不加v-,但使用时要加v-;
							2.指令名如果是多个单词,要使用kebab-case命名方式,不要用camelCase命名。
		-->
		<div id="root">

			<h6>{{name}}</h6>
			<h6>当前的n值是:<span v-text="n"></span> </h6>

			<!-- <h2>放大10倍后的n值是:<span v-big-number="n"></span> </h2> -->
			<h6>放大10倍后的n值是:<span v-big="n"></span> </h6>

			<button @click="n++">点我n+1</button>

			<hr/>

			<input type="text" v-fbind:value="n">

		</div>

	</body>
	
	<script type="text/javascript">

		Vue.config.productionTip = false

		//定义全局指令
		/* Vue.directive('fbind',{
			//指令与元素成功绑定时(一上来)
			bind(element,binding){
				element.value = binding.value
			},
			//指令所在元素被插入页面时
			inserted(element,binding){
				element.focus()
			},
			//指令所在的模板被重新解析时
			update(element,binding){
				element.value = binding.value
			}
		}) */

		new Vue({

			el:'#root',

			data:{

				name:'宋江',
				n:1

			},

			directives:{

				//big函数何时会被调用?1.指令与元素成功绑定时(一上来)。2.指令所在的模板被重新解析时。
				/*'big-number'(element,binding){
					// console.log('big')
					element.innerText = binding.value * 10
				},*/
				big(element,binding){
					
					console.log('big',this) //注意此处的this是window
					//console.log('big')
					element.innerText = binding.value * 10

				},

				fbind:{

					//指令与元素成功绑定时(一上来)
					bind(element,binding){
						element.value = binding.value
					},

					//指令所在元素被插入页面时
					inserted(element,binding){
						element.focus()
					},
					
					//指令所在的模板被重新解析时
					update(element,binding){
						element.value = binding.value
					}

				}

			}

		})
		
	</script>

</html>

生命周期

初始化显示:

  • beforeCreate()
  • created()
  • beforeMount()
  • mounted()

更新状态: this.xxx = value

  • beforeUpdate()
  • updated()

销毁 vue 实例: vm.$destory()

  • beforeDestory()
  • destoryed()

常用的生命周期方法

  • mounted(): 发送 ajax 请求, 启动定时器等异步任务
  • beforeDestory(): 做收尾工作, 如: 清除定时器

备注:
1、模板编写没有提示。
2、没有构建过程, 无法将 ES6 转换成 ES5。
3、不支持组件的CSS。
4、真正开发中几乎不用。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>引出生命周期</title>
		
		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		生命周期:
			1.又名:生命周期回调函数、生命周期函数、生命周期钩子。
			2.是什么:Vue在关键时刻帮我们调用的一些特殊名称的函数。
			3.生命周期函数的名字不可更改,但函数的具体内容是程序员根据需求编写的。
			4.生命周期函数中的this指向是vm或组件实例对象。
		-->
		<div id="root">

			<h6 v-if="a">宋江</h6>
			<h6 :style="{opacity}">欢迎学习Vue</h6>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		
		 new Vue({

			el:'#root',

			data:{

				a:true,
				opacity:1

			},

			methods: {
				
			},

			//Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted。
			mounted(){

				console.log('mounted',this)

				setInterval(() => {

					this.opacity -= 0.01
					if(this.opacity <= 0) this.opacity = 1

				},16)

			}

		})

		//通过外部的定时器实现(不推荐)
		/*setInterval(() => {

			vm.opacity -= 0.01
			if(vm.opacity <= 0) vm.opacity = 1
		},16)*/

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>分析生命周期</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<div id="root" :x="n">

			<h2 v-text="n"></h2>

			<h2>当前的n值是:{{n}}</h2>

			<button @click="add">点我n+1</button>
			<button @click="bye">点我销毁vm</button>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		new Vue({

			el:'#root',

			// template:`
			// 	<div>
			// 		<h2>当前的n值是:{{n}}</h2>
			// 		<button @click="add">点我n+1</button>
			// 	</div>
			// `,
			data:{

				n:1

			},

			methods: {

				add(){
					console.log('add')
					this.n++
				},
				
				bye(){
					console.log('bye')
					this.$destroy()
				}
			},

			watch:{
				n(){
					console.log('n变了')
				}
			},

			beforeCreate() {
				console.log('beforeCreate')
			},

			created() {
				console.log('created')
			},

			beforeMount() {
				console.log('beforeMount')
			},

			mounted() {
				console.log('mounted')
			},

			beforeUpdate() {
				console.log('beforeUpdate')
			},

			updated() {
				console.log('updated')
			},

			beforeDestroy() {
				console.log('beforeDestroy')
			},

			destroyed() {
				console.log('destroyed')
			},

		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>引出生命周期</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		常用的生命周期钩子:
			1.mounted: 发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】。
			2.beforeDestroy: 清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】。

		关于销毁Vue实例
			1.销毁后借助Vue开发者工具看不到任何信息。
			2.销毁后自定义事件会失效,但原生DOM事件依然有效。
			3.一般不会在beforeDestroy操作数据,因为即便操作数据,也不会再触发更新流程了。
		-->
		<div id="root">

			<h6 :style="{opacity}">欢迎学习Vue</h6>

			<button @click="opacity = 1">透明度设置为1</button>
			<button @click="stop">点我停止变换</button>

		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		 new Vue({

			el:'#root',

			data:{

				opacity:1

			},

			methods: {

				stop(){

					this.$destroy()

				}

			},

			//Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted。
			mounted(){

				console.log('mounted',this)

				this.timer = setInterval(() => {

					console.log('setInterval')
					this.opacity -= 0.01
					if(this.opacity <= 0) this.opacity = 1

				},16)
			},

			beforeDestroy() {
				clearInterval(this.timer)
				console.log('vm即将驾鹤西游了')
			},
		})

	</script>

</html>

组件

非单文件组件

基本使用

<!DOCTYPE html>

<html>
	
	<head>
		<meta charset="UTF-8" />
		<title>基本使用</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>
	
	</head>

	<body>
		
		<!-- 
		Vue中使用组件的三大步骤:
			1、定义组件(创建组件)
			2、注册组件
			3、使用组件(写组件标签)

		如何定义一个组件?
			使用Vue.extend(options)创建,其中options和new Vue(options)时传入的那个options几乎一样,但也有点区别;
				区别如下:
					1.el不要写,为什么? ——— 最终所有的组件都要经过一个vm的管理,由vm中的el决定服务哪个容器。
					2.data必须写成函数,为什么? ———— 避免组件被复用时,数据存在引用关系。
					备注:使用template可以配置组件结构。

		如何注册组件?
			1.局部注册:靠new Vue的时候传入components选项
			2.全局注册:靠Vue.component('组件名',组件)

		编写组件标签:<school></school>
		-->
		<div id="root">
			
			<hello></hello>

			<hr>

			<h6>{{message}}</h6>

			<hr>

			<!-- 第三步:编写组件标签 -->
			<school></school>

			<hr>

			<!-- 第三步:编写组件标签 -->
			<student></student>

		</div>

		<div id="root1">
			<hello></hello>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false

		//第一步:创建school组件
		const school = Vue.extend({

			template:`
				<div class="demo">
					<h6>学校名称:{{schoolName}}</h6>
					<h6>学校地址:{{address}}</h6>
					<button @click="showName">点击弹出学校名称</button>	
				</div>
			`,
			// el:'#root', //组件定义时,一定不要写el配置项,因为最终所有的组件都要被一个vm管理,由vm决定服务于哪个容器。
			data(){
				return {
					schoolName:'替天行道学校',
					address:'梁山'
				}
			},
			methods: {
				showName(){
					alert(this.schoolName)
				}
			},
		})

		//第一步:创建student组件
		const student = Vue.extend({
			template:`
				<div>
					<h6>学生姓名:{{studentName}}</h6>
					<h6>学生年龄:{{age}}</h6>
				</div>
			`,
			data(){
				return {
					studentName:'宋江',
					age:50
				}
			}
		})
		
		//第一步:创建hello组件
		const hello = Vue.extend({

			template:`
				<div>	
					<h6>你好,{{name}}</h6>
				</div>
			`,

			data(){

				return {
					name:'卢俊义'
				}

			}
			
		})
		
		//第二步:全局注册组件
		Vue.component('hello',hello)

		//创建vm
		new Vue({
			el:'#root',
			data:{
				message:'love'
			},
			//第二步:注册组件(局部注册)
			components:{
				school,
				student
			}
		})

		new Vue({
			el:'#root1',
		})

	</script>

</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>几个注意点</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		1.关于组件名:
			一个单词组成:
				第一种写法(首字母小写):school
				第二种写法(首字母大写):School
			多个单词组成:
				第一种写法(kebab-case命名):my-school
				第二种写法(CamelCase命名):MySchool (需要Vue脚手架支持)
			备注:
				(1).组件名尽可能回避HTML中已有的元素名称,例如:h2、H2都不行。
				(2).可以使用name配置项指定组件在开发者工具中呈现的名字。

		2.关于组件标签:
				第一种写法:<school></school>
				第二种写法:<school/>
				备注:不用使用脚手架时,<school/>会导致后续组件不能渲染。

		3.一个简写方式:
			const school = Vue.extend(options) 可简写为:const school = options
		-->
		<div id="root">
			<h6>{{message}}</h6>
			<school></school>
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false
		
		//定义组件
		const s = Vue.extend({
			name:'school',
			template:`
				<div>
					<h6>学校名称:{{name}}</h6>	
					<h6>学校地址:{{address}}</h6>	
				</div>
			`,
			data(){
				return {
					name:'替天行道学校',
					address:'梁山'
				}

			}

		})

		new Vue({
			el:'#root',
			data:{
				message:'欢迎学习Vue!'
			},

			components:{
				school:s
			}

		})

	</script>

</html>

组件的嵌套

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>组件的嵌套</title>
	
		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<div id="root">
			
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false 

		//定义student组件
		const student = Vue.extend({

			name:'student',

			template:`
				<div>
					<h6>学生姓名:{{name}}</h6>	
					<h6>学生年龄:{{age}}</h6>	
				</div>
			`,

			data(){

				return {

					name:'宋江',
					age:50

				}

			}

		})
		
		//定义school组件
		const school = Vue.extend({

			name:'school',

			template:`
				<div>
					<h6>学校名称:{{name}}</h6>	
					<h6>学校地址:{{address}}</h6>	
					<student></student>
				</div>
			`,
			data(){

				return {
					name:'替天行道学校',
					address:'梁山'
				}

			},

			//注册组件(局部)
			components:{
				student
			}
		})

		//定义hello组件
		const hello = Vue.extend({
			template:`<h6>{{message}}</h6>`,
			data(){
				return {
					message:'欢迎学习Vue!'
				}
			}
		})
		
		//定义app组件
		const app = Vue.extend({
			template:`
				<div>	
					<hello></hello>
					<school></school>
				</div>
			`,
			components:{
				school,
				hello
			}
		})

		//创建vm
		new Vue({
			template:'<app></app>',
			el:'#root',
			//注册组件(局部)
			components:{app}
		})

	</script>

</html>

VueComponent

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>VueComponent</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		关于VueComponent:
		1.school组件本质是一个名为VueComponent的构造函数,且不是程序员定义的,是Vue.extend生成的。
		2.我们只需要写<school/>或<school></school>,Vue解析时会帮我们创建school组件的实例对象,即Vue帮我们执行:new VueComponent(options)。
		3.特别注意:每次调用Vue.extend,返回的都是一个全新的VueComponent。
		4.关于this指向:
			(1).组件配置中:data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是VueComponent实例对象。
			(2).new Vue(options)配置中:data函数、methods中的函数、watch中的函数、computed中的函数,它们的this均是Vue实例对象。
		5.VueComponent的实例对象(组件实例对象)。
		-->
		<div id="root">
			<school></school>
			<hello></hello>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		
		//定义school组件
		const school = Vue.extend({

			name:'school',

			template:`
				<div>
					<h6>学校名称:{{name}}</h6>	
					<h6>学校地址:{{address}}</h6>	
					<button @click="showName">点击弹出学校名称</button>
				</div>
			`,

			data(){
				return {
					name:'替天行道学校',
					address:'梁山'
				}
			},

			methods: {

				showName(){
				
					alert(this.name);
					console.log('showName', this)				
					
				}

			},

		})

		const test = Vue.extend({
			template:`<span>hello</span>`
		})

		//定义hello组件
		const hello = Vue.extend({
			template:`
				<div>
					<h6>{{message}}</h6>
					<test></test>	
				</div>
			`,
			data(){
				return {
					message:'hi'
				}
			},
			components:{test}
		})


		// console.log('@',school)
		// console.log('#',hello)

		//创建vm
		const vm = new Vue({
			el:'#root',
			components:{
				school,
				hello
			}
		})

	</script>

</html>

一个重要的内置关系

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>一个重要的内置关系</title>

		<script type="text/javascript" src="../../../../reference/vue/00000_v2.6.12/vue.js"></script>

	</head>

	<body>

		<!-- 
		1.一个重要的内置关系:VueComponent.prototype.__proto__ === Vue.prototype
		2.为什么要有这个关系:让组件实例对象可以访问到Vue原型上的属性、方法。
		-->
		<div id="root">
			<school></school>
		</div>

	</body>

	<script type="text/javascript">

		Vue.config.productionTip = false
		Vue.prototype.x = 99

		//定义school组件
		const school = Vue.extend({
			name:'school',
			template:`
				<div>
					<h6>学校名称:{{name}}</h6>	
					<h6>学校地址:{{address}}</h6>	
					<button @click="showX">点我输出x</button>
				</div>
			`,
			data(){
				return {
					name:'替天行道学校',
					address:'梁山'
				}
			},
			methods: {
				showX(){
					console.log(this.x)
				}
			},
		})

		//创建一个vm
		const vm = new Vue({
			el:'#root',
			data:{
				message:'hi'
			},
			components:{school}
		})

		
		//定义一个构造函数
		/* function Demo(){
			this.a = 1
			this.b = 2
		}
		//创建一个Demo的实例对象
		const d = new Demo()

		console.log(Demo.prototype) //显示原型属性

		console.log(d.__proto__) //隐式原型属性

		console.log(Demo.prototype === d.__proto__)

		//程序员通过显示原型属性操作原型对象,追加一个x属性,值为99
		Demo.prototype.x = 99

		console.log('@',d) */

	</script>
	
</html>

单文件组件

main.js

import App from './App.vue'

new Vue({

	el:'#root',

	template:`
		<App></App>
	`,

	components:{
		App
	}

})

App.vue

<template>

	<div>

		<School></School>
		<Student></Student>

	</div>

</template>

<script>

	//引入组件
	import School from './School.vue'
	import Student from './Student.vue'

	export default {

		name:'App',

		components:{

			School,
			Student
			
		}

	}

</script>

School.vue

<template>

	<div class="demo">
		<h6>学校名称:{{name}}</h6>
		<h6>学校地址:{{address}}</h6>
		<button @click="showName">点击弹出学校名称</button>	
	</div>

</template>

<script>

	 export default {

		name:'School',

		data(){

			return{
				name:'替天行道学校',
				address:'梁山'
			}

		},

		methods: {

			showName(){
				alert(this.name)
			}

		}

	}

</script>

<style>

	.demo{

		background-color: orange;

	}

</style>

Student.vue

<template>

	<div>

		<h6>学生姓名:{{name}}</h6>
		<h6>学生年龄:{{age}}</h6>

	</div>

</template>

<script>

	 export default {

		name:'Student',

		data(){

			return {

				name:'宋江',
				age:50

			}
		}

	}

</script>

index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />

		<title>单文件组件的语法</title>

	</head>

	<body>

		<div id="root"></div>

	</body>

</html>

点击继续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值