Home > Web Front-end > JS Tutorial > Communication between components in Vue.js - dynamic property transfer

Communication between components in Vue.js - dynamic property transfer

php中世界最好的语言
Release: 2018-03-13 14:17:30
Original
2226 people have browsed it

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 &#39;./components/a.vue&#39;
  export default {    components: {
      ComA
    },
    data () {      return {        myVal: &#39;&#39;
      }
    }
  }</script>
Copy after login

Sub-component a.vue

<template>
  <div>
    {{hello}}
    {{ myValue }}  </div></template><script>
  export default {//    声明number属性//    未指定类型//    props: [&#39;number&#39;],//    指定类型
    props: {      &#39;my-value&#39;: [Number, String]
    },
    data () {      return {        hello: &#39;I am componnet a&#39;
      }
    }
  }</script>
Copy after login

Communication between components in Vue.js - dynamic property transfer

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>
Copy after login

com-a component

<template>
  <div class="hello">
    {{hello}}
    {{ myValue }}
  //给插槽设置默认值    <slot>no slot</slot>
  </div></template>
Copy after login

Communication between components in Vue.js - dynamic property transfer

If there is no content in the passed slot, it is empty

<com-a :my-value="myVal"></com-a>
Copy after login

Set the default value for the slot

<slot>no slot</slot>
Copy after login

Then display

Communication between components in Vue.js - dynamic property transfer

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>
Copy after login

com-a component

Copy after login

execution result:

Communication between components in Vue.js - dynamic property transfer

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template