0
点赞
收藏
分享

微信扫一扫

VUE组件注册练习

他说Python 2022-01-20 阅读 149
<!-- 
 * @Description: 
 * @Autor: leechoy
 * @Date: 2022-01-20 00:55:52
 * @IDE: Visual Studio Code
  -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 组件数据相互独立 -->
        <button-test @test='handle'></button-test>
        <button-test></button-test>
        <button-test></button-test>
        <!-- 记得用短横线 -->
        <hello-lee></hello-lee>
    </div>
</body>
<script>
    // 注册一个全局组件(全局组件不能调用局部组件)
    Vue.component('button-test', {
        data() {
            return {
                name: 'leechoy'
            }
        },
        // 模板必须有一个根元素
        template: '<div  @click="handle">123</div>',
        methods: {
            handle() {
                alert(this.name)
                this.$emit('test', {
                    id: '12345'
                });
            }
        }
    })

    // 局部组件
    var helloLee = {
        template: '<h1> hellolee</h1>'
    }
    var helloWang = {
        template: '<h1> helloWang</h1>'
    }
    var helloSong = {
        template: '<h1> helloSong</h1>'
    }
    var vm = new Vue({
        el: '#app',
        data: {

        },
        // 引入组件
        components: {
            helloLee,
            helloSong,
            helloWang
        },
        methods: {
            handle(e) {
                alert(e.id)
            }
        }
    })
</script>

</html>
举报

相关推荐

0 条评论