Vue技术教程(2022-10-27)

第一章:Vue安装及使用

1.Vue是一套构建用户界面的渐进式JavaScript框架
特点:1.采用组件化模式,提高代码复用率,且让代码更好维护
		2.声明式编码,让编码人员无需直接操作dom时间,提高开发效率
		
Vue环境搭建:
安装方式:使用script引入安装
			使用npm+命令行去安装
1.开始使用:创建一个文件夹把vue的js文件添加进去你
2.在浏览器里面输入https://microsoftedge.microsoft.com/addons/detail/vuejs-devtools/olofadcdnkkjdfgjcmjaadnlehnnihnl
	下载Vue浏览器开发工具
3.Vue全局配置:Vue.config.productionTip=false//阻止vue在启动的时候生成提示
4.引入页面图片标签文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 1.引入Vue文件 -->
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>

<!-- 1.准备一个容器 -->
<div id="root"> 
<h1 >hello {{name}}</h1>
</div>


<script type="text/javascript" src="../VueJs/vue.js">
Vue.config.productionTip=false//阻止vue在启动的时候生成提示
// 创建员工Vue的实例:参数只有一个,类型为对象类型
const vm=new Vue({
            el:'#root',//el用于指定当前Vue实例为那个容器服务,值通常为css选择器字符串
        data:{
// data中用于存储数据,数据了所指定的容器去使用
            name:'计算机科学与技术'
}
        });

</script>
   

<!-- 想让Vue工作就必须创建员工VUe实例,且传入员工配置对象
2.root容器里面的代码依然符合HTML规范,只不过混入了一些特殊的Vue语法
3.root容器里面的代码称为Vue模板
-->
</body>
</html>
代码实例:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 1.引入Vue文件 -->
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>

<!-- 1.准备一个容器 -->
<div id="root"> 
<h1 >hello {{name},{address}}</h1>
</div>

<div>
<h1 id="root1">我家住在,{{address}}</h1>

</div>

<script type="text/javascript" >
Vue.config.productionTip=false//阻止vue在启动的时候生成提示
// 创建员工Vue的实例:参数只有一个,类型为对象类型


const vm=new Vue({
            el:'#root',//el用于指定当前Vue实例为那个容器服务,值通常为css选择器字符串
        data:{
// data中用于存储数据,数据了所指定的容器去使用
            name:'计算机科学与技术',
            address:'昆明理工'
}
 });


new Vue({
el:'#root1',
data:{
address:'北京!'

}
        })

// 注意区分JS代码和js语句
// 1.表达式:员工表达式会生成员工值,可以放在如何员工需要的地方
</script>
   

<!-- 想让Vue工作就必须创建员工VUe实例,且传入员工配置对象
2.root容器里面的代码依然符合HTML规范,只不过混入了一些特殊的Vue语法
3.root容器里面的代码称为Vue模板

<!-- 4vue实例和容器是一一对应的
5.在开发中只有一个vue实例,并且配合着组件一起使用
6.{{xx}}里面的xx要写js表达式,且xxx会自动读取
7.一旦数据改变,会自动更新

-->
-->
</body>
</html>
模板语法:指令语法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>

    <!-- 2.准备员工容器 -->
    <div id="root">
        <h1>1.插值语法!</h1>
        <h1 >我是,{{name}}</h1>
        <h1 >我是,{{url}}</h1>

        <h2>指令语法</h2>
          <a v-bind:href="www.baidu">点我去学习!</a>
          <!-- 简写 -->
          <a :href="url" :="hello">点我去学习!</a>

    </div>

    <script type="text/javascript" >
        // z阻止Vue提示
        Vue.config.productionTip=false;//阻止Vue生产出的提示
        new Vue({
                el:'#root',
        data:{
            name:'~风轻云淡~',
            url:'www.baidu'
        }
        })
        </script>
        <!-- 
