码迷,mamicode.com
首页 > 其他好文 > 详细

vue-组件

时间:2018-10-23 18:07:12      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:``   分享图片   The   pre   dev   ons   device   图片   doctype   

组件的注册

全局组件

<!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">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
</head>
<body>
<div id="app1">
    <!-- 全局组件是共用的 -->
    <zjm></zjm>
</div>
<hr>
<div id="app2">
    <!-- 多次调用 -->
    <zjm></zjm>
    <zjm></zjm>
</div>
<script>
    // 全局组件 Vue.component("组件的名称",{template: ``})
    Vue.component("zjm", {
        template: `<div><h1>这是组件</h1></div>`,
    });

    const app = new Vue({  // 作用域1
        el: "#app1",
    })

    const app2 = new Vue({  // 作用域2
        el: "#app2"
    })
</script>
</body>
</html>

 

 

局部注册

<!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">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <jbzj></jbzj>
</div>
<script>
    // 定义局部组件
    let zjm = {
        template: `<div>
                        <h1>{{name}}</h1>
                        <button @click="my_click">点击666</button>
                    </div>`,
        data: function () {
            return {
                name: "我是局部组件"
            }
        },
        methods: {
            my_click: function () {
                alert(666)
            }
        }

    };

    const app = new Vue({
        el: "#app",
        components: {  // 注册局部组件
            jbzj: zjm, 
        }
    })
</script>
</body>
</html>

 

 

子组件注册

<!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">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <father></father>
</div>
<script>
    let zzj = {
        template: `<div><h2>这是子组件</h2></div>`,
    };

    let fzj = {
        template: `<div>
                    <h1>这是父组件</h1>
                    <child></child>
                    </div>`,
        components: {
            child: zzj,  // 注册子组件  
        }
    };

    const app = new Vue({
        el: "#app",
        components: {
            father: fzj  // 注册父组件
        }
    })
</script>
</body>
</html>

技术分享图片

 

vue-组件

标签:``   分享图片   The   pre   dev   ons   device   图片   doctype   

原文地址:https://www.cnblogs.com/c-x-m/p/9837588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!