zoukankan      html  css  js  c++  java
  • vue父传子

    父组件传递数据给子组件用props,父组件中使用子组件,子组件用props接收父组件数据。

    Home父组件代码:

    <template>
    <div>
         {{test}}
         <!-- 使用子组件,绑定父组件数据数据 -->
        <Child :test="test"></Child>
    </div>
    </template>
    <script>
    // import子组件
    import Child from './Child.vue'
    export default {
      name: "Home",
      //components引入子组件
      components:{
          Child
      },
      data () {
        return {
            test:123
        };
      }
    }
    </script>
    <style lang="css" scoped>
    </style>

    Child子组件代码:

    <template>
    <div>
        <!-- 使用子组件数据 -->
         {{test}}
    </div>
    </template>
    <script>
    export default {
      name: "Child",
    //   props使用获取父组件数据
      props:["test"],
      data () {
        return {
            
        };
      },
      created(){
        //   使用子组件数据
          console.log(this.test);
          
      }
    }
    </script>
    <style lang="css" scoped>
    </style>
  • 相关阅读:
    多态
    抽象类和接口
    面向对象3
    类的继承 设计模式
    面向对象2
    面向对象
    复习
    对json的简单认识
    关于AJAX
    PHP配置开发环境
  • 原文地址:https://www.cnblogs.com/luguankun/p/10312095.html
Copyright © 2011-2022 走看看