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. objective-c - ios百度地圖定位問題2. html - css 如何添加這種邊框?3. javascript - js 有什么優雅的辦法實現在同時打開的兩個標簽頁間相互通信?4. javascript - 關于定時器 與 防止連續點擊 問題5. javascript - 求助關于js正則問題6. javascript - node.js服務端渲染解疑7. javascript - 求助這種功能有什么好點的插件?8. html5 - rudy編譯sass的時候有中文報錯9. 為何 localStorage、sessionStorage 屬于html5的范疇,但是為何 IE8卻支持?10. 微信開放平臺 - Android調用微信分享不顯示
