vue中的父子组件之间的通信--新增、修改弹框

摘要:
在一个vue页面中有时候内容会很多,为了方便编写查看,可以分为多个子组件,最后在父组件中引入对应的子组件即可。下面这个是父子组件通信中的一个具体实例:新增、修改弹框。cancel(){this.$emit}3.在父组件中通过定义的方法接收到子组件传递的参数,即可。cancels{console.log;}完整代码:新增˂el-table-columnprop=

在一个vue页面中有时候内容会很多,为了方便编写查看,可以分为多个子组件,最后在父组件中引入对应的子组件即可。

下面这个是父子组件通信中的一个具体实例:新增、修改弹框。子组件中主要写了关于新增、修改的弹框,

vue中的父子组件之间的通信--新增、修改弹框第1张

子组件:

1.弹框:

<div class="newDocuments">
  <div class="newDocuments_center">
    <div class="center_header">
      <div class="header_left">{{ headers }}</div>
      <div class="header_right">
        <div @click="cancel" style="font-size:20px;">x</div>
      </div>
    </div>
    <div class="center_center">
      <el-form :model="form" :rules="rules" ref="form" label-  class="demo-ruleForm">
        <el-form-item label="日期" style="422px;height:51px;">
          <el-input v-model="form.date" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" style="422px;height:51px;">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="省份" style="422px;height:51px;">
          <el-input v-model="form.province" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="市区" style="422px;height:51px;">
          <el-input v-model="form.city" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" style="422px;height:51px;">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮编" style="422px;height:51px;">
          <el-input v-model="form.zip" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')" v-if="headers == '新增'">立即添加</el-button>
          <el-button type="primary" @click="submitForm('form')" v-else>立即修改</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</div>

2.接收的数据以及需要依靠父组件的点击事件

props:["modify", "headers"],

cancel() {
  this.$emit("cancel")
}

3.新增、修改时弹框显示的内容需要进行监听

watch: {
  modify: function(){
    this.form = this.modify;
  }
},

父组件:

1.引入子组件:

<children v-show="addTo" @cancel="cancels" :modify='modify' :headers='headers'></children>

import children from './father/children'

components: {
  children,
},

2.定义需要传给子组件的数据:

modify: '',
headers: '',

3.点击新增、修改按钮时传对应的数据给子组件:

addData(){
  this.addTo = true;
  this.modify = {
    date: '',
    name: '',
    province: '',
    city: '',
    address: '',
    zip: '',
  };
  this.headers = '新增';
},
editData(row){
  this.addTo = true;
  this.modify = row;
  this.headers = '修改';
},
cancels(){
  this.addTo = false;
}

注意:

1.父子组件之间可以双向传值,父组件向子组件传值使用props,子组件向父组件传值需要依赖事件,然后通过emit传递。

2.在子组件中定义事件以及需要传递的值:

emit中的cancel为子组件向父组件传值定义的方法名称,后面可以加需要传递的值,只是在该例子中不需要传值。
cancel() {
  this.$emit("cancel", value)
}

3.在父组件中通过定义的方法接收到子组件传递的参数,即可。

cancels(data){
  console.log(data);
}

完整代码:

vue中的父子组件之间的通信--新增、修改弹框第2张vue中的父子组件之间的通信--新增、修改弹框第3张
<template>
  <div class="contrainer">
    <children v-show="addTo" @cancel="cancels" :modify='modify' :headers='headers'></children>
    <div class="contrainer_top">
      <el-button @click="addData" type="primary" size="small">新增</el-button>
    </div>
    <div class="contrainer_bottom">
      <el-table
        :data="tableData"border
        style=" 100%">
        <el-table-column
          prop="date"label="日期"min-width="50">
        </el-table-column>
        <el-table-column
          prop="name"label="姓名"min-width="50">
        </el-table-column>
        <el-table-column
          prop="province"label="省份"min-width="70">
        </el-table-column>
        <el-table-column
          prop="city"label="市区"min-width="70">
        </el-table-column>
        <el-table-column
          prop="address"label="地址"min-width="120">
        </el-table-column>
        <el-table-column
          prop="zip"label="邮编"min-width="50">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button @click="editData(scope.row)" type="text" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
  </div>
