温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue.js父子组件通信动态绑定的实例

发布时间:2020-10-09 09:34:12 来源:脚本之家 阅读:114 作者:兰亭古墨 栏目:web开发

如下所示:

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<div id='app'>
  <!--这里的作用是将父组件渲染到页面上-->
  <father></father>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
<script type="text/x-template" id="father">
  <div>
    <!--这里实现一个双向绑定-->
    <!--parentMsg变量必须在data中声明,不然会报错,我就错在这个坑-->
    <input v-model="parentMsg">
    <br>
    <child :my-message="parentMsg"></child>
  </div>
</script>
<script type="text/x-template" id="child">
  <div> {{'父组件传递的数据为:'+ myMessage }} </div>
</script>
<script>

  Vue.component('father',{
//    这里我直接把template写到前面script标签中了,写在这里一大坨,难看
    template:'#father',
    data:function(){
      return {
        parentMsg:''
      }
    }
  });

  //在 Vue 中,父子组件的关系可以总结为 props down, events up。
  // 父组件通过 props 向下传递数据给子组件,子组件通过 events 给父组件发送消息
  Vue.component('child', {
    props: ['myMessage'],//这里的props选项是用来实现父子组件的通信的,传递下来的消息字组件用花括号接住
    template: '#child'
  });

  new Vue({
    el:'#app'
  })

</script>
</html>

以上这篇vue.js父子组件通信动态绑定的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI