Vue_(组件通讯)简单使用父子组件

  Vue组件  传送门

在Vue的组件内也可以定义组件,这种关系成为父子组件的关系

   如果在一个Vue实例中定义了component-a,然后在component-a中定义了component-b,那他们的关系就是

    Vue实例 -- 根组件 root

      component-a – 相对于root 这是子组件,相对于component-b这是 父组件

         component-b -- 子组件

  目录结构

  

  简单的通过在父组件调用子组件,但父组件的值是无法直接传递给子组件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <father-component></father-component>

        </div>
    </body>

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}}</span>

            <hr />

            <child-component></child-component>
        </div>
    </template>

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}}</span>
        </div>
    </template>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

        new Vue({
            data : {

            },
            components : {
                "father-component" : {
                    data(){
                        return {
                            name : ‘Gary‘
                        }
                    },
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template"
                        }
                    }
                }
            }
        }).$mount("#GaryId");

    </script>
</html>

Gary_fatherAndChild.html

实现过程

  在<body>中通过<div>调用父组件<father-component>去调用子组件<child-component>

    <body>
        <div id="GaryId">
            <father-component></father-component>
        </div>
    </body>

  父子组建,在父组件与子组件中分别调用父组件data()域中的值

<template id="father-template">
        <div>
            <h1>father component</h1>
            myData : <span>{{name}}</span>

            <hr />

            <child-component></child-component>
        </div>
    </template>

    <template id="child-template">
        <div>
            <h2>child component</h2>
            fatherData : <span>{{name}}</span>
        </div>
    </template>

  在Vue中子组件嵌套在父组件当中,其中给父组件name属性赋值"Gary"

            components : {
                "father-component" : {
                    data(){
                        return {
                            name : ‘Gary‘
                        }
                    },
                    template :  "#father-template",
                    components : {
                        "child-component" : {
                            template : "#child-template"
                        }
                    }
                }
            }

原文地址:https://www.cnblogs.com/1138720556Gary/p/10480853.html

时间: 2024-08-29 14:13:23

Vue_(组件通讯)简单使用父子组件的相关文章

Vue最常用的组件通讯有三种:父-&gt;子组件通讯、子-&gt;父组件通讯,兄弟组件通讯.(template用的pug模板语法)

Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通讯,是通过props进行数据传递,并且具有这几个特性,单向传递,子组件接收的数据不可以更改,如果更改,会发出警告,每次父组件更新时,子组件的所有 prop 都会更新为最新值. 1 父组件 2 <template lang="pug"> 3 .father 4 Children(:name='msg

小程序开发 组件定义(封装)、组件调用、父子组件方法调用、父子组件传值通讯

在小程序开发中,为了提高代码效率,和代码使用率,我们也用到了组件封装, 今天我介绍下如何在小程序中封装一个头部公用组件 首先,所有父组件(调用页面)的json文件都要引用子组件:index.json { "usingComponents": { "header": "../../component/header/header", } } 一,组件定义(封装) 子组件:header.wxml <view name='header' class

React 组件基本使用(3) ---父子组件之间的通信

当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据. 很简单的一个例子,我们在输入框中输入温度,输入框下面显示冷和热.这里就有两个组件,输入框和它下面的显示内容,且它们共享一个状态,就是温度.所以我们要把温度这个状态放到这两个组件的父组件中.这里就有三个组件,一个输入框TemperatureInput,  一个是显示内容

vue知识总结第一篇vue组件的定义以及父子组件的传值。

vue中是一个.vue结尾的vue文件其中包括<template>标签里边写html,而react是在render函数中..script用来写js,style中用来写css.那么我们来看看vue是怎么写的吧 子组件中 1,用export default 导出子组件,其中标红的是用来父子组件传值的this.$emit('reChild')指发送一个自定事件到父组件中,父组件中只要在引入这个子组件标签的时候v-on:reChild="rece"其中rece为父组件中method

vue教程3-05 vue组件数据传递、父子组件数据获取

vue教程3-05 vue组件数据传递 一.vue默认情况下,子组件也没法访问父组件数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="bower_components/vue/dist/vue.js"><

Vue组件通信之非父子组件传值

前言: 如果想要了解非父子关系的组件传值,最好是在了解父传子和子传父的基础上在来了解非父子传值可能会有更透彻的思路. 因为非父子传值是通过定义事件总线来代理实现父传子+子传父从而实现的传值方式. 这是我总结的父子传值相关的知识,可供参考: https://www.cnblogs.com/ViavaCos/p/11712131.html 然后大概回顾一下父子传值的过程: 根据上述信息可知,如果两个组件需要传递值那么需要这两个组件之间是父子关系才能传递数据. 那么如果有这样一个组件,既可以帮你传递数

Vue组件通讯

Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通讯,是通过props进行数据传递,并且具有这几个特性,单向传递,子组件接收的数据不可以更改,如果更改,会发出警告,每次父组件更新时,子组件的所有 prop 都会更新为最新值. 1 父组件 2 <template lang="pug"> 3 .father 4 Children(:name='msg

react父子组件传值

react均是以组件构成,那父子组件传值就很重要了 父组件传值给子组件,不仅可以传值,传事件,还可以传实例 1.父组件传值给子组件 传值 父组件: import React from 'react'; import Children from './Children'; class Father extends React.Component { constructor(props) { super(props); this.state = { msg:'父组件的msg' }; } render

Vue入门七、父子组件间通讯

一.父子组件通讯 父传子:1.父用子的时候通过属性传递2.子要声明props:['属性名']接收3.子组件template中直接用 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script type="