Vue 组件化

在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <!--组件名就是调用组件时的标签-->
        <my-hello></my-hello>

    </div>
    
    <template id="content">
        <div>
            <h3>自定义全局组件</h3>
            <p>MyHello</p>
            <p>name:{{name}}</p>
        </div>
    </template>
</body>

<script>
    const app = Vue.createApp({
        data(){
        }
    })
    app.component('my-hello',{ // 组件名使用短横线相连,全小写
        template:'#content',
        data(){
            return{
                name:'tom'
            }
        }
    })

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

在这里插入图片描述

组件多次调用,他们之间是互不影响的

<body>
    <div id="app">
        <!--组件名就是调用组件时的标签-->
        <my-hello v-for="(item,index) in 3" :key="index"></my-hello>

    </div>
    
    <template id="content">
        <div>
            <h3>自定义全局组件</h3>
            <p>MyHello</p>
            <p>name:{{name}}</p>
            <input type="text" v-model="name">
        </div>
    </template>
</body>

<script>
    const app = Vue.createApp({
        data(){
        }
    })
    app.component('my-hello',{ // 组件名使用短横线相连,全小写
        template:'#content',
        data(){
            return{
                name:'tom'
            }
        }
    })
    const vm = app.mount('#app')
</script>

在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <comp-a></comp-a>

    </div>
    
    <template id="a">
        <div>
           <h3>组件A</h3>
           <p>{{msg}}</p>
        </div>
    </template>
</body>

<script>
    const app = Vue.createApp({
        components:{
            'comp-a':{
                template:'#a',
                data(){
                    return{
                        msg:'aaa'
                    }
                }
            }
        }

    }).mount('#app')

</script>

在这里插入图片描述

<body>
    <div id="app">
        <comp-a></comp-a>
        
    </div>
    
    <template id="a">
        <div>
           <h3>组件A</h3>
           <p>{{msg}}</p>
           <comp-b></comp-b>
        </div>
    </template>

    <template id="b">
        <div>
           <h3>组件B</h3>
           <p>{{msg}}</p>
        </div>
    </template>
</body>

<script>
    const app = Vue.createApp({
        components:{ // 局部组件
            'comp-a':{  // 父组件
                template:'#a',
                data(){
                    return{
                        msg:'aaa'
                    }
                },
                components:{ //局部组件
                    'comp-b':{ //子组件 
                        template:'#b',
                        data(){
                            return{
                                msg:'bbb'
                            }
                        }
                    }

                }
            }
        }

    }).mount('#app')

</script>

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <my-hello></my-hello>
        
    </div>

    <template id="hello">
        <div>
            <h2>hello父组件</h2>
            <h2>访问自己的数据: {{ msg }},{{info}},{{user.username}}</h2>
            <hr>
            <my-world :msg="msg" :info="info" :user="user"></my-world>
        </div>
    </template>

    <template id="world">
        <div>
            <h2>world子组件</h2>
            <h3>访问父组件数据:{{msg}},{{info}},{{user.username}}</h3>
            
        </div>
    </template>



</body>

<script>
    const app = Vue.createApp({
        components:{
            'my-hello':{ //父组件
                template:'#hello',
                data(){
                    return{
                        msg:'welcome',
                        info:'vue',
                        user:{
                            id:1001,
                            username:'admin',
                            password:'123'
                        }
                    }
                },
                components:{
                    'my-world':{ //子组件
                        template:'#world',
                        // 接收方式一
                        // props:['msg','info','user']
                        // 接收方式二
                        props:{
                            msg:String,
                            info:{
                                type:String,
                                default:'csdn'
                            },
                            user:{
                                type:Object
                            }
                        }

                    }
                }
            }
        }

    }).mount('#app')

</script>


在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <my-hello></my-hello>

    </div>

    <template id="hello">
        <div>
            <h2>hello父组件</h2>
            <hr>
            <my-world  @e-data="get"></my-world>
            <h2>访问子组件数据:{{sex}}</h2>
        </div>
    </template>

    <template id="world">
        <div>
            <h2>world子组件</h2>
            <h2>访问自己的数据:{{sex}}</h2>
            <button @click="send">将子组件的数据传递给父组件</button>
        </div>
    </template>


</body>

<script>
    const app = Vue.createApp({
        components: {
            'my-hello': { //父组件
                template: '#hello',
                data() {
                    return {
                        sex:null
                    }
                },
                methods: {
                    get(sex) {
                        this.sex = sex

                    }
                },
                components: {
                    'my-world': { //子组件
                        template: '#world',
                        data() {
                            return {
                                sex: 'male'
                            }
                        },
                        methods:{
                            send(){
                                // 使用$emit() 触发一个自定义事件
                                this.$emit('e-data',this.sex)
                            }
                        }


                    }
                }
            }
        }

    }).mount('#app')

</script>

在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <button @click="flag='CompA'">组件A</button>
        <button @click="flag='CompB'">组件B</button>
        <button @click="flag='CompC'">组件C</button>
        <hr>
        <component :is="flag"></component>

    </div>
</body>

<script>
    Vue.createApp({
        data(){
            return{
                flag:'CompA'

            }
        },
        components:{
            CompA:{
                template:'<h3>组件A</h3>'
            },
            CompB:{
                template:'<h3>组件B</h3>'
            },
            CompC:{
                template:'<h3>组件C</h3>'
            },
        }
    }).mount('#app')
</script>

在这里插入图片描述

在这里插入图片描述

默认情况下点击A组件,A组件会被创建,点击B组件创建B组件的同时销毁A组件

<body>
    <div id="app">
        <button @click="flag='CompA'">组件A</button>
        <button @click="flag='CompB'">组件B</button>
        <button @click="flag='CompC'">组件C</button>
        <hr>

        
            <component :is="flag"></component>
        
    </div>
</body>

<script>
    Vue.createApp({
        data(){
            return{
                flag:'CompA'

            }
        },
        components:{
            CompA:{
                template:'<h3>组件A</h3>',
                created(){
                    console.log('组件A-------created')
                },
                unmounted(){
                    console.log('组件A-------unmounted')
                }
            },
            CompB:{
                template:'<h3>组件B</h3>',
                created(){
                    console.log('组件B-------created')
                },
                unmounted(){
                    console.log('组件B-------unmounted')
                }
            },
            CompC:{
                template:'<h3>组件C</h3>',
                created(){
                    console.log('组件C-------created')
                },
                unmounted(){
                    console.log('组件C-------unmounted')
                }
            },
        }
    }).mount('#app')
</script>

在这里插入图片描述

缓存组件不会销毁切换之前的组件

<keep-alive>
            <component :is="flag"></component>
        </keep-alive>

在这里插入图片描述

在这里插入图片描述

<body>
    <div id="app">
        <comp-a>
            <p>111</p>
        </comp-a>
        <hr>
        <comp-a>
            <p>222</p>
        </comp-a>
    </div>

    <template id="a">
        <div>
            <h3>组件A</h3>
        <slot></slot>
        </div>

    </template>
</body>

<script>
    Vue.createApp({
        data(){
            return{
                flag:''

            }
        },
        components:{
            'comp-a':{
                template:'#a'
            }
        }
    }).mount('#app')
</script>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季布,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值