日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue中子组件向父组件传递数据(实现加减的实例)

發布時間:2023/12/31 vue 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue中子组件向父组件传递数据(实现加减的实例) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里講解一下子組件向父組件傳遞值的常用方式。 這里通過一個加減法的實例向大家說明一下,這個的原理。

如下圖所示:

當沒有任何操作的時候父組件的值是 0


當點擊加號以后父組件的值是 1


當點擊減號以后父組件的值是減一變成 0


具體代碼我直接貼出來,剛出爐的代碼。

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>子組件將數據傳遞給父組件</title><script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script> </head> <script>//定義一個組件 Vue.component('counter', {template: '\<div style="background:#eee;width: 238px;">\<div>這里是子組件里面的內容!</div>\<div style="margin-top:20px"></div>\<div>\<span style="margin-right:20px;display:inline-block;">加法運算</span><button @click="incrementCounter">+</button>\</div>\<div>\<span style="margin-right:20px;margin-top:20px;display:inline-block;">減法運算</span><button @click="deleteCounter">-</button>\</div>\</div>\',data: function () {return {counter: 0}},methods: {incrementCounter: function () {this.counter += 1;this.$emit('increment',1);},deleteCounter: function () {this.counter -= 1;this.$emit('increment',2);}} })//執行一個組件 window.onload = function(){var app = new Vue({el: '#app',data: {total: 0},methods:{incrementTotal: function (val) {if(val==1){this.total += 1;}else{if(this.total<=0){this.total = 0;}else{this.total -= 1;}}}}}) }</script> <body><div id="app" style="background:red;width: 238px;"><p>這里是父組件里面的內容!</p> <p>子組件傳遞的值:<b>{{ total }}</b></p><counter v-on:increment="incrementTotal"></counter></div> </body> </html>

總結

以上是生活随笔為你收集整理的vue中子组件向父组件传递数据(实现加减的实例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。