Vue.js——父组件与子组件之间的通信:1.父传子用props['属性名'],子传父用this.$emit('事件名',this.值);2.ref

 

1.由父组件  传值 到子组件:(通过属性传值)

一共有7中定义component的方法,参考地址:https://medium.com/js-dojo/7-ways-to-define-a-component-template-in-vuejs-c04e0c72900d

(1)创建子组件 指定属性,把要传递的值给属性

<son name="值"></son>

(2)在子组件内部声明props属性

props:['name']

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue template</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<div id="example">
    {
  {myValue}}
    <father></father>
</div>

<script type="text/x-template" id="father-template">
    <div>
        <h1>this is a father template component</h1>
        <input type="text" v-model="kw">
        <button @click="handleClick">clickMe</button>
        <hr/>
        <!--用 v-bind接收变化的值-->
        <son :name="kw"></son>
    </div>
</script>
<script type="text/x-template" id="son-template">
    <div>
        <h1>this is a son component</h1&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值