给vue父子页面传值的两种方式(含代码)

推荐使用方法二

方法一:

父 :

   <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  拒绝认领  -->
    <refuse-claim-index ref="refuseClaimIndex" v-show="showRefuseClaimIndex" :reasonId="reasonId" :types="typs"/>
    <!--  确认认领  -->
    <confirm-claim ref="confirmClaim" v-show="showConfirmClaim" :confirmId="confirmId"></confirm-claim>
  </div>
</template>
//拒绝认领
refuseClaim(row) {
  this.reasonId = row.id
  this.typs = row.type;
  this.showRefuseClaimIndex = true
  if (this.showRefuseClaimIndex) {
    setTimeout(() => {
      this.$refs.refuseClaimIndex.openDialog(row.type)
    }, 300)
  }
},

子: 

<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="250px" class="demo-ruleForm">
  <el-form-item label="原因" prop="reason">
    <el-input style="width: 85%" type="textarea" :autosize="{ minRows: 3}" v-model="ruleForm.reason"></el-input>
  </el-form-item>
  <el-form-item label="电流" prop="zglszllnum"  v-if="dlPd">
    <el-input style="width: 85%" type="number" :autosize="{ minRows: 3}"

              v-model="ruleForm.zglszllnum" ></el-input>

  </el-form-item>

  <el-form-item label="有功" prop="zglszllnum"   v-if="ygPd">
    <el-input style="width: 85%" type="number" :autosize="{ minRows: 3}"

              v-model="ruleForm.zglszllnum" ></el-input>

  </el-form-item>
</el-form>
props: {
  reasonId: '',
  types: null
},
data() {
  return {
    dialogVisible: false,
    dlPd:false,
    ygPd:false,
    ruleForm: {
      reason: '',
      zglszllnum: 0,
    },
    id: '',
    rules: {
      reason: [
        {required: true, message: '请填写原因', trigger: 'blur'}
      ],
      zglszllnum: [
        {required: true, message: '请填写实际载流量/功率', trigger: 'blur'}
      ]
    }
  };
},

changeType(){
  //判断电流还是
    if (this.types ==0){
      //显示电流
      this.dlPd = true
      this.ygPd = false
    }else if (this.types ==1){
      //显示有功
      this.dlPd = false
      this.ygPd = true
    }
},
openDialog(data) {
  alert(data)
  this.dialogVisible = true;
  this.changeType();
},

方法二:

<el-table-column label="操作" align="center" min-width="20%" class-name="small-padding fixed-width">
  <template slot-scope="scope">
    <el-button v-if="scope.row.claim === 0"
               size="mini"
               icon="el-icon-success"
               @click="confirmClaim(scope.row)"
               type="text">
      确认认领
    </el-button>
    <el-button v-if="scope.row.claim === 0"
               size="mini"
               icon="el-icon-circle-close"
               @click="refuseClaim(scope.row)"
               type="text">
      拒绝认领
    </el-button>
    <el-button v-if="scope.row.claim === 2"
               size="mini"
               type="text">
      已拒绝认领
    </el-button>
    <el-button v-if="scope.row.claim === 1"
               size="mini"
               type="text"
               icon="el-icon-edit"
               @click="handleUpdate(scope.row)"

    >发布
    </el-button>

  </template>
</el-table-column>

//拒绝认领
refuseClaim(row) {
  this.reasonId = row.id
  this.formLabel = row.type==0?'实际电流':'实际功率'
  this.showRefuseClaimIndex = true
  if (this.showRefuseClaimIndex) {
    setTimeout(() => {
      this.$refs.refuseClaimIndex.openDialog()
    }, 300)
  }
},

引入一下子页面 

import refuseClaimIndex from "./components/refuseClaimIndex";

子: 

methods: { openDialog() { this.dialogVisible = true; },
<template>
  <div class="refuseClaimIndex">
    <el-dialog
      title="拒绝认领原因填写"
      :visible.sync="dialogVisible"
      width="43%"
      :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="250px" class="demo-ruleForm">
        <el-form-item label="原因" prop="reason">
          <el-input style="width: 85%" type="textarea" :autosize="{ minRows: 3}" v-model="ruleForm.reason"></el-input>
        </el-form-item>
        <el-form-item :label="formLabel" prop="zglszllnum">
          <el-input style="width: 85%" type="number" :autosize="{ minRows: 3}" v-model="ruleForm.zglszllnum"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

这里讲解一下

主要就是用了  .$refs 然后父页面引用一下;父页面定义一个需要传的元素并赋值;用$refs调用子页面的xxx方法,子页面控制 dialog 的显示隐藏;子页面可以获取到父页面的值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。在Vue中,父子组件之间传递数据是一种常见的需求。以下是一种常用的方法来实现父子组件之间的数据传递: 1. Props(属性):组件可以通过props属性向子组件传递数据。在组件中,通过在子组件标签上绑定属性的方式传递数据。在子组件中,可以通过props选项接收并使用这些数据。 组件: ```html <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } } </script> ``` 子组件: ```html <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. $emit(自定义事件):子组件可以通过$emit方法触发自定义事件,并将需要传递的数据作为参数传递给组件。在组件中,通过在子组件标签上监听自定义事件的方式接收数据。 组件: ```html <template> <div> <child-component @child-event="handleChildEvent"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleChildEvent(data) { console.log(data); // 在这里处理子组件传递的数据 } } } </script> ``` 子组件: ```html <template> <div> <button @click="emitEvent">触发事件</button> </div> </template> <script> export default { methods: { emitEvent() { this.$emit('child-event', 'Hello from child component'); // 触发自定义事件,并传递数据给组件 } } } </script> ``` 以上是Vue中实现父子组件之间传递数据的两种常用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值