第12组件基础

1. 全局组件创建

效果图: 1、创建新组件,  2. 实例化对象,  3.调用组件

 

 代码:

<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title></title>
    <link type="text/css" rel="stylesheet" href=" "/>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="one">
    <!-- 3. 最后调用组件 -->
    <button-counter></button-counter>
    <button-counter></button-counter>
</div>
<script type="text/javascript">
    // 1.先在vue中定义一个名为 button-counter 的新组件
    Vue.component('button-counter',{
        // 定义数据函数  =》 每个实例返回独立的数据
        data:function(){
            return {
                count: 0
            }
        },
        // 定义组件模板内容
        template:'<button v-on:click="++count">You clicked me {{ count }} times</button>'
    });
    // 2.再将含有添加新组件的vue实例化给变量 vm
    var vm=new Vue({
        el:'#one',
        data:{ }
    });
</script>
</body>
</html>

 2. 局部组件创建

 

 代码:

<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title></title>
    <link type="text/css" rel="stylesheet" href=" "/>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- 局部注册组件 -->
<div id="two">
    <button_local></button_local>
</div>
<script type="text/javascript">
    var vm_local = new Vue({
        el:'#two',
        data:{
            info1: 'ha'
        },
        components:{
            button_local:{
                template: '<h1 @click="add">哈哈</h1>',
                methods:{
                    add:function(){
                        alert('你好!')
                    }
                }
            }
        }
    })
</script>
</body>
</html>

 

posted on 2019-09-23 23:09  人生与戏 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyingLiao/p/11575573.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值