首页 > 其他分享 >Vue.js -- 动态组件&异步组件

Vue.js -- 动态组件&异步组件

时间:2022-11-12 23:31:47浏览次数:43  
标签:异步 Vue -- currendItem app component myInput 组件

动态组件

根据数据的变化,动态切换组件的显示。

点击切换组件

首先定义两个子组件

      // 子组件
       app.component('myInput', {
            template:`
            <input />
            `
        })
       // 子组件
       app.component('myText', {
            template:`
            <div>text</div>
            `
        })

然后再父组件中调用之前定义的两个子组件和切换按钮。

            <myInput v-show="currendItem === 'myInput'"/>
            <myText v-show="currendItem === 'myText'"/>
            <button>切换</button>

最后绑定点击事件,设置data。

 			data(){
                return {
                    currendItem: 'myInput'
                }
            },
            methods: {
                handleClick(){
                    this.currendItem === 'myInput' ? this.currendItem = 'myText' : this.currendItem = 'myInput';
                }
            },

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>动态组件和异步组件</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
    <script>
        // 父组件
        const app = Vue.createApp({
            data(){
                return {
                    currendItem: 'myInput'
                }
            },
            methods: {
                handleClick(){
                    this.currendItem === 'myInput' ? this.currendItem = 'myText' : this.currendItem = 'myInput';
                }
            },
            template:`
            <myInput v-show="currendItem === 'myInput'"/>
            <myText v-show="currendItem === 'myText'"/>
            <button @click="handleClick">切换</button>
            `
        })
       // 子组件
       app.component('myInput', {
            template:`
            <input />
            `
        })
       // 子组件
       app.component('myText', {
            template:`
            <div>text</div>
            `
        })
        const vm = app.mount('#root');
    </script>
</body>
</html>

页面效果: 在这里插入图片描述 点击后 在这里插入图片描述

优化点击切换组件

使用component标签优化代码

<component>标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件。

使用component标签优化之前定义的两个子组件和切换按钮。

			<component :is="currendItem"/>
            <button @click="handleClick">切换</button>

使用keep-alive标签完善功能

<keep-alive>标签是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM。

使用keep-alive标签对component标签优化之后的代码进一步完善,==将输入框切换之前的状态存储下来==。

			<keep-alive>
                <component :is="currendItem"/>
            </keep-alive>
            <button @click="handleClick">切换</button>

异步组件

异步执行某些组件的逻辑

首先调用Vue的defineAsyncComponent 方法存储在AsyncComp中,然后在defineAsyncComponent 方法中传入一个函数,这个函数返回一个Promise。

		const AsyncComp = Vue.defineAsyncComponent(() => {
           return new Promise((resolve, reject) => {
               setTimeout(() => {
                resolve({
                    template: `<div>我是异步组件</div>`
                })
               }, 5000);
           })
       })

最后将AsyncComp写入子组件。

app.component('asyncItem', AsyncComp)

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>动态组件和异步组件</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root"></div>
    <script>
        // 父组件
        const app = Vue.createApp({         
            template:`
            <asyncItem />
            `
        })

        const AsyncComp = Vue.defineAsyncComponent(() => {
           return new Promise((resolve, reject) => {
               setTimeout(() => {
                resolve({
                    template: `<div>我是异步组件</div>`
                })
               }, 5000);
           })
       })
       // 子组件
       app.component('asyncItem', AsyncComp)
        const vm = app.mount('#root');
    </script>
</body>
</html>

打开浏览器五秒后页面渲染出:我是异步组件

总结

动态组件

==使用component标签动态绑定组件==

异步组件

==调用defineAsyncComponent 方法传入一个函数,函数返回Promise==

结语

本小节到此结束,谢谢大家的观看!

如有问题欢迎各位指正

标签:异步,Vue,--,currendItem,app,component,myInput,组件
From: https://blog.51cto.com/u_15718546/5846847

相关文章

  • 小树不修不直溜,Cube不修性能堪忧
    小树不修不直溜,Cube不修性能堪忧坚持原创,写好每一篇文章ApacheKylin的Cube的构建是根据维度来进行构建的,如果是m个维度,就会产生2的m次方个Cuboid,如果维度过多的话,产生......
  • 常用的包管理工具的简单使用
    brew(homebrew)brew是macOS系统的包管理软件。Homebrew是以最简单,最灵活的方式来安装苹果公司在MacOS中不包含的UNIX工具。homebrew基于Git仓库管理的。可以分为五个库:软件......
  • 51.N-Queens
    The n-queens puzzleistheproblemofplacing n queensonan nxn chessboardsuchthatnotwoqueensattackeachother.Givenaninteger n,return all......
  • homework2软件方法论
        什么是软件工程方法论?     1.软件工程是一个方法论,就是我们在开始一个项目时,大体框架一定要有这么一个概念,而具体实施时,必须根据公司一些特点,优化......
  • 单服务器部署CTFd+whale踩坑
    单服务器部署CTFd+whale踩坑环境:os:ubuntu16Docker:Version:20.10.7Docker-compose:versionv2.12.1参考:https://www.zhaoj.in/read-6333.html/comment-page-1#_......
  • I++与++I 面试题
    程序员面试宝典第28页,书中的答案是9,49而正确的答案应该是,12,423*4=126*7=421#include<stdio.h>2#defineproduct(x)((x)*(x))3intmain(intargc,char......
  • 56. Merge Intervals
    Givenanarray of intervals where intervals[i]=[starti,endi],mergealloverlappingintervals,andreturn anarrayofthenon-overlappingintervalstha......
  • ......
  • ......
  • 第二章--使用自定义类
    代码:publicclassClassAndObjectTest{ publicstaticvoidmain(String[]args) { //创建类的实例,定义一个对象变量引用这一实例 MyClassobj=newMyClass(); //......