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的值回傳給父組件,請問該如何控制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. 老哥們求助啊2. javascript - vue-router怎么不能實(shí)現(xiàn)跳轉(zhuǎn)呢3. python - 模擬滑動(dòng)驗(yàn)證碼,有源碼,求解4. 就一臺服務(wù)器,mysql數(shù)據(jù)庫想實(shí)現(xiàn)自動(dòng)備份,如何設(shè)計(jì)?5. 在MySQL中新增字段時(shí),報(bào)錯(cuò)??6. npm鏡像站全新上線7. css3 - 請問一下在移動(dòng)端CSS布局布局中通常需要用到哪些元素,屬性?8. java - 想使用別人項(xiàng)目中maven引入的jar包,就是那個(gè).class文件,要怎么操作?9. java - 安卓電視盒子取得了root權(quán)限但是不能安裝第三方應(yīng)用,請問該怎么辦?10. html5 - angularjs中外部模版加載無法使用