因此Vue语法可以分为两类:
1.插值语法:功能:用于解析标签体里面的内容
        写法{{xxx}},xxx是js表达式,且可以直接读取到data中的属性
        2.指令语法:
        功能:用于解析标签(包括:标签属性,标签体内容,绑定事件...
        举例:v-bind:href="xxx" 或:href="xxx",xxx同样为js表达式
且可以直接读取到data中的所有属性
备注:Vue中有很多的指令,且形式都是v-????,此处我们那v-bind举个例子

         -->

</body>
</html>

数据绑定:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 1.引入Vue -->
    <script type="text/javascript" src="/VueJs/vue.js"></script>
    <title>数据绑定</title>
</head>

<body>
    
<!-- 1.准备一个容器 -->
<div id="root">
单向数据绑定:<input type="text" v-bind:value="name"><br>
双向数据绑定:<input type="text" v-model:value="name"><br>

<!--  如下代码错误,因为v-model只能应用在表单类元素(输入类元素)-->
<h2 v-model:x="name">你好!</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip=false//阻止Vue在启动的时候,生成提示
new Vue({
    el:'#root',
    data:{
        name:'~风轻云淡~'

    }

})

</script>
<!-- // vue中主要有2种绑定的方式
1.单向绑定(v-bind)数据只能从data流向页面
2.双向绑定(v-model):数据不仅能从data流向页面,还可以从页面流
向data
-->
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <!-- 1.准备员工容器 -->

    <div id="root">

        <h1 id="root">你好{{name},{name}}</h1>
    </div>

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

Vue.congig.productionTip=false//阻止Vue运行时候产生的提示
const v= new Vue({

// el:'#root',

data:{
name:'~风轻云淡~'
}

})
console.log(v);
v.$mount('#root')//第二种写法

//data的第二章写法:函数式

new Vue({

 el:'#root',


 data:function(){

return{

    name:'风轻云淡'

}

 }

})

</script>
<!-- 
data的两种写法:
1.el的两种写法:new Vue时候配置el属性  2.先创建vue实例,随后通过vm.$mount('#root')指定el的值

对象式,函数式





 -->
</html>
模板
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>
    

    <!-- 1.准备一个容器 -->
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>
    

    <!-- 1.准备一个容器 -->
    <div id="root">
<h1>学校名称: {{name}}</h1>
<h1>学校地址:{{address}}</h1>

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

Vue.config.productionTip=false//阻止vue在启动时产生提示
const vm=new Vue({
el:'#root',
data:{
name:'风轻云淡',
address:'云南'

}
})
</script>
<!-- 
MVVM模型:
1.M(model) data中的数据
V:视图view:模板代码
VM:视图模型:vue实例
<!-- 观察发现:data中的使用属性,最后都出现在vm身上 -->
 -->
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>
    

    <!-- 1.准备一个容器 -->
    <div id="root">
<h1>学校名称: {{name}}</h1>
<h1>学校地址:{{address}}</h1>

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

Vue.config.productionTip=false//阻止vue在启动时产生提示
const vm=new Vue({
el:'#root',
data:{
name:'风轻云淡',
address:'云南'

}
})
</script>
<!-- 
MVVM模型:
1.M(model) data中的数据
V:视图view:模板代码
VM:视图模型:vue实例
<!-- 观察发现:data中的使用属性,最后都出现在vm身上 -->
 -->
</html>

第二章:数据代理

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript">
        let number=10;
        let person={

                name:'张三',
                sex:'男',
                age:number

        }

        console.log(person);
Object.defineProperty(person,'age',{
                    
// 单有人读取Person的age属性时,get函数就会倍调用,返回值就是age的值
            get:function(){
                console.log("有人更改了属性");
return number;
},

// 当有人修改Person的age属性时,set函数就会被调用,返回值就是age的值,且函数返回值的是修改的具体值
            set(value){
console.log(value);


            }

})
console.log(person);
</script>
</head>
<body>
    <!-- 数据代理:通过一个对象代理对另一个对象中属性的操作  读/写-->
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script type="text/javascript">

            let obj1={x:100}
            let obj2={y:100}
            Object.defineProperty(obj2,'x',{

                get(){

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

                }
            })
    </script>
    <title>Document</title>
</head>
<body>
   
</body>
</html>

Vue中的数据代理:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascriptt" src="../VueJs/vue.js"></script>

    <title>Document</title>
    
</head>
<body>
    <!-- 1.准备一个容器 -->
    <div id="root">
        <h1>学校名称: {{name}}</h1>
        <h1>学校地址:   {{address}}</h1>
        
            </div>
            <!-- 
1.Vue中的数据代理:通过vm对象来代理data对象中的属性的操作(读写)
2.vue中关系数据的好处
更加方便的操作data中的数据
3.基本原理:通过Object。defineProperty()把data对象中所有属性都添加到vm上
为每一个添加到vm上的属性,都指定一公getter/setter
             -->
    
</body>
<script type="text/javascript">

    Vue.config.productionTip=false//阻止vue在启动时产生提示
    const vm=new Vue({
    el:'#root',
    data:{
    name:'风轻云淡',
    address:'云南'
    
    }
    })
    </script>
</html>

第三章:事件的处理

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <!-- 
1.事件的基本使用:
    1使用v-on:xxx 或@xxx绑定事件,其中xxx是事件名称
        2   事件的回调需要配置在methods对象中,最终会在vm上
            3.methods中配置的函数,不能用箭头函数,否则this就不是vml
            4.methods中配置的函数,都是被vue所管理的函数,this的指向是vm或组件实例对象
            5.@click=“demo”和@clink="demo($event)"效果一直,但是后者可以传参
     -->

    <!-- 1.准备一个容器 -->
    <div id="root">
<h1>学校名称: {{name}}</h1>
<h1>学校地址:{{address}}</h1>

<!-- 在Vue中绑定事件使用v-on -->
<button v-on:click="showInfo">点我提示(不传参)</button>
<button @click="showInfo2(78,$event)">点我提示(传参)</button>
    </div>
</body>
<script type="text/javascript">

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

                const vm=new Vue({
                el:'#root',
                data:{
                name:'风轻云淡',
                address:'云南'
                },
                        methods:{
                        showInfo(event){
                        alert("同学你好!");
                        // console.log(event);
                        console.log(this)//此处的this是vm
                        },
                showInfo2(number,event){
                alert("同学你好!");
                console.log(number,event);
                // console.log(event);
                // console.log(this)//此处的this是vm
                }

                }
})
</script>

 -->
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../VueJs/vue.js"></script>
    <title>Document</title>
    <style>
        .demo{
height: 250px;
width: 100px;
background-color: aquamarine;
        }

div{

height: 1000px;
width: 2000px;
background-color: aqua;

}
    </style>
</head>
<body>
    <div id="root">
        <!-- 1.prevent 阻止事件
            2.stop阻止事件冒泡
            3.once 事件只触发一次
            4.capture 使用事件的捕获方式
            5.self 只有event.target是当前操作元素才是触发事件
        
        -->

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

        <!-- 给这个提示框绑定一个事件 ,并且调用事件修饰符-->
<a href="www.baidu.com" @click.prevent="showInfo">点我</a>


<!-- 2.阻止事件,冒泡 -->

    </div>
<div class="demo" @clink="showInfo"> 

    <button @clink="showInfo">你</button>
</div>

</body>

<script type="text/javascript">

Vue.config.productionTip=false//阻止Vue启动提示

new Vue({

el:'#root',
data:{

name:'计算机科学与技术学院'

},
methods:{

    showInfo(e){
        e.preventDefault()

alert("你好!")

    }
}

})


</script>

</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 1.引入vue文件 -->
    <script type="text/javascript" src="./vuejs/vue.min.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="root">
        <!-- 1.绑定一个键盘事件 -->

<h2>欢迎来到{{name}}学习!</h2>
<!-- 例子: -->
<!-- <input type="text" placeholder="按下回车键输入!" @keyup.enter="showInfo"> -->

<!-- <input type="text" placeholder="按下回车键输入!" @keyup.delete="showInfo"> -->
<!-- <input type="text" placeholder="按下回车键输入!" @keyup.tab="showInfo"> -->

<input type="text" placeholder="按下回车键输入!" @keydown.enter="showInfo">
<!-- 

                   1. vue中常用的按键别名:
                    回车==>enter
                    删除==>delete
                    退出===>esc
                    换行==>tab(这个比较特殊,需要搭配keydown使用)
                    空格==>space
                    上==>up
                    下==>down
                    左==>left
                    右==>right

                    2.vue为提供别名的按键,可以使用按键原始的key去绑定,但是要注意,要直接转换为kebab-case(短横线命名)
            3.系统修饰键:语法特殊:ctrl  alt shift meta
        配合:keyup使用:按下修饰键的同时,在按下其他键,随后释放其他键,事件才能被触发
        配合keydown使用:正常触发事件


4.也可以使用keyCode去找到具体按键(不推荐)

5.vue.config.keyCodes。自定义命名=键码 可以去地址按键别名
 -->
    </div>
</body>

<script type="text/javascript">

Vue.config.productionTip=false
Vue.config.keyCodes.huiche=13//这个就是定义一个回车键,编码是13
new Vue({

el:'#root',
data:{
        name:'计算机科学与技术学院'
},
methods:{

showInfo(e){

    console.log(e.target.value)
    console.log("我换行了!")
}

}

})

</script>
</html>

第四章:计算属性与监视

方法一:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./vuejs/vue.min.js"></script>
    <title>Document</title>
</head>
<body>
                    
                    <div id="root">
                姓:<input type="text" v-model="firstName"><br>
                名:<input type="text" v-model="lastName"><br>
                姓名:      <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 lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./vuejs/vue.min.js"></script>
    <title>Document</title>
</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(){

        return this.firstName+'-' + this.lastName

    }

},


})
</script>
</html>
计算属性:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./vuejs/vue.min.js"></script>
    <title>Document</title>