</template>

<script>import children from './father/children'export default{
  name: 'father',
  components: {
    children, 
  },
  data () {
    return{
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333}, {
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1517 弄',
        zip: 200333}, {
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1519 弄',
        zip: 200333}, {
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1516 弄',
        zip: 200333}],
      addTo: false,
      modify: '',
      headers: '',
      
    }
  },
  methods: {
    addData(){
      this.addTo = true;
      this.modify ={
        date: '',
        name: '',
        province: '',
        city: '',
        address: '',
        zip: '',
      };
      this.headers = '新增';
    },
    editData(row){
      this.addTo = true;
      this.modify =row;
      this.headers = '修改';
    },
    cancels(){
      this.addTo = false;
    }

  },

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>.contrainer{
  .contrainer_top{
    display: flex;
    justify-content: flex-end;
    margin-bottom: 10px; 
  }
  .contrainer_bottom{
    /deep/ .el-table .cell {
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      white-space: normal;
      word-break: break-all;
      line-height: 15px;
    }
    /deep/ .el-table thead th {
      padding: 4px 0;
      font-size: 14px;
      color: #43464a;
      background-color: #f5f6fa !important;
      border-bottom: 1px solid #dee2ed;
      border-right: 1px solid #ebeef5;
    }
    /deep/ .el-table td, .el-table th.is-leaf {
      border-bottom: 1px solid #EBEEF5;
    }
    .el-table th, .el-table td {
      padding: 10px 0;
    }
  }
}
</style>
父组件
vue中的父子组件之间的通信--新增、修改弹框第4张vue中的父子组件之间的通信--新增、修改弹框第5张
<template>
  <div class="newDocuments">
    <div class="newDocuments_center">
      <div class="center_header">
        <div class="header_left">{{ headers }}</div>
        <div class="header_right">
          <div @click="cancel" style="font-size:20px;">x</div>
        </div>
      </div>
      <div class="center_center">
        <el-form :model="form" :rules="rules" ref="form" label-  class="demo-ruleForm">
            <el-form-item label="日期" style="422px;height:51px;">
                <el-input v-model="form.date" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="姓名" style="422px;height:51px;">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="省份" style="422px;height:51px;">
                <el-input v-model="form.province" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="市区" style="422px;height:51px;">
                <el-input v-model="form.city" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="地址" style="422px;height:51px;">
                <el-input v-model="form.address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮编" style="422px;height:51px;">
                <el-input v-model="form.zip" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('form')" v-if="headers == '新增'">立即添加</el-button>
                <el-button type="primary" @click="submitForm('form')" v-else>立即修改</el-button>
                <el-button @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default{
  props:["modify", "headers"],
  components:{
    
  },
  data() {
    return{
      form: {
        date: '',
        name: '',
        province: '',
        city: '',
        address: '',
        zip: '',
      },
      rules: {
      }
    };
  },
  watch: {
    modify: function(){
      this.form = this.modify;
    }
  },
  methods: {
    submitForm(formName) {

    },
    cancel() {
      this.$emit("cancel")
    }
  }
}
</script>
<style lang="scss" scoped>.newDocuments{
  position: absolute;
  top: 0;
  left: 0;
   100%;
  height: 100%;
  z-index: 999;
  background: rgba($color: #000000, $alpha: 0.4);
  //display: flex;
  //align-items: center;
  //justify-content: center;
  min-height: 500px;
  overflow: auto;
  .newDocuments_center{
     43%;
    background: white;
    border-top-right-radius: 3px;
    border-top-left-radius: 3px;
    position: relative;
    margin: 5% auto; /*水平居中*/top: 70px; /*偏移*/transform: translateY(-50% -50%);
    .center_header{
      background: #f8f8f8;
      height: 45px;
      border-bottom: 1px solid #e7eaec;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-radius: 3px;
      font-size: 14px;
      .header_left{
        margin-left: 20px;
      }
      .header_right{
        margin-right: 20px;
        display: flex;
        align-items: center;
        div{
          margin-left: 10px;
          cursor: pointer;
        }
      }
    }

    .center_center{
      border-radius: 3px;
       97%;
      margin: 20px auto;
      padding: 0 0 20px 0;
      /deep/ .el-form-item__label {
        font-size: 15px !important;
      }
      /deep/ .el-input__inner{
        height: 30px;
        line-height: 30px;
      }
      /deep/ .el-form-item {
        margin-bottom: 12px;
      }
      /deep/ .el-form-item__content {
        line-height: 30px;
        position: relative;
        font-size: 14px;
         300px;
      }
      /deep/ .el-date-editor.el-input, .el-date-editor.el-input__inner{
         300px;
        line-height: 30px;
        position: relative;
        font-size: 14px;
      }
      /deep/ .el-input__icon{
        line-height: 30px;
        height: 30px;
      }
      /deep/ .el-form-item__error {
        color: #f56c6c;
        font-size: 12px;
        line-height: 2px;
        padding-top: 0px;
        position: absolute;
        top: 100%;
        left: 0;
      }
      /deep/ .el-form-item {
        margin-bottom: 0px;
      }
      /deep/ .el-form-item__content{
        100%;
        display: flex;
      }
      /deep/ .el-form-item__label{
        line-height: 27px;
      }
      /deep/ .el-button{
        padding: 7px 14px;
        font-size: 14px;
        border-radius: 2px;
      }
      /deep/ .el-form-item__label {
        font-size: 15px !important;
      }
    }
  }
}
</style>
子组件

OK了。

免责声明:文章转载自《vue中的父子组件之间的通信--新增、修改弹框》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Jmeter-测试计划元件通俗易懂大白话之C#委托与事件实例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CentOS下防御或减轻DDoS攻击方法(转)

查看攻击IP 首先使用以下代码,找出攻击者IP netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 将会得出类似如下的结果: 1 114.226.9.132 1 174.129.237.157 1 58.60.118.142 1 Address 1 s...

H5移动端复制功能实现

1 // 点击复制信息 2 copyTxt (txt) { 3 var u = navigator.userAgent 4 var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 5 // 要先判断当前...

Vue快速搭建项目

1. 安装node.js,这是最基础的环境; 2. 安装cnpm: npm install cnpm -g --registry=https://registry.npm.taobao.org 3. 全局安装vue-cli: cnpm install -g vue-cli 4. 安装vue-cli成功后,通过cd命令进入你想放置项目的文件夹,...

Vuex的深入学习

一、状态管理Vuex 1.Vuex使用 Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以对应的规则保证状态以一种可预测的方式发生变化。 (1)state:单一状态书,每个应用将仅仅包含一个store实例 *this.$store.state.状态名字 *...mapState(['title'])...

主流RPC框架通讯协议实现原理与源码解析

主流RPC框架通讯协议实现原理与源码解析。互联网+的大环境下,用户量、数据量的急剧增长,使得单机系统不能承载更多的数据处理能力,从而催生了分布式技术的快速发展。 分布式RPC框架,已经有很多开源的高性能框架,例如Dubbo、GRpc、Spring Cloud,他们都是非常优秀的RPC框架。这个PPT主要是用来分析常见RPC框架的实现原理和源码解析,最后通...

Vue移动端项目如何使用手机预览调试

  最近在开发移动端Vue移动端项目,查了一些资料,这里分享下如何在webpack工具构建下的vue项目,在手机端调试和预览,言归正传。 1.电脑和手机连接到同一个WIFI a.台式电脑和手机同时链接一个路由器,使用同一个wifi;  b.笔记本也可以直接启用一个wifi,手机链接笔记本wifi也可以; 2.查询本地IP地址 WIN+R,输入cmd回车,...