This time I will bring you the communication between Vue.js components - dynamic property transfer, communication between Vue.js components - what are the precautions for dynamic attribute transfer , the following is a practical case, let’s take a look.
The content in the form is dynamically displayed in the sub-component
<template> <div> <input> <com-a></com-a> </div></template><script> import ComA from './components/a.vue' export default { components: { ComA }, data () { return { myVal: '' } } }</script>
Sub-component a.vue
<template> <div> {{hello}} {{ myValue }} </div></template><script> export default {// 声明number属性// 未指定类型// props: ['number'],// 指定类型 props: { 'my-value': [Number, String] }, data () { return { hello: 'I am componnet a' } } }</script>
Communication between components - Dynamic attribute transfer
Slot slot
Pass a template to the child component
<com-a :my-value="myVal"> <p>我是一个插槽</p> <span>123456</span></com-a>
com-a component
<template> <div class="hello"> {{hello}} {{ myValue }} //给插槽设置默认值 <slot>no slot</slot> </div></template>
If there is no content in the passed slot, it is empty
<com-a :my-value="myVal"></com-a>
Set the default value for the slot
<slot>no slot</slot>
Then display
Named Slot
<template> <div id="myapp"> <!--具名插槽--> <com-a :my-value="myVal"> <p slot="header">xxxx header</p> <p slot="footer">yyyy footer</p> </com-a> </div></template>
com-a component
{{hello}} {{ myValue }}
no header 乱七八糟的内容
no footer
execution result:
I believe you have mastered the method after reading the case in this article, more Please pay attention to php Chinese websiteOther related articles for exciting content!
Recommended reading:
Event binding of Vue.js - Form event binding
vue of Vue.js Tag attributes and conditional rendering
The above is the detailed content of Communication between components in Vue.js - dynamic property transfer. For more information, please follow other related articles on the PHP Chinese website!