vue 动态组件、异步组件

vue 在使用组件时,大部分都在使用同步组件和动态组件,少数部分使用 异步组件。

1. 动态组件 普通做法
背景:一个组件显示,另外一个组件隐藏,反之。


<script>

    // 创建 vue实例
   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:  `
    <input-item v-show=" currentItem === 'input-item' "/>
    <common-item v-show=" currentItem === 'common-item' "/>
    <button @click="handleClick">切换</button>
   `
    });

    // 自定义组件
    app.component('input-item', {
        template:  `
        <input />
        `
    });

    app.component('common-item', {
        template:  `
        <div> hello world</div>
        `
    });

    const vm = app.mount('#root');
    
</script>

2. 动态组件 升级版


<script>

    // 创建 vue实例
   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'
            }
        }
    },

    // 动态组件:根据 component这个标签,来随时动态切换组件
    // 动态组件 使用 component 根据 参数currentItem 决定显示哪个控件
    // <keep-alive> 缓存组件,会将组件内的数据进行缓存,下次使用时直接从缓存中获取
    template:  `
    <keep-alive>
        <component :is="currentItem" />
    </keep-alive>
    <button @click="handleClick">切换</button>
   `
    });

    // 自定义组件
    app.component('input-item', {
        template:  `
        <input />
        `
    });

    app.component('common-item', {
        template:  `
        <div> hello world</div>
        `
    });

    const vm = app.mount('#root');
    
</script>

3. 异步组件


<script>

// defineAsyncComponent
// Promise
// 异步
const asynvCommonItem = Vue.defineAsyncComponent(()=> {
        return new Promise ((resolve, reject)=> {
            setTimeout(() => {
                resolve({
                    template:  `
                     <div> asynvCommonItem </div>
                    `
                })
            }, 4000);
        })
    });

    // 创建 vue实例
   const app = Vue.createApp({


    // 异步组件
    // 异步组件:是异步执行某些组件的逻辑,这叫做异步组件
    template:  `
    <div>
        <common-item />
        <async-common-item />
    </div>
   `
    });

    // 同步组件
    app.component('common-item', {
        template:  `
        <div> hello world</div>
        `
    });

    // 异步组件
    app.component('async-common-item', asynvCommonItem);

    const vm = app.mount('#root');
    
</script>

vue 基础使用大致这些,下面会输出 vue和css 动画的一些操作。

相关推荐

  1. vue 动态组件异步组件

    2024-04-12 08:18:04       15 阅读
  2. vue2 实战:动态组件,异步组件

    2024-04-12 08:18:04       19 阅读
  3. vue 异步加载组件

    2024-04-12 08:18:04       40 阅读
  4. Vue3 异步组件defineAsyncComponent

    2024-04-12 08:18:04       19 阅读
  5. vue如何实现异步组件

    2024-04-12 08:18:04       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-12 08:18:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-12 08:18:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-12 08:18:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-12 08:18:04       20 阅读

热门阅读

  1. CSS3进阶技巧:Flexbox布局实战与高级应用

    2024-04-12 08:18:04       16 阅读
  2. element-ui自定义table表头,render-header使用

    2024-04-12 08:18:04       15 阅读
  3. 箭头函数和普通函数的区别

    2024-04-12 08:18:04       13 阅读
  4. Restful API接口规范(以Django为例)

    2024-04-12 08:18:04       13 阅读
  5. CIrcuits--Sequential--Finite_1

    2024-04-12 08:18:04       12 阅读
  6. Thinkphp下载图片至压缩包

    2024-04-12 08:18:04       14 阅读