温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue组件化的实例分析

发布时间:2021-10-19 14:52:09 来源:亿速云 阅读:113 作者:小新 栏目:web开发

这篇文章将为大家详细讲解有关vue组件化的实例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

全局组件

<!DOCTYPE html>
<html>
<head>
    <title>vue</title>
    <!-- <script src="./vue.js"></script> -->
    <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>
</head>
<body>
    <div id="root">
        <input v-model="inputValue"/>
        <button @click="addTextFunc">提交</button>
        <ul>
            <todo-item v-bind:content="item" v-for="item in list"></todo-item>
        </ul>
    </div>
    <script>
        //全局组件
        Vue.component("TodoItem", {
            props: ["content"],
            template: "<li>{{content}}</li>"
        })
        var app = new Vue({
            el: "#root",
            data: {
                list: []
            },
            methods: {
                addTextFunc: function() {
                    this.list.push(this.inputValue)
                    this.inputValue = ""
                }
            }
        });
    </script>
</body>
</html>

局部组件

<!DOCTYPE html>
<html>
<head>
    <title>vue</title>
    <!-- <script src="./vue.js"></script> -->
    <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script>
</head>
<body>
    <div id="root">
        <input v-model="inputValue"/>
        <button @click="addTextFunc">提交</button>
        <ul>
            <todo-item v-bind:content="item" v-for="item in list"></todo-item>
        </ul>
    </div>
    <script>
        //局部组件
        var TodoItem = {
            props: ['content'],
            template: "<li>{{content}}</li>"
        }
        var app = new Vue({
            el: "#root",
            //注册TodoItem组件
            components: {
                TodoItem: TodoItem //命名叫TodoItem,在实例中也叫TodoItem
            },
            data: {
                list: []
            },
            methods: {
                addTextFunc: function() {
                    this.list.push(this.inputValue)
                    this.inputValue = ""
                }
            }
        });
    </script>
</body>
</html>

子组件、父组件双向传递数据

<!DOCTYPE html>
<html>
<head>
    <title>vue</title>
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
    <div id="root">
        <input v-model="inputValue"/>
        <button @click="addTextFunc">提交</button>
        <ul>
            "v-bind:"可以简写为":"
            <todo-item v-bind:content="item" v-bind:index="index" v-for="(item, index) in list" @delete="handleItemDelete"></todo-item>
        </ul>
    </div>
    <script>
        //局部组件
        var TodoItem = {
            props: ['content', 'index'], //要使用就要声明(父组件给子组件传值,子组件要接收!)
            //v-on:click的简写:@click
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods: {
                handleItemClick() {
                    //子组件向父组件传值(触发事件,父组件的@delete="handleItemDelete"在监听)
                    this.$emit("delete", this.index); //不但触发delete时间,同时还把this.index作为参数给父组件
                }
            }
        }
        var app = new Vue({
            el: "#root",
            //注册TodoItem组件
            components: {
                TodoItem: TodoItem //命名叫TodoItem,在实例中也叫TodoItem
            },
            data: {
                list: []
            },
            methods: {
                addTextFunc: function() {
                    this.list.push(this.inputValue)
                    this.inputValue = ""
                },
                handleItemDelete: function(index) { //此处接收传过来的this.index
                    //全部删除
                    //this.list = []
                    //删除点击的数据(标识为当前数据的index)
                    this.list.splice(index, 1)
                }
            }
        });
    </script>
</body>
</html>

关于“vue组件化的实例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI