Home >Web Front-end >JS Tutorial >Detailed explanation of vue.js data transfer and data distribution slot examples

Detailed explanation of vue.js data transfer and data distribution slot examples

小云云
小云云Original
2018-05-14 17:23:252356browse

This article mainly analyzes the relevant knowledge of vue.js data transfer and data distribution slot through code examples. Friends who are interested in this aspect can refer to it. I hope it can help everyone.

1. Data transfer between components

1. The parent component obtains the data of the child component

*The child component transfers its own data , sent to the parent

*vm.$emit(event name, data);

*v-on: @

Example usage: when the send button is clicked , "111" becomes "I am the data of the child component"

76c82f278ac045591c9159d381de2c57
9fd01892b579bba0c343404bcccd70fb
93f0f5c25f18dab9d176bd4f6de5d30e
  a80eb7cbb6fff8b0ff70bae37074b813
  b2386ffb911b14667cb8f0f91ea547a7父级获取子级的数据6e916e0f7d1e588d4f442bf645aedb2f
  6ebdbce067bc95e1851e3b8093fd634e2cacc6d41bbb37262a98f745aa00fbf0
  c9ccee2e6ea535a969eb3f532ad9fe89
  531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
e388a4556c0f65e1904146cc1a846bee
  21faa6d2b39cd643d626023a8aaca7f5
  5839c0ca9224f814274f66bf7ba46f9c
94b3e26ee717c64999d7867364b1b4a3
d477f9ce7bf77f53fbcf36bec1b69b7a
  45a2772a6b6107b401db3c9b82c049c2我是父级 -> {{msg}}54bdf357c58b8a65c66d7c19c8e4d114
  //自动调用get方法,@child-msg和下面的this.$emit('child-msg',this.a)相对应
  369642beebf5fc7b730ea03cb679d725eaf2859ee44b5934442adb5b52b718da
21c97d3a051048b8e55e3c8f199a54b2
d477f9ce7bf77f53fbcf36bec1b69b7a
  684271ed9684bde649abda8831d4d355子组件-39528cedfa926ea0c01e69ef5b2ea9b0
  cd71a9f1a461f9e485615a1e4f95e5d8
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        data:function(){
          return {
            msg:111,
            msg2:'我是父组件的数据'
          }
        },
        template:'#aaa',
        methods:{
          //这里的msg实际上就是子组件传递给父组件的数据
          get:function(msg){
            this.msg=msg;
          }
        },
        components:{
          'bbb':{
            data:function(){
              return {
                a:'我是子组件的数据'
              }
            },
            template:'#bbb',
            methods:{
              send:function(){
                this.$emit('child-msg',this.a);
              }
            }
          }
        }
      }
    }
  });
2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

2. The child component obtains the data of the parent component

When calling the child component:

d7b114c020e498efc92386ccdf352a6eeaf2859ee44b5934442adb5b52b718da

Within subcomponent:

props:['m','myMsg']
props:{
'm':String,
'myMsg':Number
        }
76c82f278ac045591c9159d381de2c57
9fd01892b579bba0c343404bcccd70fb
93f0f5c25f18dab9d176bd4f6de5d30e
  a80eb7cbb6fff8b0ff70bae37074b813
  b2386ffb911b14667cb8f0f91ea547a7自己获取父级的数据6e916e0f7d1e588d4f442bf645aedb2f
  6ebdbce067bc95e1851e3b8093fd634e2cacc6d41bbb37262a98f745aa00fbf0
  c9ccee2e6ea535a969eb3f532ad9fe89
  531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
e388a4556c0f65e1904146cc1a846bee
  e388a4556c0f65e1904146cc1a846bee{{a}}94b3e26ee717c64999d7867364b1b4a3
  21faa6d2b39cd643d626023a8aaca7f5
    {{msg}}
  5839c0ca9224f814274f66bf7ba46f9c
94b3e26ee717c64999d7867364b1b4a3

d477f9ce7bf77f53fbcf36bec1b69b7a
  4a249f0d628e2318394fd9b75b4636b111111473f0a7621bec819994bb5020d29372a
  24ba698398d3c67097270230bd00cab2eaf2859ee44b5934442adb5b52b718da
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
  var vm=new Vue({
    el:'#box',
    data:{
      a:'a'
    },
    components:{
      'aaa':{
        data:function(){
          return {
            msg:111,
            msg2:'我是父组件的数据'
          }
        },
        template:'#aa',
        components:{
          'bbb':{
            props:{
              'mmm':String,
              'myMsg':Number
            },
            template:'684271ed9684bde649abda8831d4d355我是bbb组件->{{mmm}} 0c6dc11e160d3b678d68754cc175188a {{myMsg}}39528cedfa926ea0c01e69ef5b2ea9b0'
          }
        }
      }
    }
  });

2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

Run result:

