vue 父組件如何實(shí)時(shí)監(jiān)聽(tīng)子組件data數(shù)據(jù)變化 然后進(jìn)行業(yè)務(wù)邏輯處理。
父組件代碼:
<template lang = "pug">
#debugger
.debugger-1
//- 請(qǐng)求
.debugger_body_request
.debugger_body_request_host
input(autocomplete = "off", placeholder = "服務(wù)器未配置",readonly = "readonly","type" = "text", value = "http://localhost")
.debugger_body_request_method_url
.debugger_body_request_method(@mouseleave = "isShowRequestMethod = false", style = "display: -webkit-inline-box;")
button.btn(@click = "isShowRequestMethod = !isShowRequestMethod")
strong {{ currentRequestMethod }}
i.iconfont.icon-xia1(style = "font-size:13px;")
.prohibition_of_replication(v-show = "isShowRequestMethod")
ul.xiahua
li(v-for = "method in requestMethod", @click = "selectiveRequestMethod(method)") {{ method }}
.debugger_body_request_url
input(v-model = "requestUrl" type = "text", name = "requestUrl", placeholder = "http://", autocomplete = "off")
.debugger_body_reset_params.prohibition_of_replication
a(href="#") 重置為文檔參數(shù)
.debugger_body_params
.tabcontrol.prohibition_of_replication
.abcontrol-1(v-if = "tab")
div(v-for="tab, index in tab", @click = "showView(tab.view, index)", :class = "{tab: true, updateStyle: index == viewIndex}") {{ tab.name }}
div(v-if = "tab.options", @mouseleave = "isShowParamsFormat = false", style = "display:inline-block")
i.iconfont.icon-xia1(@click.stop = "isShowParamsFormat =!isShowParamsFormat", style = "vertical-align: middle")
ul.xiahua(v-show = "isShowParamsFormat", style = "widthd:82px")
li(v-for = "option,optionIndex in tab.options" @click.stop = "selectiveParamsFormat(option.name, option.view, index)") {{ option.name }}
keep-alive
component(:is = "view", :type = "type", @cacheParams = "cacheParams")
.el_button
button.request_test(type = "button") 請(qǐng)求測(cè)試
div(style = "float: left;width:60%")
button.saveArchive(type = "button") 生成文檔
//- 響應(yīng)
.debugger_response(style = "width:40%")
.debugger_response_statusCode
ul
li
span Status:
span(style = "color:red") 200
li
span Time: 7ms
li
span Size: 123kb
</template>
<script>
import formView from './form';
import queryView from './query';
import jsonView from './json';
import xmlView from './xml';
import headerView from './header';
export default {
components: {
formView,
jsonView,
queryView,
xmlView,
headerView,
},
data()
{
return {
view: "formView",
viewIndex: 0,
type: 'Form',
//接口地址
requestUrl: '',
isShowRequestMethod: false,
isShowParamsFormat: false,
requestMethod: [
'GET',
'POST',
'PUT',
'DELETE',
'OPTIONS'
],
currentRequestMethod: 'GET',
tab: [{
"name": "Form",
"view": "formView",
"options": [{
"name": "Form",
"view": "formView"
},{
"name": "JSON",
"view": "jsonView",
}, {
"name": "XML",
"view": "xmlView"
}]
}, {
"name": "Query",
"view": "queryView",
}, {
"name": "Header",
"view": "headerView",
}],
params: []
}
},
methods: {
selectiveRequestMethod(method)
{
this.currentRequestMethod = method;
this.isShowRequestMethod = !this.isShowRequestMethod;
},
/**
* 二級(jí)選項(xiàng)卡處理
*
* @param string name 展示的內(nèi)容
* @param string view 要顯示的視圖
* @index intger index 父級(jí)選項(xiàng)卡的索引
*/
selectiveParamsFormat(name, view, index)
{
this.type = name;
this.tab[index].name = name;
this.tab[index].view = view;
this.showView(view, index);
this.isShowParamsFormat = !this.isShowParamsFormat;
},
showView(view, index)
{
this.viewIndex = index;
this.view = view;
},
cacheParams(data)
{
console.log(data);
}
}
}
</script>
子組件代碼:
<template lang = "pug">
#form
.form_form
table(v-if = "data")
tr(v-for = "val, index in data")
td(style="width:7%")
label
input.check.form_input(:checked = "val.is", @click = "data[index].is = !data[index].is", type = "checkbox")
td
div
input.params_key.form_input.input-placeholder(v-model = "val.key", placeholder = "key")
td
div
input.form_input(v-model = "val.value", @keyup.enter = "", placeholder = "value")
td
i.iconfont.icon-shanchu.debugger_icon(@click = "deleteParams(index)")
button.form_button(@click = "addParams()", type = "button")
i.iconfont.icon-iconjia(style="font-weight:400, font-size:15px")
span 添加參數(shù)
</template>
<script>
export default {
data()
{
return {
data: [],
//QUERY_STRING Params
params: '?'
}
},
updated()
{
this.$emit('cacheParams', this.data);
},
methods: {
addParams()
{
this.data.push({"key":"", "value":"","is":1});
},
deleteParams(index)
{
this.data.splice(index, 1);
},
}
}
</script>
頁(yè)面:
下面那個(gè)input框是子組件, 我想實(shí)現(xiàn)的效果是 input框發(fā)生變化實(shí)時(shí)通知給父組件 然后父組件在上面那個(gè)url框進(jìn)行拼接 比如: test.php?id=1&name=3
已嘗試的解決方案:
1、在子組件里聲明updated方法然后拋出事件給父組件 this.$emit('cacheParams', this.data);
這樣每次data發(fā)生變化都會(huì)通知父組件并把改過(guò)的或者新增的data數(shù)據(jù)傳遞給父組件,
但是這樣會(huì)把全部數(shù)據(jù)給父組件 父組件每次都需要for循環(huán)一遍全部數(shù)據(jù) 很浪費(fèi)性能。
請(qǐng)問(wèn)還有沒(méi)有一些其他的解決方案
首先。。。麻煩簡(jiǎn)化一下你的代碼。。。八哥看著頭大。
其次,簡(jiǎn)要說(shuō)一下父組件監(jiān)聽(tīng)子組件數(shù)據(jù)的方法(沒(méi)看你的示例,不知道是不是你要的)
// 父組件
<template>
<child-comp v-model="a"></child-comp>
</template>
export default {
data() {
a: ''
}
}
// 子組件
<template>
<input @input="handleInput" />
</template>
export default {
methods: {
handleInput() {
this.$emit('input', e.target.value)
}
}
}
其實(shí)這里的v-model
也是一個(gè)語(yǔ)法糖,省略了在父組件里監(jiān)聽(tīng)input
事件的代碼。
如果你的數(shù)據(jù)項(xiàng)比較多,那就分別發(fā)事件吧。
this.$emit('xx', payload)
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。