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

5 Tips for Protecting Your MySQL Data Warehouse

Aggregating data from various sources allows the ...

Vue Element UI custom description list component

This article example shares the specific code of ...

mysql server is running with the --skip-grant-tables option

The MySQL server is running with the --skip-grant...

JavaScript implements double-ended queue

This article example shares the specific code of ...

A brief analysis of JS original value and reference value issues

Primitive values ​​-> primitive types Number S...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

How to implement a binary search tree using JavaScript

One of the most commonly used and discussed data ...

How to bypass unknown field names in MySQL

Preface This article introduces the fifth questio...

What are your principles for designing indexes? How to avoid index failure?

Table of contents Primary key index Create indexe...

Solution to mysql ERROR 1045 (28000) problem

I encountered mysql ERROR 1045 and spent a long t...

Summary of MySQL character sets

Table of contents Character Set Comparison Rules ...

Docker executes a command in a container outside the container

Sometimes we want to execute a command in a conta...

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

Vue implements dynamic query rule generation component

1. Dynamic query rules The dynamic query rules ar...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....