vue 组件间传值的几种方式

vue组件传值有好几种方式

1.父传子     可以通过子组件props暴露属性   ,然后在父组件中用

son.vue

<template>
  <div class="son">
    <h2>子组件</h2>
    {{ sonMsg }}
  </div>
</template>
<script>
export default {
  props: {
    sonMsg: {
      type: String,
      default: "今天阳光明媚",
    },
  },
};
</script>
<style lang="less" scoped>
.son {
  width: 300px;
  height: 200px;
  background-color: #eee;
}
</style>

  father.vue

<template>
  <div class="father">
    <h1>父组件</h1>
    <son sonMsg="天气有点不好"></son>
  </div>
</template>
<script>
import Son from "./Son.vue";
export default {
  components: {
    Son,
  },
  data() {
    return {};
  },
};
</script>
<style lang="less" scoped>
.father {
  width: 300px;
  height: 300px;
  background-color: #ccc;
  margin: 50px auto;
}
</style>

效果:

2.子传父   可以通过在子组件定义$emit方法  ,然后去父组件用    this.$emit("方法名",值);

son.vue

<template>
  <div class="son">
    <h2>子组件</h2>
    {{ sonMsg }}
    <button @click="sure">点击</button>
  </div>
</template>
<script>
export default {
  props: {
    sonMsg: {
      type: String,
      default: "今天阳光明媚",
    },
  },
  methods: {
    sure() {
      //   向父组件传递值  mTest方法
      this.$emit("mTest", "还有很多事要做");
    },
  },
};
</script>
<style lang="less" scoped>
.son {
  width: 300px;
  height: 200px;
  background-color: #eee;
}
</style>

father.vue      (拿到子组件中的mTest方法,值就是子组件方法中的参数

<template>
  <div class="father">
    <h1>父组件</h1>
    <p>{{ hText }}</p>
    <son sonMsg="天气有点不好" @mTest="even"></son>
  </div>
</template>
<script>
import Son from "./Son.vue";
export default {
  components: {
    Son,
  },
  data() {
    return {
      hText: "",
    };
  },
  methods: {
    even(val) {
      //   console.log(val);
      this.hText = val;
    },
  },
};
</script>
<style lang="less" scoped>
.father {
  width: 300px;
  height: 300px;
  background-color: #ccc;
  margin: 50px auto;
}
</style>

效果:  点击后的效果

3.父子组件之间的联动

son.vue

<template>
  <div class="son">
    <h2>子组件</h2>
    {{ sonMsg }}
    <button @click="sure">点击</button>
    <input type="text" v-model="sonWord" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      sonWord: "换",
    };
  },
  watch: {
    sonWord: function (newValue, oldVaue) {
      this.$emit("mTest", newValue);
    },
  },
  props: {
    sonMsg: {
      type: String,
      default: "今天阳光明媚",
    },
  },
  methods: {
    sure() {
      //   向父组件传递值  mTest方法
      this.$emit("mTest", "还有很多事要做");
    },
  },
};
</script>
<style lang="less" scoped>
.son {
  width: 300px;
  height: 200px;
  background-color: #eee;
}
</style>

father.vue

<template>
  <div class="father">
    <h1>父组件</h1>
    <p>{{ hText }}</p>
    <!-- 父子联动 -->
    <input type="text" v-model="fatherWord" />
    <son :sonMsg="fatherWord" @mTest="even"></son>
  </div>
</template>
<script>
import Son from "./Son.vue";
export default {
  components: {
    Son,
  },
  data() {
    return {
      fatherWord: "天气有点不好",
      hText: "",
    };
  },
  methods: {
    even(val) {
      //   console.log(val);
      this.hText = val;
    },
  },
};
</script>
<style lang="less" scoped>
.father {
  width: 300px;
  height: 300px;
  background-color: #ccc;
  margin: 50px auto;
}
</style>

4.provide 和inject实现父子传值     父组件使用provide定义变量   子组件使用inject接收变量   然后在子组件中使用

son.vue

<template>
  <div class="son">
    <h2>子组件</h2>
    {{ sonMsg }}
    <button @click="sure">点击</button>
    <input type="text" v-model="sonWord" />
    {{ msg }}
  </div>
</template>
<script>
export default {
  data() {
    return {
      sonWord: "换",
    };
  },
  watch: {
    sonWord: function (newValue, oldVaue) {
      this.$emit("mTest", newValue);
    },
  },
  inject: ["msg"],
  props: {
    sonMsg: {
      type: String,
      default: "今天阳光明媚",
    },
  },
  methods: {
    sure() {
      //   向父组件传递值  mTest方法
      this.$emit("mTest", "还有很多事要做");
    },
  },
};
</script>
<style lang="less" scoped>
.son {
  width: 300px;
  height: 200px;
  background-color: #eee;
}
</style>

 father.vue

<template>
  <div class="father">
    <h1>父组件</h1>
    <p>{{ hText }}</p>
    <!-- 父子联动 -->
    <input type="text" v-model="fatherWord" />
    <son :sonMsg="fatherWord" @mTest="even"></son>
  </div>
</template>
<script>
import Son from "./Son.vue";
export default {
  components: {
    Son,
  },
  data() {
    return {
      fatherWord: "天气有点不好",
      hText: "",
    };
  },
  provide: {
    msg: "12345aaa",
  },
  methods: {
    even(val) {
      //   console.log(val);
      this.hText = val;
    },
  },
};
</script>
<style lang="less" scoped>
.father {
  width: 300px;
  height: 300px;
  background-color: #ccc;
  margin: 50px auto;
}
</style>

5.通过插槽传值     子组件定义 <slot text="aaaaa"></slot>    父组件中使用

 <son :sonMsg="fatherWord" @mTest="even">

      <!-- <h2>父传子的数据</h2> -->

      <template slot-scope="scope">

        {{ scope.text }}

      </template>

    </son>

son.vue

<template>
  <div class="son">
    <h2>子组件</h2>
    {{ sonMsg }}
    <button @click="sure">点击</button>
    <input type="text" v-model="sonWord" />
    {{ msg }}
    <slot text="aaaaa"></slot>
  </div>
</template>
<script>
export default {
  data() {
    return {
      sonWord: "换",
    };
  },
  watch: {
    sonWord: function (newValue, oldVaue) {
      this.$emit("mTest", newValue);
    },
  },
  inject: ["msg"],
  props: {
    sonMsg: {
      type: String,
      default: "今天阳光明媚",
    },
  },
  methods: {
    sure() {
      //   向父组件传递值  mTest方法
      this.$emit("mTest", "还有很多事要做");
    },
  },
};
</script>
<style lang="less" scoped>
.son {
  width: 300px;
  height: 200px;
  background-color: #eee;
}
</style>

father.vue

<template>
  <div class="father">
    <h1>父组件</h1>
    <p>{{ hText }}</p>
    <!-- 父子联动 -->
    <input type="text" v-model="fatherWord" />
    <son :sonMsg="fatherWord" @mTest="even">
      <!-- <h2>父传子的数据</h2> -->
      <template slot-scope="scope">
        {{ scope.text }}
      </template>
    </son>
  </div>
</template>
<script>
import Son from "./Son.vue";
export default {
  components: {
    Son,
  },
  data() {
    return {
      fatherWord: "天气有点不好",
      hText: "",
    };
  },
  provide: {
    msg: "12345aaa",
  },
  methods: {
    even(val) {
      //   console.log(val);
      this.hText = val;
    },
  },
};
</script>
<style lang="less" scoped>
.father {
  width: 300px;
  height: 300px;
  background-color: #ccc;
  margin: 50px auto;
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值