Vue2.5开发去哪儿网App 第二章笔记

2019-02-17 01:50:29来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Vue完成  TodoList

1.默认方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TodoList</title>
    <script src="../../vue.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button v-on:click="addItem">添加</button>
        <ul>
            <li v-for="item in list">{{ item }}</li>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el:'#app',
            data:{
                list:[],
                inputValue:''
            },
            methods:{
                addItem:function () {
                    this.list.push(this.inputValue)
                    this.inputValue = ''
                }
            }
        })
        console.log(app.$data)
    </script>
</body>
</html>
View Code

2.以全局组件的方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="inputValue">
        <button v-on:click="addItem">添加</button>
    <tode-item v-bind:content="item" v-for="item in list"></tode-item>
</div>
<script>
    var Myconponent = Vue.extend({
        props:['content'],
        template:"<li>{{content}}</li>"
    })

    Vue.component('tode-item',Myconponent)

    var app = new Vue({
        el:'#app',
        data:{
            list:[],
            inputValue:''
        },
        methods:{
            addItem:function () {
                this.list.push(this.inputValue)
                this.inputValue = ''
            }
        }
    })
</script>
</body>
</html>
View Code

3.以局部组件的方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>局部组件 TodoList</title>
    <script src="../../vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="inputValue">
    <button v-on:click="addItem">添加</button>
    <todo-item v-bind:content="item" v-for="item in list"></todo-item>
</div>
<script>
    var Myconponent = {
        props:['content'],
        template:"<li>{{content}}</li>"
    }
    var app = new Vue({
        el:'#app',
        components:{
            'todo-item':Myconponent
        },
        data:{
            list:[],
            inputValue:''
        },
        methods:{
            addItem:function () {
                this.list.push(this.inputValue)
                this.inputValue = ''
            }
        }
    })
</script>
</body>
</html>
View Code

Vue 组件间传值

子组件向父组件传值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>局部组件 TodoList</title>
    <script src="../../vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="inputValue">
    <button v-on:click="addItem">添加</button>
    <!--v-bind简写::  v-on:  @-->
    <todo-item :content="item" :index="index" @delete="HandleItemDelete" v-for="(item,index) in list"></todo-item>
</div>
<script>
    var Myconponent = {
        props:['content','index'],
        template:"<li @click='HandleItemClick'>{{content}}</li>",
        methods:{
            HandleItemClick:function () {
                this.$emit('delete',this.index)
            }
        }
    }
    var app = new Vue({
        el:'#app',
        components:{
            'todo-item':Myconponent
        },
        data:{
            list:[],
            inputValue:''
        },
        methods:{
            addItem:function () {
                this.list.push(this.inputValue)
                this.inputValue = ''
            },
            HandleItemDelete:function (index) {
                console.log(index)
                this.list.splice(index,1)
            }
        }
    })
</script>
</body>
</html>
View Code

 


原文链接:https://www.cnblogs.com/donghaoblogs/p/10389611.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:微信小程序获取复选框全选,反选选中的值

下一篇:Table 组件构建过程中遇到的问题与解决思路