【VUE-学习01】父组件 子组件之间传值 非父子之间传值

父组件 子组件之间传值

 

父->子 

  1. 父组件 v-bind绑定属性
  2. 子组件 prop接收属性

子->父

  1. 子组件发送 emit 并传参
  2. 父组件@监听  

ps:

   子组件不能直接修改父组件传来的值  (比如下面的代码里handleItemClick函数里,不能够写this.index = 0;

<!--
 父子组件传值 
 20190819
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div id='app'>
    <input type="text" v-model='todoValue'>
    <button @click='handleButtonClick'>submit</button>
    <ul>
        <todo-item 
        v-bind:content='item' 
        v-bind:index='index'
        v-for="(item, index) in list"
         @delete="handleItemDelete">  <!--父组件监听子组件的delete事件 -->
    </todo-item>
    </ul>
</div>


</body>
<script src="./js/vue.min.js"></script>
<script>
    // Vue.component("TodoItem",{
    //     props:['content'],
    //     template:"<li> {{content}} </li>"
    // });
    var TodoItem ={
        props:['content', 'index'], //子组件接收父组件的属性值,父组件中使用v-bind
        /*
        //props可指定类型 
        props:{
            content:String,
            index:Number
        },
        */
        template:"<li @click='handleItemClick'> {{content}} </li>",
        methods:{
            handleItemClick:function()
            {
                this.$emit('delete', this.index);//子组件向父组件发送delete事件
            }
        }
    };

    var app = new Vue({
        el: '#app',
        data: {
            todoValue: "",
            list: []
        },
        components:{
            TodoItem:TodoItem
        },
        methods:{
            handleButtonClick:function()
            {
                this.list.push(this.todoValue);
                this.todoValue = '';
            },
            
            handleItemDelete:function(index){
                console.log(index);
                this.list.splice(index, 1);
            }
        }
    });
</script>
</html>

非父子之间传值

<!--
 非父子组件传值  使用Vue.prototype 主线传值与监听
 20190819
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<div id='app'>
    <child content='one' ></child>
    <child content='two' ></child>
</div>

</body>
<script src="./js/vue.min.js"></script>
<script>
    //定义Vue的属性 ,所有vue示例均有此属性bus
    Vue.prototype.bus =  new Vue();
    Vue.component('child',{
        template:"<div @click='handlerClick'>{{selfcontent}}</div>",
        props:{
            content:{
                type:String,
                value:'default'
            }
        },
        data: function(){
                return {
                    selfcontent : this.content
                }
        },
        methods:{
            handlerClick: function(){
                this.bus.$emit('change', this.selfcontent)
            }
        },
        mounted: function(){
            
            var this_ = this;
            this_.bus.$on('change', function(msg){
                console.log('change: '+msg)
                this_.selfcontent = msg
            })
        }
        
        
    });
    var vm = new Vue({
        el:'#app'
    })
</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值