</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(){
                        // get的作用:当有人读取fullname的时候,get就会被调用,且返回值就是fullName的值
                        console.log(this)//此处的tihis就是vm
                        console.log("计算机科学与技术")
                        return this.firstName+this.lastName
                        // get说明时候被调用:1.初次调用的时候  2.所依赖的数据发生变化时
                                },
                                // set说明时候被调用:当fullname被修改的时候
                                set(value){
                                    return this.firstName+this.lastName

                                }
                                


                            }

                        }

})
</script>
<!-- 

计算属性:1.定义:要用的属性不存早,要通过已有的属性计算得来
2.原理:底层借助了Object.defineproperty方法提供的getter和setter
3.get函数说明时执行:1.初次读取会调用一次 2.当依赖属性的数据发生该变的时候会再次被调用

 -->
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./vuejs/vue.min.js"></script>
    <title>Document</title>
</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:'三'

                        },
                        // 计算属性
                    //    简写:把get书写为一个函数来调用
                       
computed:{fullName:function(){
// 只有不烤炉修改的时候才可以使用简写属性
console.log("计算机")
return this.lastName+this.lastName

                        }
}
                        
})
</script>


 -->
</html>
Vue插件提示代码:Vues 3 script


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

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

    <!-- 绑定事件的时候: @xxx=yyyy yyyy可以写一些执行的语句 -->

    <div id="root">

    <h2>具体天气很{{info }},{{x}}</h2>

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

<script type/javascript>

Vue.config.productionTip=false


new Vue({
el:'#root',
data:{

isHot:true,
x:1

},
computed:{

// 书写info计算属性
info(){

    return this.isHot ?'炎热' : '凉爽'
}

},
// 绑定按钮切换事件
methods: {
    changeWeather(){

this.isHot=!this.isHot,
this.x++

    }
},


})


</script>
</html>
监视属性:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

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

    <!-- 绑定事件的时候: @xxx=yyyy yyyy可以写一些执行的语句 -->

    <div id="root">

    <h2>具体天气很{{info }},{{x}}</h2>

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

<script type/javascript>

Vue.config.productionTip=false


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

isHot:true,
x:1

},
computed:{

// 书写info计算属性
info(){

    return this.isHot ?'炎热' : '凉爽'
}

},
// 绑定按钮切换事件
methods: {
    changeWeather(){

this.isHot=!this.isHot,
this.x++

    }
},



// 书写监视属性
// 方法一:
// watch:{
//         isHot:{
//             // 使用一个函数:当ishot方式改变的时候去执行
//             // 配置:初始化的时候调用一下:  immediate:true,
//             immediate:true,
        
//                 handler(newValue,oldValue){

//                     console.log("被修改了!",newValue,oldValue)
//                 }


//         }
// }
})
// 方法二:
vm.$watch('isHot',{

    immediate:true,
        
        handler(newValue,oldValue){

            console.log("被修改了!",newValue,oldValue)
        }


})
</script>


<!-- 监视属性:watch1.当被监视的属性变化时,回调函数会自动调用买家秀相关操作
2.具体属性必须在,才能进行配置
3.具体属性的两种方法:new Vue时插入的watch配置

        2.通过vm.$watch
