Vue(二)

Vue(二)

Axios异步通信

  • Axios是一个开源的可以用在浏览器端和Node.js的异步通信框架,它的主要作用就是实现Ajax异步通信
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--解决闪烁问题-->
    <style>
        [v-clock] {
            display: none;
        }
    </style>

</head>
<body>

<div id="vue" v-clock>
    <div>{{info.name}}</div>
    <div>{{info.address.street}}</div>
    <a v-bind:href="info.url">点我</a>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    let vm = new Vue({
        el: "#vue",
        data: {
            info: {
                name: null,
                address: {
                    country: null,
                    city: null,
                    street: null
                },
                url: null
            }
        },
        mounted() { //钩子函数
            axios.get('../data.json')
                .then(response => (this.info = response.data));
        }
    });
</script>

</body>

计算属性

  • 能够将计算结果缓存起来的属性,可以将不经常变化的计算结果进行缓存,节约系统开销
<div id="app">
    <p>currentTime1:{{currentTime1()}}</p>
    <p>currentTime2:{{currentTime2}}</p>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            message: "hello"
        },
        methods: {
            currentTime1: () => {
                return Date.now();
            }
        },
        computed: {
            currentTime2: () => {
                return Date.now();
            }
        }
    });
</script>

内容分发与自定义事件分发

  • 使用<slot>元素作为承载分发内容的出口,可以应用在组合组件的场景中

  • @绑定事件,this.$emit调用外部事件

<div id="app">
    <todo>
        <todo-title slot="todo-title" :title="title"></todo-title>
        <!--v-bind:缩写为:绑定数据,v-on缩写为@绑定事件-->
        <todo-items slot="todo-items" v-for="(item, index) in todoItems"
                    :item="item" :index="index" @remove="removeItem(index)" :key="index"></todo-items>
    </todo>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>
    Vue.component("todo", {
        template:
            '<div>\
                <slot name="todo-title"></slot>\
                <ul>\
                    <slot name="todo-items"></slot>\
                </ul>\
            </div>'
    });

    Vue.component("todo-title", {
        props: ['title'],
        template:
            '<div>{{title}}</div>'
    });

    Vue.component("todo-items", {
        props: ['item', 'index'],
        template:
            '<li>{{index}}-{{item}}<button @click="remove(index)">删除</button></li>',
        methods: {
            remove: function (index) {
                /*click后,进入此函数,通过this.$emit,将参数传递给外部的remove事件*/
                this.$emit('remove', index);
            }
        }
    });

    let vm = new Vue({
        el: "#app",
        data: {
            title: '书籍列表',
            todoItems: ['Java', 'Linux', 'Python']
        },
        methods: {
            removeItem: function (index) {
                console.log("删除了" + vm.todoItems[index]);
                this.todoItems.splice(index, 1);
            }
        }
    });
</script>
posted @ 2021-02-09 14:53  一天到晚睡觉的鱼  阅读(63)  评论(0)    收藏  举报