zoukankan      html  css  js  c++  java
  • Vue_(组件通讯)动态组件

      动态组件  传送门

      

      在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件

      动态组件的使用:需要使用内置组件<component></component>,根据 :is 的值决定显示哪个组件,:is的值是要显示的组件id

      目录结构

      

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Gary</title>
        </head>
        <body>
            <div id="GaryId">
                <button @click="selectedName = 'my-component-a'"> a </button>
                <button @click="selectedName = 'my-component-b'"> b </button>
                <button @click="selectedName = 'my-component-c'"> c </button>
                
                <component :is="selectedName"></component>
    
            </div>
        </body>
        
        <script type="text/javascript" src="../js/vue.js" ></script>
        <script type="text/javascript">
                
                new Vue({
                    data:{
                        selectedName:'my-component-a'
                    },
                    components:{
                        "my-component-a":{
                            template:"<h1>Hello Vue</h1>"
                        },
                        "my-component-b":{
                            template:"<a href='https://www.cnblogs.com/1138720556Gary/'>Gary博客</a>"
                        },
                        "my-component-c":{
                            template:"<p>欢迎来到Gary博客</p>"
                        }
                    }
                }).$mount("#GaryId");
        </script>
    </html>
    Gary_dynamic_component.html

    实现过程

      Vue的data域中为组件申请一个名称

        data:{
            selectedName:'my-component-a'
        },    

      <div>标签中标明使用'my-component-a'组件模板

            <div id="GaryId">
                <button @click="selectedName = 'my-component-a'"> a </button>
                <button @click="selectedName = 'my-component-b'"> b </button>
                <button @click="selectedName = 'my-component-c'"> c </button>
                
                <component :is="selectedName"></component>
    
            </div>

      在Vue的components中标明组件模块

      new Vue({
                    data:{
                        selectedName:'my-component-a'
                    },
                    components:{
                        "my-component-a":{
                            template:"<h1>Hello Vue</h1>"
                        },
                        "my-component-b":{
                            template:"<a href='https://www.cnblogs.com/1138720556Gary/'>Gary博客</a>"
                        },
                        "my-component-c":{
                            template:"<p>欢迎来到Gary博客</p>"
                        }
                    }
                }).$mount("#GaryId");
    (如需转载学习,请标明出处)
  • 相关阅读:
    洛谷 P2807 三角形计数
    洛谷 P1727 计算π
    洛谷 P1595 信封问题
    洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens
    3.1、spark集群运行应用
    移动端自适应
    【Flex布局】
    【pm2】
    【安全】
    【Bower】
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/10468092.html
Copyright © 2011-2022 走看看