Vue动态组件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        *{padding: 0;margin: 0;}
        ul{
            display: flex;
            list-style: none;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            justify-content:space-between;
            height: 60px;
        }
        li{
            background: royalblue;
            width: 33%;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
    <script src="../vue.js"></script>

    <div id="app">
        <!-- <keep-alive>标签:保留状态避免重新渲染-->
        <!-- <component>标签:元素,动态地绑定多个组件到它的is属性 -->
        <keep-alive>
        <component :is="isShow"></component>
        </keep-alive>
        <ul>
            <li @click="handleHomeClick">首页</li>
            <li @click="handleListClick">列表页</li>
            <li @click="handleShopcarClick">购物车页</li>
        </ul>
    </div>
    <script>

        new Vue({
            el: "#app",
            components: {//局部组件
                home:{
                    template:`<div>home<input type="text"></div>`
                },
                list:{
                    template: `<div>list</div>`
                },
                shopcar:{
                    template: `<div>shopcar</div>`
                },
            },
            data: {
                isShow:"home"
            },
            methods: {
                // 改变 is属性
                handleHomeClick() {
                    this.isShow = "home"
                },
                handleListClick() {
                    this.isShow = "list"
                },
                handleShopcarClick() {
                    this.isShow = "shopcar"
                }
            },
            
        })
    </script>
</body>
</html>
效果图
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容