vuejs組件內(nèi)的props的屬性賦值問題?
問題描述
組件:<test :loading.sync="loading"></test>
Vue.component('test',{ template: '#testText', props: { loading: { type: Boolean, default: false} }, methods: {getData: function (data) { this.loading = false;//此句有錯(cuò)誤,該如何更正} }});new Vue({el: '#indexBox',data: { loading : false},methods : {loadMore: function () { this.loading = true;} } });
我想在子組件里面變更loading的值回傳給父組件,請(qǐng)問該如何控制loading
問題解答
回答1:你用的是vue2吧,如果是vue2的話就應(yīng)該用事件來把子組件的狀態(tài)傳給父組件,有兩種辦法,一種是在父組件中傳一個(gè)v-model='outerLoading',然后子組件里面
watch:{ outerLoading (v) {this.innerLoading = v }, innerLoading (v) {this.emit('input', v) }}
這樣outLoading就會(huì)響應(yīng)innerLoading,實(shí)現(xiàn)雙向綁定的功能。還有一種做法和這個(gè)類似,就是把this.emit('input', v)換成this.emit('eventName', v),然后在父組件中@eventName='eventFunc', 再通過父組件中的eventFunc(v) { //code... }來響應(yīng)子組件的狀態(tài)
相關(guān)文章:
1. windows誤人子弟啊2. 冒昧問一下,我這php代碼哪里出錯(cuò)了???3. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)4. python - linux怎么在每天的凌晨2點(diǎn)執(zhí)行一次這個(gè)log.py文件5. 數(shù)據(jù)庫 - Mysql的存儲(chǔ)過程真的是個(gè)坑!求助下面的存儲(chǔ)過程哪里錯(cuò)啦,實(shí)在是找不到哪里的問題了。6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. mysql優(yōu)化 - MySQL如何為配置表建立索引?8. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問題
