爬坑日记--------vue之弹出框传值问题

在Vue项目中,遇到一个弹出框组件传值问题,首次打开时dialogStatus为空,再次打开时值才正确传递。解决方法是将dialogStatus声明为props属性,确保父子组件间数据正确传递。参考了vue.js官方文档来理解并解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一个vue页面,进入另一个弹出框定义的vue(传入dialogStatus值)时,我遇到第一次进入弹出框页面dialogStatus值为空,第二次进入时值传过去了。

代码:

vue页面:

<el-dialog :title="textMap[dialogStatus]" size="large" :visible.sync="dialogFormVisible">
      <modify @closeStationDialog="closeStationDialog" :dialogStatus="dialogStatus" ref="modify"></modify>
</el-dialog>
export default {
   components: {
    'modify': () => import('./components/modify')
  },
  methods: {
      handleAdd(){
          this.dialogStatus = 'create';
          this.dialogFormVisible = true;
          console.log(this.$refs.modify);
          if (this.$refs.modify !== undefined) {
            this.$refs.modify.dialogStatus = this.dialogStatus;
          }
      }
   }
}
modify页面(弹出框页面):

<div style="text-align: center; margin-bottom: 1rem;">
     <el-button @click="cancel('form')">取 消</el-button>
     <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
     <el-button v-else type="primary" @click="update('form')">确 定</el-button>
</div>  
export default {
    data(){
       return{
          dialogStatus: ''
       }
  }
}


发现问题:将 dialogStatus 值定义在 data 里,出现第一次进入弹出框页面 dialogStatus 值为空,第二次进入时 dialogStatus 有值。

解决办法:将 dialogStatus(需要传递的值)定义在 props 里。

export default {
  props: {
    dialogStatus: {
        default: '1'
    }
  }
}


来自vue.js官网的介绍:vue.js

组件实例的作用域是孤立的。这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据。父组件的数据需要通过 prop 才能下发到子组件中。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值