关于使用props控制confirm组件显示时出现的问题

关于使用props控制confirm组件显示时出现的问题

老师您好,我按自己的思路想通过在index.vue中传值来控制confirm组件的显示,但是我发现我只能让它显示一次,点击取消后再点击清空搜索记录按钮就没有反应了,我在watch中也尝试过打印值,发现watch中的值只会打印一次,后面传入的props再发生变化它就没有响应了。附上代码,麻烦老师看看

<template>
<div class="confirm" v-if="control">
<div class="confirm-wrapper">
<div class="confirm-container">
<div class="confirm-top">
<p class="confirm-title">确定要清空吗?</p>
</div>
<div class="confirm-bottom">
<div class="confirm-left" @click="cancel">取消</div>
<div class="confirm-right" @click="confirm">确定</div>
</div>
</div>
</div>
</div>
</template>
<script type="text/javascript">
import {SEARCH_HISTORY_KEYWORD_KEY} from './config.js'
import storage from '@/assets/js/storage.js'
export default {
data() {
return {
control: this.confirmStatus,
}
},
props: {
confirmStatus: {
type: Boolean,
default: false
}
},
watch: {
confirmStatus(newval) {
this.control = true;
}
},
methods: {
cancel() {
this.control = false;
},
confirm() {
storage.remove(SEARCH_HISTORY_KEYWORD_KEY);
this.control = false;
this.$emit("confirm")
},
// show() {
//  this.control = true;
// }
}
}
</script>
<style type="text/css" scoped>
@import 'src/assets/scss/mixins';
.confirm {
&-wrapper {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: $search-z-index;
background: rgba(0,0,0, .5);
}
&-container {
position: absolute;
height: 170px;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
z-index: $search-popup-z-index;
width: 80%;
background: #fff;
border-radius: 15px;
overflow: hidden;
}
&-top {
height: 130px;
}
&-title {
line-height: 130px;
text-align: center;
border-bottom: 1px solid #ccc;
font-size: 18px;
font-weight: bold;
}
&-bottom {
height: 40px;
}
&-left {
width: 50%;
text-align: center;
line-height: 40px;
float: left;
}
&-right {
width: 50%;
text-align: center;
line-height: 40px;
background: #f00;
color: #fff;
float: left;
}
}
</style>
<template>
<div class="search">
<div class="g-header-container">
<search-header></search-header>
</div>
<div class="g-content-container">
<search-scroll ref='scroll' :barStatus="false" :dragStatus="false">
<search-hot @hotDataGet="updateScroll"></search-hot>
<search-history @deleteAll="deleteAll" ref="history"></search-history>
</search-scroll>
</div>
<search-confirm :confirmStatus="confirmStatus" @confirm="confirm" ref="confirm"></search-confirm>
</div>
</template>
<script type="text/javascript">
import searchHeader from './header.vue'
import searchHot from './hot.vue'
import searchScroll from '@/base/scroll'
import loading from '@/components/loading'
import searchHistory from './history.vue'
import searchConfirm from './confirm.vue'
export default {
data() {
return {
confirmStatus: false
}
},
components: {
searchHeader,
searchHot,
searchScroll,
searchHistory,
searchConfirm
},
methods: {
updateScroll() {
this.$refs.scroll.update();
},
deleteAll() {
this.confirmStatus = true;
this.$refs.confirm.show();
},
// confirm() {
//  this.$refs.history.update();
// }
},
}
</script>
<style type="text/css" scoped>
@import 'src/assets/scss/mixins'
</style>


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

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

1回答
好帮手慕夭夭 2019-05-18 14:10:08

你好同学,vue里面是单向数据流,父组件中可以改变子组件中的数据,但是子组件不能改变父组件传递过来的数据。而你的代码中问题就是想通过父组件传递props,然后在子组件中改变,所以这个思路是不对的。

同学应该把这个控制组件显示的参数定义在子组件中,通过$emit触发confirm事件时,传递这个参数。然后父组件接收这个参数来控制confirm组件是否显示。先试着改一改,如果哪里不会了,可以重新提问哦。

祝学习愉快 ,望采纳。

  • 提问者 qq_那些荒废流年 #1
    我知道视频中的方法,我也照做了,那样的话的确没问题。但是我不知道我这里出了什么问题,我知道vue是单向数据流呀,所以我在data中复制了一份confirmStatus的值,把这个复制了值赋给control。然后用监听器监听props中ConfirmStatus的变化,如果有变化就把变化的值也就是true再赋给我在data中定义的值control 我觉得我的思路没问题呀,但是就像我之前说的,我在监听器中随便打印一个值,发现监听器只会打印一次,在后面当传进来的ConfirmStatus中的值发生变化的到时候,它好像并没有监听到变化。想问下老师该怎么解决
    2019-05-18 14:31:38
  • 你好同学,请查看私信,以便老师进一步帮助你解决问题。
    2019-05-18 18:22:32
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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