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. python小白的基础问题 关于while循环的嵌套2. php自学从哪里开始?3. MySQL客户端吃掉了SQL注解?4. 数据库 - MySQL 单表500W+数据,查询超时,如何优化呢?5. mac 安装 python_MySQLdb6. 求大神帮我看看是哪里写错了 感谢细心解答7. javascript - 百度echarts series数据更新问题8. javascript - 图片能在网站显示,但控制台仍旧报错403 (Forbidden)9. mysql - AttributeError: ’module’ object has no attribute ’MatchType’10. phpstady在win10上运行
