Vue(二)

时间:2021-02-10 12:57:39   收藏:0   阅读:0

Vue(二)

Axios异步通信

<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>

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

<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>
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!