vue5

作者: 暴打小乳猪 | 来源:发表于2018-09-23 19:59 被阅读0次

1.父给子传值 属性 props:['属性']

2.子给父传 用事件传 $emit

子给父传:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Document</title>

</head>

<body>

  <div id='app'>

      <my-father></my-father>

  </div>

  <script src='js/vue.js'></script>

  <script>

      Vue.component("my-father",{

          template:`

            <div>

              <h1>{{mess}}</h1>

              <my-child @send='rcvMsg'></my-child>

            </div>

          `,

          data:function(){

              return{

                  mess:''

              }

          },

          methods:{

              rcvMsg:function(txt){

                  this.mess=txt

              }

          }

      })

      Vue.component('my-child',{

          template:`

                <button @click='sendToFather'>传给父元素</button>

            `,

          data:function(){

              return{

                  msg:'我是子组件中的数据,要给父组件传值'

              }

          },

          methods:{

            sendToFather:function(){

//                this.$emit('自定义事件名',要传输的数据)

                  this.$emit('send',this.msg)

            } 

          }

      })

      new Vue({

          el:"#app"

      })

    </script>

</body>

</html>

相关文章

  • vue5

    1.父给子传值 属性 props:['属性'] 2.子给父传 用事件传 $emit 子给父传:

  • vue5

    es6新内容 class 解构赋值 扩展对象 模块化 什么是模块化 模块的作用 怎么实现模块化 模块化的标准 Co...

  • 【Vue5】父子组件间的数据传递

    父组件向子组件传递数据 父组件通过属性的形式,向子组件传递数据。 如果我们希望加上点击自身,增加数字的功能,可能理...

网友评论

      本文标题:vue5

      本文链接:https://www.haomeiwen.com/subject/fnyqoftx.html