Vue
vue简介
Vue (发音为 /vjuː/,类似 view) 是一款用于构建用户界面的JavaScript框架。它基于标准HTML、CSS和JavaScript构建,并提供了一套声明式的、组件化的编程模型,帮助开发者高效地开发用户界面。 [7]
Vue特征
- 解耦视图与数据
- M-V-VM模型 关注模型和视图
- M:即Model,模型,包括数据和一些基本操作
- V:即View,视图,页面渲染结果
- VM:即View-Model,模型和视图间的双向
- 双向数据绑定
Vue入门
1、使用方式
- vue 是一个前端框架,也是其实是一个js文件,下载vue.js文件并在页面中引入
2、vue.js 的下载方式
- 可以引入在线的vue.js(公共的CDN服务)
<script src="https://unpkg.com/vue@3.2.45/dist/vue.global.js"></script>
- 可以离线下载vue.js
- 网址:https://unpkg.com/vue@3.2.45/dist/vue.global.js
- npm包资源管理器,可以下载vue.js
初始化:npm init -y安装vue:npm install vue --save注意:重启计算机
3、第一个vue案例
- 基本格式v-***:其实就是将JS中操作DOM的各种属性或语法,封装成对应的v-XXX,直接使用v-XXX就能达到操作DOM的效果。指令直接当成标签的属性来使用
<!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 src="js/vue.global.js"></script>
</head>
<body><div id="app">{{id}}<br>{{name}}</div><script>const vueApp = Vue.createApp({data(){return {id:1,name:"chenwei"}}});vueApp.mount("#app");</script>
</body>
</html>
(1) v-text 和 v-html
- v-text:相当于JS中的innerText
- v-html:相当于JS中的innerHTML
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app"><p v-text="t"></p><p v-html="h"></p></div><script>const vueApp = Vue.createApp({data(){return {t:"天气针不错!",h:"<h1>长路漫漫</h1>"}}});vueApp.mount("#app");</script>
</body>
</html>
(2) v-if、v-else 和 v-show
- v-if、v-else:相当于JS中的if(){}else{}语法
- v-show:相当于JS中的if(){}语法
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app"><p v-if="flag">这是 真</p><p v-else="flag">这是 假</p><p v-show="flag">这是 真假</p></div><script>const vueApp = Vue.createApp({data(){return {flag:false}}});vueApp.mount("#app");</script>
</body>
</html>
(3) v-on
- v-on:相当于JS中的事件on:XXX
- 语法:v-on:事件名=”methods中定义的函数名”
- 简写:@事件名=”methods中定义的函数名”
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app">{{count}}<input type="button" value="测试1" v-on:click="f1()"><input type="button" value="测试2" @click="f2('测试个屁')"><input type="button" value="测试3" @click="f3()"></div><script>const vueApp = Vue.createApp({data(){count:1}},methods:{f1:function(){alert("测试1");},f2(b){alert(b);},f3(){this.count ++;}}});vueApp.mount("#app");</script>
</body>
</html>
(4) v-for
- v-for:相当于JS中的for循环语法
- 语法:v-for=”(item,index) in 被循环的内容”。item是循环出来的内容,index是索引
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app"><table><thead><tr><th>学号</th><th>名字</th></tr></thead><tbody><tr v-for="(student,index) in students"><td>{{student.id}}</td><td>{{student.name}}</td></tr></tbody></table></div><script>const vueApp = Vue.createApp({data(){return {students:[{"id":1,"name":"tom"},{"id":2,"name":"jack"},{"id":3,"name":"rosy"}]}} });vueApp.mount("#app");</script>
</body>
</html>
(5) v-bind
- v-bind:设置HTML属性的值
- 语法:v-bind:属性名=”值”
- 简写::属性名=”值”
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app"><a v-bind:href="url">跳转</a><br><a :href="url">跳转</a></div><script>const vueApp = Vue.createApp({data(){return {url:"https://www.baidu.com/"}}});vueApp.mount("#app");</script>
</body>
</html>
(6) v-model
- v-model:输入域的值,相当于JS中的value属性(双向绑定)
<!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>test03</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app">{{username}}<input type="text" v-model="username"></div><script>const vueApp = Vue.createApp({data(){return {username:"lfg"}}});vueApp.mount("#app");</script>
</body>
</html>
Vue生命周期
每个 Vue 实例在被创建时都要经过一系列的初始化过程 :创建实例,装载模板,渲染模板等等。Vue为生命周期中的每个状态都设置了钩子函数(监听函数)。每当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"><title>Document</title><script src="js/vue.global.js"></script>
</head>
<body><div id="app">{{message}}<input type="button" value="测试" @click="show()"></div><script>const vueApp = Vue.createApp({data:function(){return {message:"hello world"}},methods:{show:function(){this.message = "你脑子what啦"}},beforeCreate:function(){console.log("Vue对象创建前");},created:function(){console.log("Vue对象创建后");},beforeMount(){console.log("数据渲染前");},mounted(){console.log("数据渲染后");},beforeUpdate(){console.log("数据更新前");},updated(){console.log("数据更新后");},beforeUnmount(){console.log("销毁前");},unmounted(){console.log("销毁后");}});vueApp.mount("#app");</script>
</body>
</html>
Vue组件
- 通俗来说,就是自定义具有特殊效果的标签。而且一旦定义,可以多次使用。
1、全局组件
<!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 src="js/vue.global.js"></script>
</head>
<body><div id="app">{{message}}<test></test><test></test><test></test><test></test></div><script>const vueApp = Vue.createApp({data:function(){return {message:"hello World"}}});vueApp.component("test",{template:`<div>{{count}}<input type="button" value="测试" @click="f1()"></div>`,data:function(){return {count:0}},methods:{f1:function(){this.count++}}});vueApp.mount("#app");</script>
</body>
</html>
2、局部组件
<!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 src="js/vue.global.js"></script>
</head>
<body><div id="app">{{message}}<test></test><test></test></div><script>let demo = {template:`<div>{{count}}<input type="button" value="测试" @click="f1()"></div>`,data:function(){return {count:0}},methods:{f1:function(){this.count++}}};const vueApp = Vue.createApp({data:function(){return {message:"hello World"}},components:{"test":demo}});vueApp.mount("#app");</script>
</body>
</html>
}},methods:{f1:function(){this.count++}}};const vueApp = Vue.createApp({data:function(){return {message:"hello World"}},components:{"test":demo}});vueApp.mount("#app");
</script>
```