<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue3组件</title>
    <script src="./vue3.js"></script>
</head>
<body>
    <div id="app">
        {{msg}}
        <br>
        ----------
        <br>
        全局组件:<child></child>
        <br>
        局部组件:<comp></comp>
    </div>
    <template id="comp">
        子组件内容 {{msg}} {{count}}
        <button @click="count++">add</button>
    </template>
    <script>
        const {createApp} = Vue
        const comp = {
            data:()=>({
                msg:'子组件',
                count:0
            }),
            template:"#comp",
            methods:{ }
        }
        const options = {
            data:()=>({
                msg:'vue3组件'
            }),
            components:{
                comp
            }
        }
        
        const app = createApp(options)
        
        // 创建一个全局组件
        app.component('child',comp)
  
        app.mount('#app')


    </script>
</body>
</html>

在这里插入图片描述

Logo

前往低代码交流专区

更多推荐