-->


</html>


深度监视:deep

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

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

    <!-- 绑定事件的时候: @xxx=yyyy yyyy可以写一些执行的语句 -->

    <div id="root">

    <h2>具体天气很{{info }}</h2>

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

    <h3>a的值是{{number.a}}</h3>
    <button @click="number.a++">点我a+1</button>
    </div>
    
</body>

<script type/javascript>

Vue.config.productionTip=false

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

isHot:true,
number:{
a:1,
b:1
}
},
computed:{

// 书写info计算属性
info(){

    return this.isHot ?'炎热' : '凉爽'
}

},
// 绑定按钮切换事件
methods: {
    changeWeather(){

this.isHot=!this.isHot,
this.x++

    }
},



// 书写监视属性
// 方法一:
 watch:{
         isHot:{            // 使用一个函数:当ishot方式改变的时候去执行
             // 配置:初始化的时候调用一下:  immediate:true,
            //  immediate:true,
        
            //      handler(newValue,oldValue){

            //          console.log("被修改了!",newValue,oldValue)
            //      },
                 number:{
                    //  监视多级结构中石油属性的变化
                deep:true,
                handler(){

                        console.log("改变了")

                }
                  
            }
         }
        }
        
})
// 方法二:

</script>


<!-- 总结:1.vue中的watch默认不监听对象内部值的改变
2.配置deep:true 可以检测对象内部值的改变
备注:vue自身可以检测对象内部值的改变,但是vue提供的watch默认不可以
2.watch时根据数据的具体结果,决定是否采用深度监测

-->


</html>

第八章:计算属性

​ 1.



<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_插值语法实现</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="root">
			姓:<input type="text" v-model="firstName"> <br/><br/>
			名:<input type="text" v-model="lastName"> <br/><br/>
			全名:<span>{{firstName}}-{{lastName}}</span>
		</div>
	</body>

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

		new Vue({
			el:'#root',
			data:{
				firstName:'张',
				lastName:'三'
			}
		})
	</script>
</html>



<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_methods实现</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="root">
			姓:<input type="text" v-model="firstName"> <br/><br/>
			名:<input type="text" v-model="lastName"> <br/><br/>
			全名:<span>{{fullName()}}</span>
		</div>
	</body>

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

		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>
		<!-- 引入Vue -->
		<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/><br/>
			名:<input type="text" v-model="lastName"> <br/><br/>
			测试:<input type="text" v-model="x"> <br/><br/>
			全名:<span>{{fullName}}</span> <br/><br/>
			<!-- 全名:<span>{{fullName}}</span> <br/><br/>
			全名:<span>{{fullName}}</span> <br/><br/>
			全名:<span>{{fullName}}</span> -->
		</div>
	</body>

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

		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>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="root">
			姓:<input type="text" v-model="firstName"> <br/><br/>
			名:<input type="text" v-model="lastName"> <br/><br/>
			全名:<span>{{fullName}}</span> <br/><br/>
		</div>
	</body>

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

		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>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="root">
			<h2>今天天气很{{info}}</h2>
			<!-- 绑定事件的时候:@xxx="yyy" yyy可以写一些简单的语句 -->
			<!-- <button @click="isHot = !isHot">切换天气</button> -->
			<button @click="changeWeather">切换天气</button>
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
		
		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>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/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 //阻止 vue 在启动时生成生产提示。
		
		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(newValue,oldValue){
						console.log('isHot被修改了',newValue,oldValue)
					}
				}
			} */
		})

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


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例_深度监视</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
				深度监视:
						(1).Vue中的watch默认不监测对象内部值的改变(一层)。
						(2).配置deep:true可以监测对象内部值改变(多层)。
				备注:
						(1).Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以!
						(2).使用watch时根据数据的具体结构,决定是否采用深度监视。
		 -->
		<!-- 准备好一个容器-->
		<div id="root">
			<h2>今天天气很{{info}}</h2>
			<button @click="changeWeather">切换天气</button>
			<hr/>
			<h3>a的值是:{{numbers.a}}</h3>
			<button @click="numbers.a++">点我让a+1</button>
			<h3>b的值是:{{numbers.b}}</h3>
			<button @click="numbers.b++">点我让b+1</button>
			<button @click="numbers = {a:666,b:888}">彻底替换掉numbers</button>
			{{numbers.c.d.e}}
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
		
		const vm = new Vue({
			el:'#root',
			data:{
				isHot:true,
				numbers:{
					a:1,
					b:1,
					c:{
						d:{
							e:100
						}
					}
				}
			},
			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:{
					deep:true,
					handler(){
						console.log('numbers改变了')
					}
				}
			}
		})

	</script>
