🥳🥳Welcome Huihui's Code World ! !🥳🥳
接下来看看由辉辉所写的关于Vue的相关操作吧
目录
🥳🥳Welcome Huihui's Code World ! !🥳🥳
1.文本
html> <html> <head> <meta charset="utf-8"> <title>插值title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>文本h1> {{content1}} div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ content1:'我是天才' , content2 :'我是天才 ' } } }) script> body> html>
2.html
html> <html> <head> <meta charset="utf-8"> <title>插值title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>文本h1> {{content1}} <h1>htmlh1> <h2 v-html="content2">h2> div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ content1:'我是天才' , content2 :'我是天才' } }, methods: { }, }) script> body> html>注意!!!
3.属性&class绑定&style绑定
html> <html> <head> <meta charset="utf-8"> <title>插值title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <style> .fs{ font-size: 38px; } style> <div id="app"> <h1>classh1> <h2 :class="content3 " v-html="content2">h2> div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ content2 :'我是天才' , content3 :'fs' , } }, methods: { }, }) script> body> html>
4.表达式
html> <html> <head> <meta charset="utf-8"> <title>插值title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <style> .fs{ font-size: 100px; } style> <div id="app"> <h1>表达式h1> {{content4+2}}br> {{content5.substr(0,4)}}br> <input v-model="content6" /> {{content6==1 ?'是的':'不细啊'}} div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ content4 :5, content5 :'我是天才的女人', content6:1 } }, methods: { }, }) script> body> html>
1.v-if&v-else&v-else-if
html> <html> <head> <meta charset="utf-8"> <title>指令title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>v-if...h1> <input v-model="score"/><br/> <b v-if="score<60">情商低b> <b v-else-if="score>60 && score<70">情商忽上忽下b> <b v-else-if="score>70 && score<80">情商一般b> <b v-else-if="score>80 && score<90">中等情商b> <b v-else=" ">高情商b> div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ score:69, } }, methods: { }, }) script> body> html>
2.v-show
3.v-for
html> <html> <head> <meta charset="utf-8"> <title>指令title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>v-if...h1> <input v-model="score"/><br/> <b v-if="score<60">情商低b> <b v-else-if="score>=60 && score<=70">情商忽上忽下b> <b v-else-if="score>70 && score<=80">情商一般b> <b v-else-if="score>80 && score<=90">中等情商b> <b v-else=" ">高情商b> <h1>v-if和v-show的区别h1> <b v-if="ifshow">ifb> <b v-show="ifshow">showb> <h1>v-forh1> <i v-for="a in arr">{{a}} i><br /> <i v-for="d in dogs">{{d.name}} i><br /> <i v-for="i,d in dogs">{{i}} i><br /> <select> <option v-for="h in hobby" :value="h.id">{{h.name}}option> select><br /> <div v-for="h in hobby"> <input type="checkbox" name="hobby" value="h.id"/>{{h.name}} div> div> body> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ score:69, ifshow:false, arr:[1,3,5,7,9], dogs:[{ id:1,name:'夏威', },{ id:2,name:'xw', },{ id:3,name:'小威威', },{ id:4,name:'夏威夷', }], hobby:[{ id:1,name:'打夏威', },{ id:2,name:'爱xw', },{ id:3,name:'亲小威威', },{ id:4,name:'去夏威夷', }], } }, methods: { }, }) script> body> html>
- html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>过滤器title>
- <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script>
- head>
-
- <div id="app">
- <h1>动态参数h1>
- <input v-model="evname" />
- <button v-on:[evname]="test">clickbutton>
- div>
- <script type="text/javascript">
- // 构建vue实例 绑定边界
- new Vue({
- el:'#app',
- data(){
- return{
- evname:'click'
- };
- },
- methods: {
- test(){
- alert("你再点一下!!!!");
- }
- }
- })
- script>
- html>
1.局部过滤器
html> <html> <head> <meta charset="utf-8"> <title>过滤器title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <div id="app"> <h1>动态参数h1> <input v-model="evname" /> <button v-on:[evname]="test">clickbutton> <h1>局部过滤器h1> {{content}}<br /> {{content | filter1}}<br /> <hr /> {{content}}<br /> {{content | filter1 | filter2}}<br /> <h1>过滤器传参h1> {{content | filter3(4,9)}}<br /> <hr /> div> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', filters:{ 'filter1':function(f){ return f.substring(0,6); }, 'filter2':function(f){ return f.substring(0,4); }, 'filter3':function(f,s,e){ return f.substring(s,e); } }, data(){ return{ content:'红红火火恍恍惚惚', time:new Date() }; }, methods: { test(){ alert("你再点一下!!!!"); } } }) script> html>2.全局过滤器
html> <html> <head> <meta charset="utf-8"> <title>过滤器title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> <script src="js/date.js" type="text/javascript" >script> head> <div id="app"> <h1>动态参数h1> <input v-model="evname" /> <button v-on:[evname]="test">clickbutton> <h1>全局过滤器h1> {{time}}<br /> {{time | all }} div> <script type="text/javascript"> // 全局过滤器 Vue.filter('all', function(value) { return fmtDate(value); }); // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ content:'红红火火恍恍惚惚', time:new Date() }; }, methods: { test(){ alert("你再点一下!!!!"); } } }) script> html>
1.计算属性
html> <html> <head> <meta charset="utf-8"> <title>计算属性&监听属性title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>计算属性h1> 单价:<input v-model="price"/> 数量:<input v-model="num"/> 小计:{{sum}} div> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ price:79, num:10, } }, computed:{ sum:function(){ return this.price * this.num } }, methods: { }, }) script> body> html>
2.监听属性
html> <html> <head> <meta charset="utf-8"> <title>计算属性&监听属性title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.js">script> head> <body> <div id="app"> <h1>监听属性h1> 千米:<input v-model="km"/> 米:<input v-model="m"/> div> <script type="text/javascript"> // 构建vue实例 绑定边界 new Vue({ el:'#app', data(){ return{ m:1000, km:1 } }, watch:{ km:function(v){ //v是千米 this.m=parseInt(v)*1000 }, m:function(v){ //v是米 this.km=parseInt(v)/1000 } }, methods: { }, }) script> body> html>
效果
代码
html> <html> <head> <meta charset="UTF-8"> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js">script> <title>购物车title> <style> .cart-title { color: white; font-size: 24px; margin-bottom: 20px; } .cart-items { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; } .cart-item { background-color: white; padding: 10px; border-radius: 5px; } .item-image { width: 100%; height: auto; } .item-name { margin-top: 10px; font-size: 18px; } .item-price { font-size: 16px; color: grey; } .item-quantity { display: flex; align-items: center; } .quantity-label { margin-right: 5px; } .quantity-button { cursor: pointer; background-color: #eee; padding: 5px; border: none; border-radius: 3px; } .subtotal { font-size: 16px; color: grey; margin-top: 5px; } .total { font-size: 20px; margin-top: 10px; } th, td { padding: 8px; text-align: center; } style> head> <body> <div id="app"> <div class="cart-container"> <h1 class="cart-title">购物车h1> <table width="100%" border="0;" cellspacing="0" cellpadding="0"> <thead> <tr> <th>商品th> <th>数量th> <th>单价th> <th>小计th> tr> thead> <tbody> <tr v-for="item in items" :key="item.id"> <td > <img :src="item.image" style="width: 64px; height: 64px;" alt="商品图片"> <h3 class="item-name">{{ item.name }}h3> td> <td class="item-quantity"> <button @click="updateQuantity(item, -1)">-button> <span class="quantity-label">{{ item.quantity }}span> <button @click="updateQuantity(item, 1)">+button> td> <td class="item-price">{{ item.price }}td> <td> <p class="subtotal">{{ calculateSubtotal(item) }}p> td> tr> tbody> <tfoot> <tr> <td colspan="3">td> <td> <p class="total" colspan="4" sty>总计{{ calculateTotal() }}p> td> tr> tfoot> table> div> div> <script> new Vue({ el: '#app', data: { items: [ { id: 1, name: '商品1', price: '¥199', image: 'img/美妆类3.jpg ', quantity: 1 }, { id: 2, name: '商品2', price: '¥299', image: 'img/美妆类5.jpg', quantity: 1 }, { id: 3, name: '商品3', price: '¥399', image: 'img/美妆类7.jpg', quantity: 1 }, ] }, methods: { updateQuantity(item, amount) { item.quantity += amount; }, calculateSubtotal(item) { return '¥' + (parseFloat(item.price.slice(1)) * item.quantity).toFixed(2); }, calculateTotal() { let total = 0; for (let item of this.items) { total += parseFloat(item.price.slice(1)) * item.quantity; } return '¥' + total.toFixed(2); } } }); script> body> html>
好啦,今天的分享就到这了,希望能够帮到你呢!😊😊