2. Content distribution:

Vue.js provides a way to mix the content of the parent component and the template of the child component itself: slot, used to occupy a position

1. Basic usage

76c82f278ac045591c9159d381de2c57
9fd01892b579bba0c343404bcccd70fb
93f0f5c25f18dab9d176bd4f6de5d30e
  a80eb7cbb6fff8b0ff70bae37074b813
  b2386ffb911b14667cb8f0f91ea547a7slot保留原来的位置6e916e0f7d1e588d4f442bf645aedb2f
  6ebdbce067bc95e1851e3b8093fd634e2cacc6d41bbb37262a98f745aa00fbf0
  c9ccee2e6ea535a969eb3f532ad9fe89
  531ac245ce3e4fe3d50054a55f265927

9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
e388a4556c0f65e1904146cc1a846bee
  21faa6d2b39cd643d626023a8aaca7f5
    ff6d136ddc5fdfeffaf53ff6ee95f185
      25edfb22a4f469ecb59f1190150159c61111bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c62222bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c63333bed06894275b65c1ab86501b08a632eb
    929d1f5ca49e04fdcb27f9465b944689
  5839c0ca9224f814274f66bf7ba46f9c
  f32b48428a809b51f04d3228cdf461fa
  21faa6d2b39cd643d626023a8aaca7f5
  5839c0ca9224f814274f66bf7ba46f9c
94b3e26ee717c64999d7867364b1b4a3
d477f9ce7bf77f53fbcf36bec1b69b7a
  4a249f0d628e2318394fd9b75b4636b1xxxx473f0a7621bec819994bb5020d29372a
  58cb293b8600657fad49ec2c8d37b472这是默认的情况7971cf77a46923278913ee247bc958ee
  e388a4556c0f65e1904146cc1a846beewelcome vue94b3e26ee717c64999d7867364b1b4a3
21c97d3a051048b8e55e3c8f199a54b2

3f1c4e4b6b16bbbd69b2ee476dc4f83a
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        template:'#aaa'
      }
    }
  });

2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

Running result: The content in the ul tag is not overwritten. If slot is not used, the content in the ul tag will be overwritten

2. The name attribute of the slot

76c82f278ac045591c9159d381de2c57
9fd01892b579bba0c343404bcccd70fb
93f0f5c25f18dab9d176bd4f6de5d30e
  a80eb7cbb6fff8b0ff70bae37074b813
  b2386ffb911b14667cb8f0f91ea547a7slot中name属性的使用6e916e0f7d1e588d4f442bf645aedb2f
  6ebdbce067bc95e1851e3b8093fd634e2cacc6d41bbb37262a98f745aa00fbf0
  c9ccee2e6ea535a969eb3f532ad9fe89
  531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
e388a4556c0f65e1904146cc1a846bee
  21faa6d2b39cd643d626023a8aaca7f5
    ccd4047109ea07979870c366d0ef0770    //这里slot的名字要与下面slot中name属性相对应
      25edfb22a4f469ecb59f1190150159c61111bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c62222bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c63333bed06894275b65c1ab86501b08a632eb
    929d1f5ca49e04fdcb27f9465b944689
    0893ef879e1c9084569d28fbef0254fb    //用法同上
      25edfb22a4f469ecb59f1190150159c6111bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6222bed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6333bed06894275b65c1ab86501b08a632eb
    f6f112ef45f603be226bc581f9dd5e90
  5839c0ca9224f814274f66bf7ba46f9c
  f32b48428a809b51f04d3228cdf461fa
  21faa6d2b39cd643d626023a8aaca7f5
  5839c0ca9224f814274f66bf7ba46f9c
94b3e26ee717c64999d7867364b1b4a3
d477f9ce7bf77f53fbcf36bec1b69b7a  
  4a249f0d628e2318394fd9b75b4636b1xxxx473f0a7621bec819994bb5020d29372a
  ef79bf350fa1af528e8524e7cce352ab这是默认的情况7971cf77a46923278913ee247bc958ee      //设置name属性,给slot命名
  e388a4556c0f65e1904146cc1a846beewelcome vue94b3e26ee717c64999d7867364b1b4a3
  3ead1386f1025b804255952fab9f3bdf这是默认的情况27971cf77a46923278913ee247bc958ee
21c97d3a051048b8e55e3c8f199a54b2

3f1c4e4b6b16bbbd69b2ee476dc4f83a
  var vm=new Vue({
    el:'#box',
    data:{
      a:'aaa'
    },
    components:{
      'aaa':{
        template:'#aaa'
      }
    }
  });

2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

Running results:

Related recommendations:

Vue content distribution slot

js component SlotMachine implements image switching effect to create lottery system_javascript skills

How to transfer data between vue.js components

The above is the detailed content of Detailed explanation of vue.js data transfer and data distribution slot examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn