赞
踩
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="./lib/vue.js"></script> </head> <body> <div id="app"> {{msg}} - {{title}} - ${{(price * 0.8).toFixed(1)}} </div> <script> Vue.createApp({ data() { return { msg: 'Hello Vue3.0', title: 'introduction', price: 117 } } }).mount('#app') </script> </body> </html>
实现步骤
data
{{msg}}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <p>我是原有内容 --- {{msg}}</p> <p v-html="msg">我是原有内容 --- </p> <p v-text="msg">我是原有内容 --- </p> </div> <script> Vue.createApp({ data () { return { msg: 'Hello World', } } }).mount('#app') </script> </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"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <a v-bind:href="url" :title="title">{{website}}</a> </div> <script> Vue.createApp({ data() { return { url: 'http://www.baidu.com', title: '百度一下,你就知道', website: '百度' } } }).mount("#app") </script> </body> </html>
两种方式
<div v-bind:class="{ active: isActive}"></div>
<div v-bind:class="[fs, bgc]"></div>"
<!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="./lib/vue.js"></script> <style> .active { color: red; border: 1px solid #336950; } .add { background-color: lightgreen; } .fs { font-style: italic; } .bgc { background-color: lightgrey; } </style> </head> <body> <div id="app"> <p v-bind:class="{active: isActive, add: isAdd}">明月几时有,把酒问青天</p> <h4 :class="[c1, c2]">不知天上宫阙,今夕是何年</h4> </div> <script> const app = { data() { return { isActive: true, isAdd: false, c1: "fs", c2: "bgc" } } } Vue.createApp(app).mount("#app") </script> </body> </html>
两种方式
<div :style="{fontSize: fs}"></div>
<div :style="[c1, c2]"></div>
<!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="./lib/vue.js"></script> </head> <body> <div id="app"> <p :style="{fontSize: objStyle.fs, backgroundColor: objStyle.bgc}">曲径通幽处</p> <h4 v-bind:style="[c1, c2]">禅房花木深</h4> </div> <script> const app = { data() { return { objStyle: { fs: '20px', bgc: 'orange' }, c1: { fontSize: '30px', color: 'blue', backgroundColor: 'lightgrey' }, c2: { border: '2px solid red' } } } } Vue.createApp(app).mount("#app") </script> </body> </html>
用来为标签绑定事件,可以简写为@
<button v-on:click="handle">点我</button>
<!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="./lib/vue.js"></script> <style> .status { width: 100px; height: 100px; line-height: 100px; text-align: center; background-color: orange; color: #fff; } </style> </head> <body> <div id="app"> <div :class="{status: statusActive}" v-on:click="handle" @mouseenter="enter('哈哈哈我来了')" >点我</div> </div> <script> const app = { data() { return { statusActive: true } }, methods: { handle() { this.statusActive = !this.statusActive }, enter(str) { console.log(str) } } } Vue.createApp(app).mount('#app') </script> </body> </html>
在对象中如果想调用data中的数据,一定需要使用this指针
<!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="./lib/vue.js"></script> <style> #app .content { width: 600px; margin: 30px auto; } #app .content img { width: 600px; } #app .content div { display: flex; justify-content: space-between; } </style> </head> <body> <div id="app"> <div class="content"> <img :src="list[i]"> <div> <button @click="prev">上一张</button> <button @click="next">下一张</button> </div> </div> </div> <script> const app = { data() { return { list: [ "./images/1.jpg", "./images/2.jpg", "./images/3.jpg", "./images/4.jpg", "./images/5.jpg" ], i: 0 } }, methods: { prev() { this.i = --this.i == -1 ? 4 : this.i }, next() { this.i = ++ this.i == 5 ? 0 : this.i } } } Vue.createApp(app).mount('#app') </script> </body> </html>
能够根据表达式的真假值来有条件地渲染元素,还有v-else
、v-else-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>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <p v-if="age > 18">成年人</p> <p v-else>未成年人</p> <h3 v-if="score >= 90">优秀</h3> <h3 v-else-if="score >= 80 && score < 90">良好</h3> <h3 v-else-if="score >= 70 && score < 80">中等</h3> <h3 v-else-if="score >= 60 && score < 70">及格</h3> <h3 v-else>不及格</h3> </div> <script> const app = { data () { return { age: 8, score: 99 } } } Vue.createApp(app).mount('#app') </script> </body> </html>
循环渲染元素
<li
v-for="(item, index) in list"
:key="index"
>
{{item}}
</li>
<!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="./lib/vue.js"></script> </head> <body> <div id="app"> <ul> <li v-for="(item, index) in array" :key="index">{{item}}</li> </ul> </div> <script> const app = { data() { return { array: ['关羽', '张飞', '赵云', '马超', '黄忠'] } } } Vue.createApp(app).mount('#app') </script> </body> </html>
用于数据的双向绑定
<input type="text" v-model="account">
data() {
return {
account: '用户名'
}
}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> <style> input { width: 260px; } textarea { width: 260px; margin-top: 10px; vertical-align: top; } ul { padding: 0px; list-style: none; width: 300px; border: 1px solid #ccc; border-radius: 5px; } .msg { box-sizing: border-box; width: 300px; padding: 10px; border-bottom: 1px solid #ccc; } .close { float: right; cursor: pointer; } span:hover { visibility: visible; } </style> </head> <body> <div id="app"> 姓名: <input type="text" v-model="name" /><br> 留言: <textarea cols="22" rows="10" v-model="content" ></textarea><br> <button @click="send">发表</button> <ul> <li class="msg" v-for="(item, index) in list" :key="index"> {{item.name}} : {{item.content}} </li> </ul> </div> <script> const app = { data () { return { list: [ {name:"后羿", content:"周日都让我射熄火了"}, {name:"鲁班", content:"智商250..."}, {name:"程咬金", content:"爱心之斧的正义冲击"}, {name:"孙悟空", content:"俺老孙来也"}, {name:"妲己", content:"妲己陪你玩哦"} ], name: '', content: '' } }, methods: { send () { if (this.name.trim().length === 0 || this.content.trim().length === 0) { alert('请正确输入姓名和留言') return; } let tmp = {name: this.name, content: this.content}; this.list.push(tmp); this.name = ''; this.content = ''; } } } Vue.createApp(app).mount('#app'); </script> </body> </html>
MVVM是Model-View-ViewModel
MVVM的核心思想是让前端程序员专注于业务的实现,最大限度的减少DOM操作
Vue2采用的是OptionsAPI,Vue3采用的是CompositionAPI,Vue3兼容Vue2的OptionsAPI
通常用来定义简单数据类型(数值型、字符串等)
使用时需要先从Vue中解构出来:const { ref } = Vue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <ul> <li>姓名: {{name}}</li> <li> 年龄: {{age}} <button @click="add">+</button> </li> </ul> 姓名: <input type="text" v-model="name"><br> 年龄: <input type="text" v-model="age"><br> </div> <script> // 1.从Vue对象中将ref方法解构出来 const { ref } = Vue; // 使用ref来声明响应式数据 let name = ref('王小明'); let age = ref(8); const add = () => { console.log(age); age.value++; // age是一个对象,需要修改其value值 } const app = { setup () { return { name, age, add }; } } Vue.createApp(app).mount('#app'); </script> </body> </html>
通常用来定义复杂数据类型(数组、对象)
使用时需要先从Vue中解构出来:const { reactive } = Vue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <ul> <li v-for="item in list" :key="item.id"> {{item.id}} - {{item.name}} - {{item.age}} </li> <li> {{user.id}} - {{user.name}} - {{user.age}} </li> </ul> <button @click="show">点我</button> </div> <script> const { reactive } = Vue; const list = reactive([ {id: 1, name: 'zs', age: 8}, {id: 2, name: 'ls', age: 12}, {id: 3, name: 'ww', age: 10} ]) const user = reactive({ id: 4, name: 'zl', age: 9 }) const show = () => { alert('哈哈哈'); } const app = { setup () { return { list, user, show } } } Vue.createApp(app).mount('#app'); </script> </body> </html>
挂载:创建Vue实例并挂载到页面上
更新:页面发生变化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <p>{{msg}}</p> <button @click="change">更新</button> </div> <script> const { ref } = Vue; let msg = ref(''); const app = { beforeCreate () { console.log('beforeCreate'); }, created () { msg.value = 'Hello World'; console.log('created'); }, beforeMount () { console.log('beforeMount'); }, mounted () { document.querySelector('p').style.color = 'red'; console.log('mounted'); }, beforeUpdate () { console.log('beforeUpdate'); }, updated () { console.log('updated'); }, setup () { change = () => { msg.value = '你好,世界'; } return { msg, change } } } // createApp 用来创建Vue实例 // mount 用来将实例挂载到页面上 Vue.createApp(app).mount('#app'); </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./lib/vue.js"></script> </head> <body> <div id="app"> <p>{{msg}}</p> <button @click="change">更新</button> </div> <script> // 钩子函数必须从Vue对象中解构出来 const { ref, onBeforeMount, onMounted, onBeforeUpdate, onUpdated } = Vue; let msg = ref(''); // 初始化数据的方法,在setup中直接调用即可 const getDate = () => { msg.value = 'Hello World'; } Vue.createApp({ setup () { getDate(); onBeforeMount(() => { console.log('onBeforeMount'); }) onMounted(() => { document.querySelector('p').style.color = 'red'; console.log('onMounted'); }) onBeforeUpdate (() => { console.log('onBeforeUpdate'); }) onUpdated (() => { console.log('onUpdated'); }) change = () => { msg.value = '你好,世界'; } return { msg, change } } }).mount('#app'); </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。