</html>


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>天气案例_监视属性_简写</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 准备好一个容器-->
		<div id="root">
			<h2>今天天气很{{info}}</h2>
			<button @click="changeWeather">切换天气</button>
		</div>
	</body>

	<script type="text/javascript">
		Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
		
		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',(newValue,oldValue)=>{
			console.log('isHot被修改了',newValue,oldValue,this)
		}) */

	</script>
</html>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>姓名案例_watch实现</title>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/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">
			姓:<input type="text" v-model="firstName"> <br/><br/>
			名:<input type="text" v-model="lastName"> <br/><br/>
			全名:<span>{{fullName}}</span> <br/><br/>
		</div>
	</body>

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

		const vm = new Vue({
			el:'#root',
			data:{
				firstName:'张',
				lastName:'三',
				fullName:'张-三'
			},
			watch:{
				firstName(val){
					setTimeout(()=>{
						console.log(this)
						this.fullName = val + '-' + this.lastName
					},1000);
				},
				lastName(val){
					this.fullName = 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;
			}

			.atguigu1{
				background-color: yellowgreen;
			}
			.atguigu2{
				font-size: 30px;
				text-shadow:2px 2px 10px red;
			}
			.atguigu3{
				border-radius: 20px;
			}
		</style>
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
			绑定样式:
					1. class样式
								写法:class="xxx" xxx可以是字符串、对象、数组。
										字符串写法适用于:类名不确定,要动态获取。
										对象写法适用于:要绑定多个样式,个数不确定,名字也不确定。
										数组写法适用于:要绑定多个样式,个数确定,名字也确定,但不确定用不用。
					2. style样式
								:style="{fontSize: xxx}"其中xxx是动态值。
								:style="[a,b]"其中a、b是样式对象。
		-->
		<!-- 准备好一个容器-->
		<div id="root">
			<!-- 1.绑定class样式--字符串写法,适用于:样式的类名不确定,需要动态指定,一次就是要了如下的插值语法    绑定了点击事件-->
			<div class="basic" :class="mood" @click="changeMood">{{name}}</div> <br/><br/>





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



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



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


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

	<script type="text/javascript">
		Vue.config.productionTip = false
		//创建一个vue实例
		const vm = new Vue({
			el:'#root',
			data:{
				name:'计算机科学与技术学院',
				mood:'normal',
				classArr:['atguigu1','atguigu2','atguigu3'],
				classObj:{
					atguigu1:false,
					atguigu2:false,
			},


				styleObj:{
					fontSize: '40px',
					color:'red',
				},
				styleObj2:{
					backgroundColor:'orange'
				},
				styleArr:[
					{
						fontSize: '40px',
						color:'blue',
					},
					{
						backgroundColor:'gray'
					}
				]
			},
			methods:{

				changeMood(){

					// document.getElementById('demo').className='basic happy'
// this.mood='happy'
//				动态使用样式,使用数组来
					const arr = ['happy','sad','normal']
					const index = Math.floor(Math.random()*3)
					this.mood = arr[index]


				}

			}

			/*el:'#root',
			data:{
				name:'尚硅谷',
				mood:'normal',
				classArr:['atguigu1','atguigu2','atguigu3'],
				classObj:{
					atguigu1:false,
					atguigu2:false,
				},
				styleObj:{
					fontSize: '40px',
					color:'red',
				},
				styleObj2:{
					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="../js/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">
<!--			<h2>欢迎来到:{{n}}</h2>-->
			<h2>欢迎来到:{{name}}</h2>
<!--			<button @click="n++">点我n+1</button>-->


			


<!--			使用v-show 做为条件渲染-->
			<h1 v-show="false">欢迎来到:{{name}}</h1>
<!--			表达式一:-->
<h3 v-show="1===1">欢迎来到:{{name}}</h3>

<!--使用v-if来做条件渲染:注意这个不到万不得已不建议-->
<h4 v-if="false">欢迎来到:{{name}},计算机学院</h4>


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




			<!--以下就是通过满足指定条件来规定要显示的东西-->
<!--例如:当点击按钮 当n变为1的时候就显示 计算机科学与技术1班
以此类推
-->
			<div v-show="n===1">计算机科学与技术1班</div>
			<div v-show="n===2">计算机科学院与技术2班</div>
			<div v-show="n===9">计算机科学与技术3班</div>


<!--方式二:如果要使用else 那么div就不能有断开,必须连着-->
<div v-if="n===3">计算机科学与技术</div>
<div v-else-if="n===5">我是五</div>
<div v-else-if="n===7">我是七</div>

<!--因此条件渲染顾名思义:就是设置一个条件,当满足这个条件爱你的时候,就会出现特定的画面-->

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

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

			<!-- 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>哈哈</div> -->

			<!-- v-if与template的配合使用 -->
			<template v-if="n === 1">
				<h2>你好</h2>
				<h2>计算机科学与技术学院</h2>
				<h2>北京</h2>
			</template>

		</div>
	</body>

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

		const vm = new Vue({
			el:'#root',
			data:{
				name:'计算机科学与技术学院',
				n:0
			}
		})
	</script>
</html>

第十二章:列表渲染

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>基本列表</title>
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!--
				v-for指令:
						1.用于展示列表数据
						2.语法:v-for="(item, index) in xxx" :key="yyy"
						3.可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)
		-->
		<!-- 准备好一个容器-->
		<div id="root">
			<h2>人员列表</h2>
			<ul>
				<li v-for="(p,index) in persons" :key="index">
					{{p.name}}---->{{p.age}} {{index}}

				</li>

			</ul>
<!--这个方法可以用来遍历对象-->
			<h3>汽车信息</h3>
			<ul>
				<li v-for="(value,k) of car ":key="k">
					{{k}}-{{value}}

				</li>

			</ul>





		</div>

<!--		<div id="root">-->
<!--			&lt;!&ndash; 遍历数组 &ndash;&gt;-->
<!--			<h2>人员列表(遍历数组)</h2>-->
<!--			<ul>-->
<!--				<li v-for="(p,index) of persons" :key="index">-->
<!--					{{p.name}}-{{p.age}}-->
<!--				</li>-->
<!--			</ul>-->

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

<!--			&lt;!&ndash; 遍历字符串 &ndash;&gt;-->
<!--			<h2>测试遍历字符串(用得少)</h2>-->
<!--			<ul>-->
<!--				<li v-for="(char,index) of str" :key="index">-->
<!--					{{char}}-{{index}}-->
<!--				</li>-->
<!--			</ul>-->
<!--			-->
<!--			&lt;!&ndash; 遍历指定次数 &ndash;&gt;-->
<!--			<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:'奥迪A8',
						price:'70万',
						color:'黑色'
					},
					str:'hello'
				}
			})
		</script>
	</body>
</html>


key作用与原理:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title>key的原理</title>
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>

		<!-- 
				面试题:react、vue中的key有什么作用?(key的内部原理)
						
						1. 虚拟DOM中key的作用:
										key是虚拟DOM对象的标识,当数据发生变化时,Vue会根据【新数据】生成【新的虚拟DOM】, 
										随后Vue进行【新虚拟DOM】与【旧虚拟DOM】的差异比较,比较规则如下:
										
						2.对比规则:
									(1).旧虚拟DOM中找到了与新虚拟DOM相同的key:
												①.若虚拟DOM中内容没变, 直接使用之前的真实DOM!
												②.若虚拟DOM中内容变了, 则生成新的真实DOM,随后替换掉页面中之前的真实DOM。

									(2).旧虚拟DOM中未找到与新虚拟DOM相同的key
												创建新的真实DOM,随后渲染到到页面。
												
						3. 用index作为key可能会引发的问题:
											1. 若对数据进行:逆序添加、逆序删除等破坏顺序操作:
															会产生没有必要的真实DOM更新 ==> 界面效果没问题, 但效率低。

											2. 如果结构中还包含输入类的DOM:
															会产生错误DOM更新 ==> 界面有问题。

						4. 开发中如何选择key?:
											1.最好使用每条数据的唯一标识作为key, 比如id、手机号、身份证号、学号等唯一值。
											2.如果不存在对数据的逆序添加、逆序删除等破坏顺序操作,仅用于渲染列表用于展示,
												使用index作为key是没有问题的。
		-->
		<!-- 准备好一个容器-->
		<div id="root">
			<!-- 遍历数组 -->
			<h2>计算机科学与技术学院班级人员(遍历数组)</h2>
			<button @click.once="add">添加一个学生信息</button>
			<ul>
<!--				<li v-for="(p,index) of persons" :key="index">   这个书写方法很明显是有问题的-->
				<li v-for="(p,index) of persons" :key="p.id">
					{{p.name}}-*-*-*-{{p.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 p = {id:'004',name:'王胖子',age:40}
						this.persons.unshift(p)//这个方法表示把这条数据往前放
					}
				},
			})
		</script>
	</body>
</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">
			<h2>计算机科学与技术学院</h2>
			<input type="text" placeholder="请输入名字" v-model="keyWord">
			<ul>
				<li v-for="(p,index) of filPerons" :key="index">
					{{p.name}}-{{p.age}}-{{p.sex}}
				</li>
			</ul>
		</div>

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


			//用watch实现
			//#region 
			/* new Vue({
				el:'#root',
				data:{
					keyWord:'',
					persons:[
						{id:'001',name:'马冬梅',age:19,sex:'女'},
						{id:'002',name:'周冬雨',age:20,sex:'女'},
						{id:'003',name:'周杰伦',age:21,sex:'男'},
						{id:'004',name:'温兆伦',age:22,sex:'男'}
					],
					filPerons:[]
				},
				watch:{//开始书写watch 使用watch
					keyWord:{
						immediate:true,
						handler(val){
							this.filPerons = this.persons.filter((p)=>{
								return p.name.indexOf(val) !== -1
							})
						}
					}
				}
			}) */
			//#endregion
			
			//用computed实现:
			new Vue({
				el:'#root',
				data:{
					keyWord:'',//记录用户的输入
					persons:[
						{id:'001',name:'解雨臣',age:19,sex:'女'},
						{id:'002',name:'吴邪',age:20,sex:'女'},
						{id:'003',name:'张起灵',age:21,sex:'男'},
						{id:'004',name:'王胖子',age:22,sex:'男'},
						{id:'005',name:'黑瞎子',age:40,sex: '男'}
					]
				},
				computed:{
					filPerons(){//
						return this.persons.filter((p)=>{
							return p.name.indexOf(this.keyWord) !== -1//角标返回布尔值
						})
					}
				}
			}) 
			
			
			
		</script>
		</body>
</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">
			<h2>计算机科学与技术学院</h2>
			<input type="text" placeholder="请输入名字" v-model="keyWord">
<!--			1.绑定升序事件-->
			<button @click="sortType = 2">年龄升序</button>
<!--			2.绑定降序事件-->
			<button @click="sortType = 1">年龄降序</button>
<!--			3.绑定原顺序事件-->
			<button @click="sortType = 0">原顺序</button>
			<ul>
				<li v-for="(p,index) of filPerons" :key="p.id">
					{{p.name}}-{{p.age}}-{{p.sex}}
					<input type="text">
				</li>
			</ul>
		</div>

		<script type="text/javascript">
			Vue.config.productionTip = false
			
			new Vue({
				el:'#root',
				data:{
					keyWord:'',
					sortType:0, //0原顺序 1降序 2升序
					persons:[
						{id:'001',name:'吴邪',age:40,sex:'女'},
						{id:'002',name:'张启山',age:81,sex:'女'},
						{id:'003',name:'黑瞎子',age:79,sex:'男'},
						{id:'004',name:'张起灵',age:102,sex:'男'}
					]
				},
				//以下是列表排序的算法 
				computed:{
					filPerons(){
						const arr = this.persons.filter((p)=>{
							return p.name.indexOf(this.keyWord) !== -1
						})
						//判断一下是否需要排序
						if(this.sortType){//如果这个事件成立就排序
							arr.sort((p1,p2)=>{
								//函数体
								return this.sortType === 1 ? p2.age-p1.age : p1.age-p2.age
							})
						}
						return arr
					}
				}
			}) 

		</script>
	</body>
</html>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值