Vue?子組件傳父組件?$emit更新屬性方式
子組件傳父組件 $emit更新屬性
$emit(update: prop, “newPropVulue”) => $emit(update: 屬性名, “新的屬性值”)
例如修改swicth的開(kāi)關(guān)
效果圖圖下
父組件
data() { return { swValue: true, ElementData : [ { date: "2016-05-02", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1518 弄" }, { date: "2016-05-04", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1517 弄" }, { date: "2016-05-01", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1519 弄" }, { date: "2016-05-03", name: "王小虎", address: "上海市普陀區(qū)金沙江路 1516 弄" } ]; }; },
父組件的 html
<el-table :data="ElementData" style="width: 100%"> <el-table-column prop="name" label="姓名" width="180"> ? <template> ? ? <el-input v-model="input" placeholder="請(qǐng)輸入內(nèi)容"></el-input> ? </template> </el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column prop="ppp" label="選擇"> ? <template slot-scope="scope"> ? ? <Sw v-model="swValue" :value.sync="swValue" @change="swc(scope.row)" /> ? </template> </el-table-column> </el-table>
父組件的方法中 可以接收 新的swValue值
?swc(val) { ? ? ? console.log(val); ? ? ? console.log(this.swValue); ? ? },
子組件 Sw組件
<template> ? <label role="checkbox" :class="['switch', { toggled }]"> ? ? <input type="checkbox" class="switch-input" @change="toggle" /> ? ? <div class="switch-core" :style="{ backgroundColor: toggled ? colorChecked : colorUnchecked }"> ? ? ? <div ? ? ? ? class="switch-button" ? ? ? ? :style="{ ? ? ? ? ? transition: `transform ${speed}ms`, ? ? ? ? ? transform: toggled ? `translate3d(32px, 0px, 0px)` : null ? ? ? ? }" ? ? ? ></div> ? ? </div> ? ? <span class="switch-label label-right" v-if="toggled" v-html="labelChecked"></span> ? ? <span class="switch-label label-left" v-html="labelUnchecked" v-else></span> ? </label> </template>
<script> export default { ? name: "ToggleSwitch", ? data() { ? ? return { ? ? ? toggled: this.value ? ? }; ? }, ? props: { ? ? value: { ? ? ? type: Boolean, ? ? ? default: true ? ? }, ? ? speed: { ? ? ? type: Number, ? ? ? default: 100 ? ? }, ? ? labelChecked: { ? ? ? type: String, ? ? ? default: "ON" ? ? }, ? ? labelUnchecked: { ? ? ? type: String, ? ? ? default: "OFF" ? ? }, ? ? colorChecked: { ? ? ? type: String, ? ? ? default: "#11CED2" ? ? }, ? ? colorUnchecked: { ? ? ? type: String, ? ? ? default: "#E6EAF1" ? ? } ? }, ? watch: { ? ? value: function(val) { ? ? ? this.value = val; ? ? } ? }, ? methods: { ? ? toggle(event) { ? ? ? this.toggled = !this.toggled; ? ? ? this.$emit("update:value", this.toggled); ? ? ? this.$emit("change", event); ? ? } ? } }; </script>
<style scoped> .switch { ? display: inline-block; ? position: relative; ? overflow: hidden; ? vertical-align: middle; ? user-select: none; ? font-size: 10px; ? cursor: pointer; } .switch-input { ? display: none; } .switch-label { ? position: absolute; ? top: 0; ? font-weight: 600; ? color: white; ? z-index: 2; } .switch-label.label-left { ? left: 20px; ? line-height: 20px; ? border-top-left-radius: 2px; ? border-bottom-left-radius: 2px; ? color: #b5bdc8; } .switch-label.label-right { ? right: 20px; ? line-height: 20px; ? border-top-right-radius: 2px; ? border-bottom-right-radius: 2px; } .switch-core { ? width: 50px; ? height: 20px; ? border-radius:10px; ? line-height: 20px; ? display: block; ? position: relative; ? box-sizing: border-box; ? outline: 0; ? margin: 0; ? transition: border-color 0.3s, background-color 0.3s; ? user-select: none; } .switch-button { ? width: 16px; ? height: 16px; ? display: block; ? position: absolute; ? overflow: hidden; ? top: 2; ? left: 2; ? z-index: 3; ? transform: translate3d(0, 0, 0); ? background-color: rgba(255, 255, 255, 1); ? border-radius: 10px; ? margin-top: 2px; ? margin-left: 2px; } </style>
子組件向父組件使用自定義事件$emit傳遞數(shù)據(jù)無(wú)效的坑
子級(jí)向父級(jí)傳遞—自定義事件
當(dāng)子組件需要向父組件傳遞數(shù)據(jù)時(shí),用到的是自定義事件
自定義事件的流程:
在子組件中,通過(guò)$emit()來(lái)觸發(fā)事件。
在父組件中,通過(guò)v-on來(lái)監(jiān)聽(tīng)子組件事件。
我們來(lái)看一個(gè)簡(jiǎn)單的例子:
我們之前做過(guò)一個(gè)兩個(gè)按鈕+1和-1,點(diǎn)擊后修改counter。
我們整個(gè)操作的過(guò)程還是在子組件中完成,但是之后的展示交給父組件。
這樣,我們就需要將子組件中的counter,傳給父組件的某個(gè)屬性,比如total。
? <!--父組件模板--> ? <div id="app"> ? ? <!-- 3.在父組件子標(biāo)簽中,通過(guò)v-on來(lái)監(jiān)聽(tīng)子組件事件 并添加一個(gè)響應(yīng)該事件的處理方法 --> ? ? <cpn @item-click="cpnClick"></cpn> ? </div> ? ? <!--子組件模板--> ? <template id="cpn"> ? ? <div> ? ? ? <!-- 1.在子組件中創(chuàng)建一個(gè)按鈕,給按鈕綁定一個(gè)點(diǎn)擊事件 --> ? ? ? <button v-for="item in categories" @click="btnClick(item)"> ? ? ? ? {{item.name}} ? ? ? </button> ? ? </div> ? </template> ? ? <script src="../js/vue.js"></script> ? <script> ? ? // 子傳父 自定義事件 ? ? ? // 子組件? ? ? const cpn = { ? ? ? template: '#cpn', ? ? ? data() { ? ? ? ? return { ? ? ? ? ? categories: [{ ? ? ? ? ? ? ? id: 'aaa', ? ? ? ? ? ? ? name: '熱門推薦' ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? id: 'bbb', ? ? ? ? ? ? ? name: '手機(jī)數(shù)碼' ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? id: 'ccc', ? ? ? ? ? ? ? name: '家用家電' ? ? ? ? ? ? }, ? ? ? ? ? ? { ? ? ? ? ? ? ? id: 'ddd', ? ? ? ? ? ? ? name: '電腦辦公' ? ? ? ? ? ? }, ? ? ? ? ? ] ? ? ? ? } ? ? ? }, ? ? ? methods: { ? ? ? ? btnClick(item) { ? ? ? ? ? // 發(fā)射事件: 自定義事件 ? ? ? ? ? // 2.在子組件中,通過(guò)$emit()來(lái)觸發(fā)事件 ? ? ? ? ? this.$emit('item-click', item) ? ? ? ? ? // 注意?。。?!這里的$emit事件名不要寫成駝峰!?。∧_手架里可以,會(huì)先編譯成一個(gè)組件對(duì)象render函數(shù) ? ? ? ? } ? ? ? } ? ? } ? ? ? // 父組件? ? ? const app = new Vue({ ? ? ? el: '#app', ? ? ? data: { ? ? ? ? message: '你好啊' ? ? ? }, ? ? ? components: { ? ? ? ? cpn ? ? ? }, ? ? ? methods: { ? ? ? ? cpnClick(item) { // 這里的參數(shù)是接收子組件傳過(guò)來(lái)的數(shù)據(jù)的 ? ? ? ? ? console.log('cpnClick', item); ? ? ? ? ?? ? ? ? ? } ? ? ? } ? ? }) ? </script>
重點(diǎn)來(lái)了!?。?!
這里的父組件是app,子組件是cpn
在父組件子標(biāo)簽中,通過(guò)v-on來(lái)監(jiān)聽(tīng)子組件事件 并添加一個(gè)響應(yīng)該事件的處理方法,即監(jiān)聽(tīng)的事件應(yīng)該寫在子組件cpn在父組件app里調(diào)用的標(biāo)簽上,而不是寫在app上
這里的父組件觸發(fā)其實(shí)是指的在父組件的作用域下,在子組件上v-on:觸發(fā),很久之前學(xué)的Vue了,今天做項(xiàng)目的時(shí)候?qū)懙臅r(shí)候?qū)懙絘pp上了,還是自己太粗心了
? <!--父組件模板--> ? <div id="app"> ? ? <!-- 在父組件子標(biāo)簽中,通過(guò)v-on來(lái)監(jiān)聽(tīng)子組件事件 并添加一個(gè)響應(yīng)該事件的處理方法 --> ? ? <cpn @item-click="cpnClick"></cpn> ? </div>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue和react的區(qū)別及優(yōu)缺點(diǎn)解讀
這篇文章主要介紹了vue和react的區(qū)別及優(yōu)缺點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue如何通過(guò)props方式在子組件中獲取相應(yīng)的對(duì)象
這篇文章主要介紹了vue如何通過(guò)props方式在子組件中獲取相應(yīng)的對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼
本篇文章主要介紹了vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05vue@cli3項(xiàng)目模板怎么使用public目錄下的靜態(tài)文件
這篇文章主要介紹了vue@cli3項(xiàng)目模板怎么使用public目錄下的靜態(tài)文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究
這篇文章主要介紹了vue中關(guān)于v-for循環(huán)key值問(wèn)題的研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue3監(jiān)聽(tīng)resize窗口事件(離開(kāi)頁(yè)面要銷毀窗口事件)
這篇文章主要給大家介紹了關(guān)于vue3監(jiān)聽(tīng)resize窗口事件(離開(kāi)頁(yè)面要銷毀窗口事件)的相關(guān)資料,vue是單頁(yè)面應(yīng)用,路由切換后,定時(shí)器并不會(huì)自動(dòng)關(guān)閉,需要手動(dòng)清除,當(dāng)頁(yè)面被銷毀時(shí),清除定時(shí)器即可,需要的朋友可以參考下2023-11-11vue如何使用watch監(jiān)聽(tīng)指定數(shù)據(jù)的變化
這篇文章主要介紹了vue如何使用watch監(jiān)聽(tīng)指定數(shù)據(jù)的變化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04