【前端】vue学习3:计算属性、内容分发、自定义事件

什么是计算属性?(VUE相比Angular和React的特性)

它就是一个能够将计算结果缓存起来的属性(将行为转化成了静态的属性);可以想象为缓存!


关键字:computed

<!DOCTYPE html>
<!--<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">-->
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>
<body>
<div id="app">
    currentTime1=  {{currentTime1()}}<br/>
    currentTime2=  {{currentTime2}}<br/>
    reserveString= {{reserveString}}<br/>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data:{

        },
        methods:{
            currentTime1:function () {
                return Date.now();
            }
        },
        computed:{
            currentTime2:function () {
                return Date.now();
            }
        },
		    reserveString: function () {
		           return this.msg.split('').reverse().join('');
	        }
   });
</script>
</body>
</html>

结论:
 方法同名,会以methods里的为准。所以不要写同名函数。调用方法时,每次都需要进行计算,既然有计算过程则必定产生系统开销,那如果这个结果是不经常变化的呢?此时就可以考虑将这个结果缓存起来,采用计算属性可以很方便的做到这一点,计算属性的主要特性就是为了将不经常变化的计算结果进行缓存,以节约我们的系统开销;


插槽

在Vue.js中我们使用 元素作为承载分发内容的出口,作者称其为插槽,可以应用在组合组件的场景中;
这里穿插以下vue的语法缩写
v:bind: 可以缩写为一个:
v-on: 可以缩写为一个@


li 遍历实现

<!DOCTYPE html>
<html lang="en" xmlns:v-for="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>
<body>
<div id="app">
    <p>课程列表</p>
    <ul >
        <li v-for="item in items" >{{item}}</li>
    </ul>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            msg: "hello,computed!",
            items:['java','python','c++']
        }
    });
</script>
</body>
</html>

插槽实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
    <!--
    v-bind:xxx="yyy" 缩写为 :xxx="yyy"
    -->
    <todo>
        <todo-h3 slot="slot-h3" :receive_title="title"></todo-h3>
        <todo-li slot="slot-li" v-for="item in books" :receive_item="item"></todo-li>
    </todo>
</div>
<script>

    Vue.component("todo",{
        template:'<div>\
                       <slot name="slot-h3"></slot>\
                       <ul>\
                            <slot name="slot-li"></slot>\
                       </ul>\
                  </div>'
    });
    Vue.component("todo-h3",{
        props:['receive_title'],
        template:'<h3>{{receive_title}}</h3>'
    });
    Vue.component("todo-li",{
        props:['receive_item'],
        template:'<li>{{receive_item}}</li>'
    });

    var vm = new Vue({
        el:"#app",
        data:{
            title:"四大名著",
            books:["红楼梦","三国演义","水浒传","西游记"]
        }
    });
</script>
</body>
</html>

自定义事件

通过以上代码不难发现,数据项在Vue的实例中,但删除操作要在组件中完成,那么组件如何才能删除Vue实例中的数据呢?此时就涉及到参数传递与事件分发了,Vue为我们提供了自定义事件的功能很好的帮助我们解决了这个问题;使用this.$emit(‘自定义事件名’,参数)

在这里插入图片描述


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
    <!--
    remove绑定removeBook
    -->
    <todo>
        <todo-h3 slot="slot-h3" :receive_title="title"></todo-h3>
        <todo-li slot="slot-li"
                 v-for="(item,index) in books"
                 :receive_item="item"
                 :index="index"
                 v-on:remove="removeBook(index)"></todo-li>
    </todo>
</div>
<script>

    Vue.component("todo",{
        template:'<div>\
                       <slot name="slot-h3"></slot>\
                       <ul>\
                            <slot name="slot-li"></slot>\
                       </ul>\
                  </div>'
    });
    Vue.component("todo-h3",{
        props:['receive_title'],
        template:'<h3>{{receive_title}}</h3>'
    });
    Vue.component("todo-li",{
        props:['receive_item','index'],
        template:'<li>{{index}}--{{receive_item}}<button style="margin: 5px" @click="remove()">删除</button></li>',
        methods:{
            remove:function (index) {
                //触发当前实例上的事件。即触发了与remove绑定的removeBook
                //v-on:remove="removeBook(index)
                this.$emit('remove',index);
            }
        }
    });

    var vm = new Vue({
        el:"#app",
        data:{
            title:"四大名著",
            books:["红楼梦","三国演义","水浒传","西游记"]
        },
        methods:{
            removeBook:function (index) {
                console.log(this.books[index]);

                //删除索引index的元素,删除1个。
                this.books.splice(index,1);
            }
        }

    });
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

nickkkkkkkkk

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

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

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

打赏作者

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

抵扣说明:

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

余额充值