vue component 组件的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>

</head>
<body>
<div id="app">
    <button-counter>

    </button-counter>
</div>
<script>
    Vue.component('button-counter', {
        data: function () {
            return {
                count: 0
            }
        },
        template: '<button v-on:click="count++">You click me {{count}} times</button>'
    });
    var vm = new Vue({
        el: "#app",
    });
</script>
<style>

</style>
</body>
</html>

 点击按钮会增加次数

多个组件互相不会影响

 如何传递属性进来?

<div id="app">
    <button-counter title="我是传递过来的参数"></button-counter>
    <button-counter></button-counter>
</div>
<script>
    Vue.component('button-counter', {
        props: ['title'],
        data: function () {
            return {
                count: 0
            }
        },
        template: '<button v-on:click="count++">title {{title}}You click me {{count}} times</button>'
    });
    var vm = new Vue({
        el: "#app",
    });
</script>

 some times 我们需要多个标签

        template: '<div> <h1>请注意。只能有一个标签哦</h1><button v-on:click="clickfun">title {{title}}You click me {{count++}} times</button></div>',

这个里面只能有一个大标签。多个标签也只有外面的生效 所以要用div包裹

我们要监听里面的数据变化如何操作?

 

 总代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>

</head>
<body>
<div id="app">
    <button-counter title="我是传递过来的参数" @clicknow="clicknow"></button-counter>
    <button-counter></button-counter>
</div>
<script>
    Vue.component('button-counter', {
        props: ['title'],
        data: function () {
            return {
                count: 0
            }
        },
        template: '<div> <h1>请注意。只能有一个标签哦</h1><button v-on:click="clickfun">title {{title}}You click me {{count}} times</button></div>',
        methods: {
            clickfun: function () {
                this.count++;
                this.$emit('clicknow', this.count)
            }
        }
    });
    var vm = new Vue({
        el: "#app",
        methods: {
            clicknow: function (e) {
                console.log(e);
            }
        }
    });
</script>
<style>

</style>
</body>
</html>

传入内容

你看这里是不是中间差点什么东西

有没有方法把空间传递到这里面去

   <button-counter title="我是传递过来的参数" @clicknow="clicknow"></button-counter>

像这样

    <button-counter title="我是传递过来的参数" @clicknow="clicknow"><h3>我是传递过来的插件</h3></button-counter>

针对模板 里面要新增一个solt插件

        template: '<div> <h1>请注意。只能有一个标签哦</h1><button v-on:click="clickfun">title {{title}}You click me {{count}} times</button> <br> <slot></slot></div>',

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值