zoukankan      html  css  js  c++  java
  • vue父子组件间传值

    父组件给子组件传值

    父组件:

    <template>
        <div id="container">
            <Child :msg="data"></Child>
        </div>
    </template>
    <script>
    import Child from "@/components/Child";
    export default {
      data() {
        return {
          data: "父组件的值"
        };
      },
      methods: {
      },
      components: {
        Child
      }
    };
    </script>

    子组件:

    <template>
        <div id="container">
            <h1>{{msg}}</h1>
        </div>
    </template>
    <script>
    export default {
      data() {
        return {};
      },
      props:["msg"]
    };
    </script>

    方法二:可以通过$refs来触发,同时传参

    <template>
        <div id="container">
        <h1 @click="getData">点击将触发子组件方法,并把参数传给子组件</h1>
          <child ref="mychild"></child>
        </div>
    </template>
    <script>
    import Child from "@/components/Child";
    export default {
      data() {
        return {
            name: '',
            age: ''
        };
      },
      
      components: {
        Child
      }
      
      methods: {
      getData(){
        //触发子组件方法,并传参
            this.$refs.mychild.init("chrchr","120");
      } 
      
      },
      
    };
    </script>

    子组件:

    <template>
        <div id="container">
            <h1>{{name}}</h1>
            <h1>{{age}}</h1>
        </div>
    </template>
    <script>
    export default {
    
      props:["msg"],
      
      data() {
        return {
            name: '',
            age: ''
        };
      },
      
      mounted:{
        
      },  
      method:{
        //父组件触发子组件的init方法
        init(name,age){
            this.name = name;
            this.age = age;
        }
      }
    };
    </script>

    子组件给父组件传值:通过  this.$emit 来传值

    <template>
        <div class="app">
           <input @click="sendMsg" type="button" value="给父组件传递值">
        </div>
    </template>
    <script>
    export default {
     
        data () {
            return {
                //将msg传递给父组件
                msg: "我是子组件的msg",
            }
        },
         methods:{
             sendMsg(){
                 //func: 是父组件指定的传数据绑定的函数,this.msg:子组件给父组件传递的数据
                 this.$emit('func',this.msg)
             }
         }
    }
    </script>

    父组件接收:

    <template>
        <div class="app">
            <child @func="getMsgFormSon"></child>
        </div>
    </template>
    <script>
    import child from './child.vue'
    export default {
        data () {
            return {
                msgFormSon: "this is msg"
            }
        },
        components:{
            child,
        },
        methods:{
                getMsgFormSon(data){
                    this.msgFormSon = data
                    console.log(this.msgFormSon)
                }
        }
    }
    </script>

    这两种传值也可以通过 vuex 来实现

  • 相关阅读:
    Git使用
    A star算法
    禅语人生
    android中GridView
    关于Android资源学习
    买了胡百敬老师的<SQL SERVER 2008 管理实战>
    人生七苦
    SQL Server 2008实现"编辑所有行"和"返回所有行"的方法
    陈慧娴《永远是你的朋友》专辑歌词
    SQL Server 2008 Service Pack 1 简体中文补丁包下载
  • 原文地址:https://www.cnblogs.com/weiweiyeyu/p/12843467.html
Copyright © 2011-2022 走看看