vuejs組件內的props的屬性賦值問題?
問題描述
組件:<test :loading.sync="loading"></test>
Vue.component('test',{ template: '#testText', props: { loading: { type: Boolean, default: false} }, methods: {getData: function (data) { this.loading = false;//此句有錯誤,該如何更正} }});new Vue({el: '#indexBox',data: { loading : false},methods : {loadMore: function () { this.loading = true;} } });
我想在子組件里面變更loading的值回傳給父組件,請問該如何控制loading
問題解答
回答1:你用的是vue2吧,如果是vue2的話就應該用事件來把子組件的狀態傳給父組件,有兩種辦法,一種是在父組件中傳一個v-model='outerLoading',然后子組件里面
watch:{ outerLoading (v) {this.innerLoading = v }, innerLoading (v) {this.emit('input', v) }}
這樣outLoading就會響應innerLoading,實現雙向綁定的功能。還有一種做法和這個類似,就是把this.emit('input', v)換成this.emit('eventName', v),然后在父組件中@eventName='eventFunc', 再通過父組件中的eventFunc(v) { //code... }來響應子組件的狀態
相關文章:
1. mysql索引 - mysql的表如何重新組織(或生成)索引?2. python selenium 獲取圖片驗證碼3. Python爬蟲的亂碼問題?4. 請問這個課程講師是否有講PHP開發課程,他所講PHP課程名稱是什么?謝謝!5. php 如何獲取第一個參數?6. 如何用筆記本上的apache做微信開發的服務器7. Python中的占位符問題8. android - Python代碼轉為java代碼?9. android - 類似微信朋友圈或者QQ空間說說那種點擊圖片放大,并且有放大縮小手勢,左右滑動圖片手勢效果10. android - 怎樣才能在連接本地WIFI是通過 3G/4G 實現微信分享?
