computed问题

computed问题

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1,user-scalable=0">
	<title>调查问卷</title>
	<style type="text/css" media="screen">
		.next{
			width: 100px;
			height: 30px;
			background-color: green;
			color: white;
			font-size: 14px;
			border: none;
			position: absolute;
			bottom: 20px;
			left: 40px;
		}
		.last{
			width: 100px;
			height: 30px;
			background-color: #abcdef;
			color: white;
			font-size: 14px;
			border: none;
			position: absolute;
			bottom: 20px;
			left: 160px;
		}
		.reset{
			width: 100px;
			height: 30px;
			background-color: #abcdef;
			color: white;
			font-size: 14px;
			border: none;
			position: absolute;
			bottom: 20px;
			right: 40px;
		}
		.page1 form, .page2 form, .page3 form{
			font-size: 30px;
		}
		.page2 input{
			display: inline-block;
			width: 15px;
			height: 15px;
		}
		.page3 textarea{
			width: 80%;
			height: 150px;
			text-indent: 20px;
			display: block;
			margin: 48px auto;
		}
	</style>
</head>
<body>
	<div id="app">
			<page val="1" ref="abc" v-model="mainvalue" :everypage="pages" class="page1">
				<h1>1.请问您的性别是?</h1>
				<form id="formtext1">
					男<input type="radio" name="sex" checked="checked">
					女<input type="radio" name="sex">	
					<btn :allpage="pages" v-model="mainvalue"></btn>
				</form>
			</page>
			<page val="2" ref="abc" v-model="mainvalue" :everypage="pages" class="page2">
				<h1>2.请选择您的兴趣爱好:</h1>
				<form id="formtext2">
					看书<input type="checkbox" name="hobby" value="看书"><br/>
					游泳<input type="checkbox" name="hobby" value="游泳"><br/>
					跑步<input type="checkbox" name="hobby" value="跑步"><br/>
					看电影<input type="checkbox" name="hobby" value="看电影"><br/>
					听音乐<input type="checkbox" name="hobby" value="听音乐"><br/>
					<btn :allpage="pages" v-model="mainvalue"></btn>
				</form>
				<h3>请最少选择两个爱好!</h3>
			</page>
			<page val="3" ref="abc" v-model="mainvalue" :everypage="pages" class="page3">
				<h1>3.请介绍一下自己:</h1>
				<form id="formtext3">
					<textarea name="imf" placeholder="不少于100字" minlength="100" autofocus></textarea>
					<btn :allpage="pages" v-model="mainvalue"></btn>
				</form>
			</page>
	</div>

  <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
	<script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
	<script src="btn.js"></script>
	<script src="page.js"></script>
	<script>
		var app = new Vue({
			el: '#app',
			data: {
				mainvalue: '1',
				pages: []
			},
			methods: {					
				update: function() {
					var this_page = this.$children.filter(function(item) {
							return item.$options.name === 'page';
						});
					this.$root.pages = this_page;
					console.log(this.pages);
					this.$refs.abc.appear();
					}
			},
			mounted() {
				this.update();
			}
		});
	</script>
