01 局部组件的创建和使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app">
        <!--3.使用子组件-->
        <App></App>
    </div>
    <script src="./vue.js"></script>
    <script>
        //App组件 html+css+js
        //使用局部组件的打油诗:建子 挂子 用子
        //1.创建组件和导入
        //注意:在组件中这个data必须是一个函数,返回一个对象
        const App = {
            data(){
                return {
                   msg:'我是组件'
                }
            },
            template:`
                <div>
                    <h3>{{msg}}</h3>
                    <button @click="handleClick">按钮</button>
                </div>`,
            methods:{
                handleClick(){
                    this.msg = '学习组件'
                }
            }
        }

        new Vue({
            el:'#app',
            data:{

            },
            components:{
                //2.挂载子组件
                App
            }
        })

    </script>
</body>
</html>
posted @ 2020-12-28 17:14  *!Walter!*  阅读(94)  评论(0编辑  收藏  举报