Vue组件信息的传递(父传子)(子传父)

路由部署注册在我的另一篇博文:Vue路由的部署

总结
父传子:在标签上传递之后子组件通过props接收
子传父:子组件类似创建一个事件可以通过$emit传递 触发父组件

父传子代码
父组件:

<template >
  <div>
    <child :msg="text"></child>
    <br /><br /><br /><br /><br /><br /><br />
    <!-- 此处(text)可以省略 -->
    <input type="text" v-model="text" @change="dataChange(text)" />
  </div>
</template>
<script>
import child from "../components/child";
export default {
  name: "login",
  data() {
    return {
      text: "我是登录页面",
    };
  },
  components: {
    child,
  },
  methods: {
    dataChange(data) {
      this.msg = data;
    },
  },
};
</script>

在标签处传递
子组件:

<template>
  <div class="container">
    {{ msg }}
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  props: {
    msg: String,
  },
};
</script>
<style scoped>
#container {
  color: red;
  margin-top: 50px;
}
</style>

通过props接收后面注意类型

字传父代码

子组件:

<template>
  <div class="container">
    <input type="text" v-model="msg" />
    <button @click="setData">立刻!传递</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      msg: "我要传点什么呢",
    };
  },
  methods: {
    setData() {
      this.$emit("getDataa", this.msg);
    },
  },
};
</script>
<style scoped>
#container {
  color: red;
  margin-top: 50px;
}
</style>

通过emit传值

父组件:

<template >
  <div>
    <!-- 看好aa和ss即可清楚谁和谁一对 -->
    <child @getDataa="getDatas"></child>
    <p>{{ msg }}</p>
  </div>
</template>
<script>
import child from "../components/child";
export default {
  name: "login",
  data() {
    return {
      msg: "我是登录页面",
    };
  },
  components: {
    child,
  },
  methods: {
    getDatas(data) {
      this.msg = data;
    },
  },
};
</script>

注意命名 哪个对应哪个可以把getDataa当成一个事件 子组件可以触发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值