这篇“Vue动态组件与异步组件怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue动态组件与异步组件怎么使用”文章吧。
何为动态组件
动态组件是根据数据的变化,可以随时切换不同的组件,比如咱们现在要展示一个文本框和输入框,我们想要根据我们data中定义的值去决定是显示文本框还是输入框,如果用以前学的知识去做的话就是使用v-show的方式去做,虽然也能实现,但是比较复杂,代码也多了很多,这时候用动态组件能很好的解决这个问题
何为异步组件
异步组件可以以异步的方式加载组件,实际项目中我们可以把大型的项目拆分成许多小js文件,等使用时再组合,而且可以实现懒加载,有些组件暂时不需要展示给用户的我们可以等用户看到时再加载进来。
示例解析
动态组件
展示一个输入框或者文本,通过一个按钮,点击后可以切换展示,比如当前展示文本,点击按钮就会变为展示输入框,代码如下:
首先我们先定义两个组件,一个展示输入框,一个展示文本
  app.component('input-item',{
        template:`
            <input />
           `
    });
    app.component('common-item',{
        template:`<div>hello world</div>`
    });定义一个currentItem变量用于控制组件的展示
data() {
        return {
            currentItem: 'input-item'
        }
    },使用组件时使用component关键字 ,然后使用
:is = "显示具体组件的依赖数据(本例子中时currentItem)"的方式动态加载组件
template: ` <keep-alive> <component :is="currentItem"/> </keep-alive> <button @click="handleClick">switch</button> `
keep-alive:组件切换时在组件中的值会被清掉,比如输入框中的值,所以需要使用keep-alive来防止值被清理
定义点击按钮后执行的方法,这个方法就是改变current Item的值,让其显示不同的组件
 methods: {
        handleClick(){
            if(this.currentItem === 'input-item'){
                this.currentItem = 'common-item';
            }else{
                this.currentItem = 'input-item';
            }
        }
    }所有代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态组件</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
 const app = Vue.createApp({
    data() {
        return {
            currentItem: 'input-item'
        }
    },
    methods: {
        handleClick(){
            if(this.currentItem === 'input-item'){
                this.currentItem = 'common-item';
            }else{
                this.currentItem = 'input-item';
            }
        }
    },
        template: 
        `
        <keep-alive>
        <component :is="currentItem"/>
        </keep-alive>
        <button @click="handleClick">switch</button>
        `
    });
    app.component('input-item',{
        template:`
            <input />
           `
    });
    app.component('common-item',{
        template:`<div>hello world</div>`
    });
    const vm = app.mount('#root');
</script>
</html>异步组件
假如我们要展示一个文本,这个文本不会马上展示,而是4秒后再展示
首先定义两个组件,一个同步组件,一个异步组件
定义同步组件
 app.component('common-item',{
        template:`<div>hello world</div>`
    })定义异步组件,使用
Vue.defineAsyncComponent定义异步组件
 app.component('async-common-item',
 Vue.defineAsyncComponent(()=>{
        return new Promise((resolve,reject)=>{
            setTimeout(()=>{
                resolve({
                    template:`<div>this is an async component</div>`
                })
            },4000)  
        })
    }));使用组件
 const app = Vue.createApp({
        template: 
        `
        <div>
            <common-item />
            <async-common-item />
        </div>
        `
    });全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>异步组件</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
</body>
<script>
 const app = Vue.createApp({
        template: 
        `
        <div>
            <common-item />
            <async-common-item />
        </div>
        `
    });
    app.component('common-item',{
        template:`<div>hello world</div>`
    })
    // 异步组件:可以通过异步组件的方式动态加载组件,可以把大型项目拆分成许多的小js 文件,使用时再组合
    app.component('async-common-item',
        Vue.defineAsyncComponent(()=>{
        return new Promise((resolve,reject)=>{
            setTimeout(()=>{
                resolve({
                    template:`<div>this is an async component</div>`
                })
            },4000)        
        })
    }));
    const vm = app.mount('#root');
</script>
</html>