Vue 子组件传父组件 $emit更新属性

$emit(update: prop, “newPropVulue”) => $emit(update: 属性名, “新的属性值”)

例如修改swicth的开关
效果图图下
在这里插入图片描述
父组件

data() {
    return {
    swValue: true,
    ElementData : [
      {
        date: "2016-05-02",
        name: "王小虎",
        address: "上海市普陀区金沙江路 1518 弄"
      },
      {
        date: "2016-05-04",
        name: "王小虎",
        address: "上海市普陀区金沙江路 1517 弄"
      },
      {
        date: "2016-05-01",
        name: "王小虎",
        address: "上海市普陀区金沙江路 1519 弄"
      },
      {
        date: "2016-05-03",
        name: "王小虎",
        address: "上海市普陀区金沙江路 1516 弄"
      }
    ];
    };
  },
父组件的 html
<el-table :data="ElementData" style="width: 100%">
<el-table-column prop="name" label="姓名" width="180">
  <template>
    <el-input v-model="input" placeholder="请输入内容"></el-input>
  </template>
</el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<el-table-column prop="ppp" label="选择">
  <template slot-scope="scope">
    <Sw v-model="swValue" :value.sync="swValue" @change="swc(scope.row)" />
  </template>
</el-table-column>
</el-table>
父组件的方法中 可以接收 新的swValue值
 swc(val) {
      console.log(val);
      console.log(this.swValue);
    },

子组件 Sw组件

<template>
  <label role="checkbox" :class="['switch', { toggled }]">
    <input type="checkbox" class="switch-input" @change="toggle" />
    <div class="switch-core" :style="{ backgroundColor: toggled ? colorChecked : colorUnchecked }">
      <div
        class="switch-button"
        :style="{
          transition: `transform ${speed}ms`,
          transform: toggled ? `translate3d(32px, 0px, 0px)` : null
        }"
      ></div>
    </div>
    <span class="switch-label label-right" v-if="toggled" v-html="labelChecked"></span>
    <span class="switch-label label-left" v-html="labelUnchecked" v-else></span>
  </label>
</template>
 
<script>
export default {
  name: "ToggleSwitch",
  data() {
    return {
      toggled: this.value
    };
  },
  props: {
    value: {
      type: Boolean,
      default: true
    },
    speed: {
      type: Number,
      default: 100
    },
    labelChecked: {
      type: String,
      default: "ON"
    },
    labelUnchecked: {
      type: String,
      default: "OFF"
    },
    colorChecked: {
      type: String,
      default: "#11CED2"
    },
    colorUnchecked: {
      type: String,
      default: "#E6EAF1"
    }
  },
  watch: {
    value: function(val) {
      this.value = val;
    }
  },
  methods: {
    toggle(event) {
      this.toggled = !this.toggled;
      this.$emit("update:value", this.toggled);
      this.$emit("change", event);
    }
  }
};
</script>
 
<style scoped>
.switch {
  display: inline-block;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  user-select: none;
  font-size: 10px;
  cursor: pointer;
}
.switch-input {
  display: none;
}

.switch-label {
  position: absolute;
  top: 0;
  font-weight: 600;
  color: white;
  z-index: 2;
}

.switch-label.label-left {
  left: 20px;
  line-height: 20px;
  border-top-left-radius: 2px;
  border-bottom-left-radius: 2px;
  color: #b5bdc8;
}

.switch-label.label-right {
  right: 20px;
  line-height: 20px;
  border-top-right-radius: 2px;
  border-bottom-right-radius: 2px;
}
.switch-core {
  width: 50px;
  height: 20px;
  border-radius:10px;
  line-height: 20px;
  display: block;
  position: relative;
  box-sizing: border-box;
  outline: 0;
  margin: 0;
  transition: border-color 0.3s, background-color 0.3s;
  user-select: none;
}
.switch-button {
  width: 16px;
  height: 16px;
  display: block;
  position: absolute;
  overflow: hidden;
  top: 2;
  left: 2;
  z-index: 3;
  transform: translate3d(0, 0, 0);
  background-color: rgba(255, 255, 255, 1);
  border-radius: 10px;
  margin-top: 2px;
  margin-left: 2px;
}
</style>
发布了17 篇原创文章 · 获赞 0 · 访问量 190

猜你喜欢

转载自blog.csdn.net/who_become_gods/article/details/104608371
今日推荐