</body>
</html>
Vue.component('page', {
	name: 'page',
	template: '\
		<div class="page" v-show="show">\
			<slot></slot>\
		</div>',

	props: {
		val: {
			type: String
		},
		value: {
			type: [String, Number]
		},
		everypage: {
			type: Array
		}
	},

	data() {
		return {
			show: false,
			pageList: [],
			currentValue: this.value,
		}
	},

	methods: {
		appear: function() {
			var _this = this;
			this.pageList.forEach( function(item) {
				return item.show = item.val === _this.currentValue;
			});
		}
	},

	watch: {
		everypage: function(newval, oldval) {
			this.pageList = newval;
			this.appear();
		},
		value: function(newval, oldval) {
			this.currentValue = newval;
		}
	}
});
Vue.component('btn', {
    name: 'btn',
    template: '\
			<p class="container">\
				<button type="submit" @click="toNext()" class="next" :able="able" @click.prevent>下一步</button>\
				<button type="submit" @click="toLast()" class="last" @click.prevent>上一步</button>\
				<button type="submit" @click="reSet()" class="reset" @click.prevent>重置</button>\
			</p>',

    props: {
        allpage: {
            type: Array
        },
        value: {
            type: [String, Number]
        }
    },

    data() {
        return {
            thisValue: this.value
        }
    },

    computed: {
        able: function() {
            	if(this.$root.mainvalue === '2') { 
            		var hobbyBox = $('input[name=hobby]');
                var hobbyList = [];
                for (var i = 0; i < hobbyBox.length; i++) {
                    if (hobbyBox[i].checked) {
                        hobbyList.push(hobbyBox[i].value);
                    }
                }
				        if(hobbyList.length < 2) {
				        	$('.page2 .container .next').css('background-color', 'red');
				        }else {
				        	$('.page2 .container .next').css('background-color', 'green');
				        }
              }	
        }
      },

    methods: {
        toNext: function() {
            if (this.$root.mainvalue === '2') {
                var hobbyBox = $('input[name=hobby]');
                var hobbyList = [];
                for (var i = 0; i < hobbyBox.length; i++) {
                    if (hobbyBox[i].checked) {
                        hobbyList.push(hobbyBox[i].value);
                    }
                }
                if (hobbyList.length > 2 || hobbyList.length === 2) {
                    thisValue = String(Number(this.value) + 1);
                    if (Number(this.$parent.currentValue) < 3) {
                        this.$parent.currentValue = thisValue;
                        this.$root.mainvalue = thisValue;
                        this.$parent.appear();
                    } else {
                        this.$parent.currentValue = '1';
                        this.$root.mainvalue = '1';
                        this.$parent.appear();
                    }
                }
            } else {
                thisValue = String(Number(this.value) + 1);
                if (Number(this.$parent.currentValue) < 3) {
                    this.$parent.currentValue = thisValue;
                    this.$root.mainvalue = thisValue;
                    this.$parent.appear();
                } else {
                    this.$parent.currentValue = '1';
                    this.$root.mainvalue = '1';
                    this.$parent.appear();
                }
            }
        },
        toLast: function() {
            thisValue = String(Number(this.value) - 1);
            if (Number(this.$parent.currentValue) > 1) {
                this.$parent.currentValue = thisValue;
                this.$root.mainvalue = thisValue;
                this.$parent.appear();
            } else {
                // thisValue = '0';
                this.$parent.currentValue = '3';
                this.$root.mainvalue = '3';
                this.$parent.appear();
            }
        },
        reSet: function() {
            var formtext = document.getElementsByTagName('form');
            		if (event.target.parentNode.parentNode.id === 'formtext1') {
                    document.getElementById('formtext1').reset();
                } else if (event.target.parentNode.parentNode.id === 'formtext2') {
                    document.getElementById('formtext2').reset();
                } else {
                    document.getElementById('formtext3').reset();
                }
        }
    },

    watch: {
        value: function(newval, oldval) {
            this.thisValue = newval;
        }

    }
});

http://img1.sycdn.imooc.com//climg/5c21ded700018e3312630548.jpg

老师,这个VUE小程序里面我想实现在第二个调查问卷页面中选中2个或2个以上时按钮就能从红色变成绿色,但我这个地方不知道哪儿出问题了,非要点一次下一步按钮颜色才能变过去,麻烦老师帮我分析一下,computed中的方法不是实时计算的吗

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
一路电光带火花 2018-12-25 19:12:08

这里使用计算属性的话,也没有返回东西。而且第一次点击的时候执行了able这个,所以颜色改变了,但是之后这个就没在触发了,所以也就不会改变了。你试试不把这个写在computed中试一下。

一路电光带火花 2018-12-25 16:34:59

因为你这个是在able这个函数里面的呀,只有触发这个函数的时候,才可以改变呀。

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
热门框架Vue开发WebApp 18版
  • 参与学习           人
  • 提交作业       209    份
  • 解答问题       3299    个

本路径是通过ES6基础知识、运用Zepto、Swiper、fullPag等移动端常用工具包、以及当下流行框架Vue,结合多个实战案例,还原真实开发场景,最终实现手机端购物商城网页开发。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师