Vue?子組件傳父組件?$emit更新屬性方式
子組件傳父組件 $emit更新屬性
$emit(update: prop, “newPropVulue”) => $emit(update: 屬性名, “新的屬性值”)
例如修改swicth的開關(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ù)無效的坑
子級(jí)向父級(jí)傳遞—自定義事件
當(dāng)子組件需要向父組件傳遞數(shù)據(jù)時(shí),用到的是自定義事件
自定義事件的流程:
在子組件中,通過$emit()來觸發(fā)事件。
在父組件中,通過v-on來監(jiān)聽子組件事件。
我們來看一個(gè)簡(jiǎn)單的例子:
我們之前做過一個(gè)兩個(gè)按鈕+1和-1,點(diǎn)擊后修改counter。
我們整個(gè)操作的過程還是在子組件中完成,但是之后的展示交給父組件。
這樣,我們就需要將子組件中的counter,傳給父組件的某個(gè)屬性,比如total。
? <!--父組件模板-->
? <div id="app">
? ? <!-- 3.在父組件子標(biāo)簽中,通過v-on來監(jiān)聽子組件事件 并添加一個(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.在子組件中,通過$emit()來觸發(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ù)是接收子組件傳過來的數(shù)據(jù)的
? ? ? ? ? console.log('cpnClick', item);
? ? ? ? ??
? ? ? ? }
? ? ? }
? ? })
? </script>重點(diǎn)來了?。。?!
這里的父組件是app,子組件是cpn
在父組件子標(biāo)簽中,通過v-on來監(jiān)聽子組件事件 并添加一個(gè)響應(yīng)該事件的處理方法,即監(jiān)聽的事件應(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)簽中,通過v-on來監(jiān)聽子組件事件 并添加一個(gè)響應(yīng)該事件的處理方法 --> ? ? <cpn @item-click="cpnClick"></cpn> ? </div>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)的封裝全局filter并統(tǒng)一管理操作示例
這篇文章主要介紹了vue實(shí)現(xiàn)的封裝全局filter并統(tǒng)一管理操作,結(jié)合實(shí)例形式詳細(xì)分析了vue封裝全局filter及相關(guān)使用技巧,需要的朋友可以參考下2020-02-02
Vue自定義指令實(shí)現(xiàn)對(duì)數(shù)字進(jìn)行千分位分隔
對(duì)數(shù)字進(jìn)行千分位分隔后展示應(yīng)該是大部分同學(xué)都做過的功能了吧,常規(guī)的做法通常是編寫一個(gè)工具函數(shù)來對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換,那么我們可不可以通過vue指令來實(shí)現(xiàn)這一功能呢,下面我們就來探索一下呢2024-02-02
select的change方法傳遞多個(gè)參數(shù)的方法詳解
element-ui中的select,checkbox等組件的change方法的回調(diào)函數(shù)只有當(dāng)前選擇的val,如果想再傳入自定義參數(shù)怎么辦,本文給大家分享select的change方法如何傳遞多個(gè)參數(shù),感興趣的朋友一起看看吧2024-02-02
解決Idea、WebStorm下使用Vue cli腳手架項(xiàng)目無法使用Webpack別名的問題
這篇文章主要介紹了解決Idea、WebStorm下使用Vue cli腳手架項(xiàng)目無法使用Webpack別名的問題,需要的朋友可以參考下2019-10-10
vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能實(shí)例
這篇文章主要給大家介紹了關(guān)于vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能的相關(guān)資料, localStorage這個(gè)特性主要是用來作為本地存儲(chǔ)來使用的,解決了cookie存儲(chǔ)空間不足的問題,需要的朋友可以參考下2023-06-06
詳解vue-router 2.0 常用基礎(chǔ)知識(shí)點(diǎn)之router.push()
本篇文章主要介紹了vue-router 2.0 常用基礎(chǔ)知識(shí)點(diǎn)之router.push(),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
vue 自動(dòng)化路由實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 自動(dòng)化路由實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

