Vue components dynamic components detailed explanation

Vue components dynamic components detailed explanation

When the array changes, dynamically load the corresponding data

Scenario : Click on different component names and the interface will display the corresponding components

Step 1 : Import required components

Step 2 : Click the tab and add the corresponding component name into the array

Step 3 : Use dynamic components and bind the :is attribute to the component name

<div v-for="(item, index) in componentData" :key="index">
  <components :is="item.componentName"/>
</div>

Example : Monitoring property changes in an object, deep monitoring

<!-- DynamicComponent.vue -->
<template>
  <section>
    <div v-for="(item, index) in componentData" :key="index">
      <components :is='item.componentName' :params="item.content" />
    </div>
  </section>
</template>
<script>
import PageOne from './pageComponents/PageOne'
import PageTwo from './pageComponents/PageTwo'
import PageThree from './pageComponents/PageThree'
export default{
  name: 'DynamicComponent',
  components:
    PageOne,
    PageTwo,
    PageThree
  },
  data () {
    return {
      componentData: [
        {
          componentName: 'PageOne',
          content: {
            title: 'Title 1'
          }
        },
        {
          componentName: 'PageTwo',
          content: {
            title: 'Title 2'
          }
        }
      ]
    }
  }
}
</script>
<!-- PageOne -->
<template>
  <section>
    {{content}}
  </section>
</template>
<script>
export default{
  name: 'PageOne',
  props: {
    params: {
      type: Object,
      default: function(){
        return {}
      }
    }
  },
  data () {
    return {
      content: this.params.title
    }
  },
  watch:
    params: {
      handler(newVal, oldVal){
        this.content = newVal.title
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
<!-- PageTwo -->
<template>
  <section>
    {{content}}
  </section>
</template>
<script>
export default{
  name: 'PageTwo',
  props: {
    params: {
      type: Object,
      default: function(){
        return {}
      }
    }
  },
  data () {
    return {
      content: this.params.title
    }
  },
  watch:
    params: {
      handler(newVal, oldVal){
        this.content = newVal.title
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

When the array changes, dynamically load the corresponding data

Scenario : Click on different component names and the interface will display the corresponding components

Step 1 : Import required components

Step 2 : Click the tab and add the corresponding component name into the array

Step 3 : Use dynamic components and bind the :is attribute to the component name

<div v-for="(item, index) in componentData" :key="index">
  <components :is="item.componentName"/>
</div>

Example : Monitoring property changes in an object, deep monitoring

<!-- DynamicComponent.vue -->
<template>
  <section>
    <div v-for="(item, index) in componentData" :key="index">
      <components :is='item.componentName' :params="item.content" />
    </div>
  </section>
</template>
<script>
import PageOne from './pageComponents/PageOne'
import PageTwo from './pageComponents/PageTwo'
import PageThree from './pageComponents/PageThree'
export default{
  name: 'DynamicComponent',
  components:
    PageOne,
    PageTwo,
    PageThree
  },
  data () {
    return {
      componentData: [
        {
          componentName: 'PageOne',
          content: {
            title: 'Title 1'
          }
        },
        {
          componentName: 'PageTwo',
          content: {
            title: 'Title 2'
          }
        }
      ]
    }
  }
}
</script>
<!-- PageOne -->
<template>
  <section>
    {{content}}
  </section>
</template>
<script>
export default{
  name: 'PageOne',
  props: {
    params: {
      type: Object,
      default: function(){
        return {}
      }
    }
  },
  data () {
    return {
      content: this.params.title
    }
  },
  watch:
    params: {
      handler(newVal, oldVal){
        this.content = newVal.title
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
<!-- PageTwo -->
<template>
  <section>
    {{content}}
  </section>
</template>
<script>
export default{
  name: 'PageTwo',
  props: {
    params: {
      type: Object,
      default: function(){
        return {}
      }
    }
  },
  data () {
    return {
      content: this.params.title
    }
  },
  watch:
    params: {
      handler(newVal, oldVal){
        this.content = newVal.title
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue built-in component component--dynamically render component operations through the is attribute
  • Vue.component property description
  • Detailed explanation of the relationship between Vue and VueComponent
  • vue dynamic component
  • Solve the component tag rendering problem of Vue

<<:  Detailed explanation of MySQL EXPLAIN output columns

>>:  About Jenkins + Docker + ASP.NET Core automated deployment issues (avoid pitfalls)

Recommend

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...

Summary of the differences between count(*), count(1) and count(col) in MySQL

Preface The count function is used to count the r...

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

Vue implements mobile phone verification code login

This article shares the specific code of Vue to i...

How to display percentage and the first few percent in MySQL

Table of contents Require Implementation Code dat...

Example code for implementing transparent gradient effects with CSS

The title images on Zhihu Discovery columns are g...

Docker FAQ

Docker only maps ports to IPv6 but not to IPv4 St...

MySQL 8.0.13 installation and configuration method graphic tutorial

This article shares the installation and configur...

Navicat cannot create function solution sharing

The first time I wrote a MySQL FUNCTION, I kept g...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...

Detailed explanation of linux nslookup command usage

[Who is nslookup?] 】 The nslookup command is a ve...

Mysql stores tree structure through Adjacency List (adjacency list)

The following content introduces the process and ...