vue学习1之组件传递

子传父;

 
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="counter-event-example">
    <div>
        sync修饰符的使用
    </div>
    <!--
    :foo.sync="bar"扩展为如下:
    <comp :foo="bar" @update:foo="val => bar = val"></comp>

    <!--&ndash;&gt; -->

    <comp :foo.sync='bar'></comp>
    <span>绑定bar值为--{{bar}}</span>
    <br><br><br>
    <div>组件子向父组件传参---在表单输入点击sendd</div>
    <span>{{total}}</span>
    <button-counter @child-info="get"></button-counter>
    <br><br><br>
    <div>组件子向父组件传参---native原生操作</div>
    <span>{{total}}</span>
    <button-counter @clild-info="get" v-on:click.native='doTheThing'></button-counter>
    <!--<span>{{num}}</span>-->
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.rawgit.com/chrisvfritz/5f0a639590d6e648933416f90ba7ae4e/raw/974aa47f8f9c5361c5233bd56be37db8ed765a09/currency-validator.js"></script>

<script>
    Vue.component('button-counter',{
        template:"<div><input type='text' v-model='ceshi'><button @click='send'>send1</button></div>",
        data:function () {
            return{
                ceshi:''
            }
        },
        methods:{
            send:function () {
                this.$emit('child-info',this.ceshi)
            }
        }
    })
    Vue.component('comp',{
        props:['foo'],
        template:"<div>{{foo}}<button @click='send'>点击foo</button></div>",
        data:function () {
            return {
                fa:''
            }
        },
        methods:{
            send:function(){
                this.$emit('update:foo','ee')
            //    this.$emit('child-info',this.ceshi)
            }
        }
    })
    var aa = new Vue({
        el:'#counter-event-example',
        data:{
            total:'',
            bar:'aa'
        },
        methods:{
            get:function (msg) {
                this.total = msg;
                alert(this.total)
            },
            doTheThing:function () {
                alert("这是原生的事件啊 啊")
            }
        }
    })
</script>
</body>
</html>

父传子:


  1. ps:App.vue 父组件  
  2.   Hello.vue 子组件  
  3.    
  4.   
  5. App.vue  :  
  6.   
  7. <template>  
  8. <div id="app">  
  9. <input type="button" name="" id="" @click="parentCall" value="父调子" />  
  10.   
  11. <hello ref="chil" />  
  12.   
  13. </div>  
  14. </template>  
  15.   
  16. <script>  
  17. import hello from './components/Hello'  
  18. export default {  
  19. name: 'app',  
  20. 'components': {  
  21. hello  
  22. },  
  23. methods: {  
  24.   parentCall () {  
  25.     this.$refs.chil.chilFn('我是父元素传过来的')  
  26.   }  
  27. }  
  28. }  
  29. </script>  
  30.   
  31. Hello.vue  :  
  32.   
  33. <template>  
  34. <div class="hello">  
  35.     
  36. </div>  
  37. </div>  
  38. </template>  
  39.   
  40. <script>  
  41. export default {  
  42. name: 'hello',  
  43. 'methods': {  
  44.   
  45.   chilFn (msg) {  
  46.     alert(msg)  
  47.   }  
  48.   
  49.   
  50. }  
  51. }  
  52. </script>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值