Vue组件------父子间通信

子组件传递数据 用户已经登录

父组件接收数据 并显示列表,未登录不显示列表

/*
有两个组件,分别是main-component,header-component.
main-component是由header-component和一个列表(有5条数据 [100,200,300,400,500]),
header-component是由一个h1的标签:‘这是页头‘,有一个数据isUserLogin:true

在渲染main-component时候,读取header-component在挂载完毕之后通过事件传递来的数据(isUserLogin),根据该数据的真假来决定列表是否显示.

<child-component @myEvent="recvMsg">
</child-component>

this.$emit(‘myEvent‘,myPhone)
*/

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>子与父通信</title>
    <script src="js/vue.js"></script>
 </head>
 <body>
  <div id="container">
        <p>{{msg}}</p>
        <main-component></main-component>
    </div>
    <script>

//创建父组件
        Vue.component("main-component",{
            data:function(){
                return{
                    myList:[1,2,3,4,5],
                    isUserLogin:true
                }
            },
            methods:{
                recvMsg:function(msg){
                    this.isUserLogin = msg;
                }
            },
            template:`
            <div>
                <header-component @checkUserLogin="recvMsg"></header-component>
                <ul v-if="isUserLogin">
                    <li v-for="tmp in myList">{{tmp}}</li>
                </ul>
            </div>`
        })
        //创建子组件
        Vue.component("header-component",{
        //data属性中保存用户已经登录
            data:function(){
                return{
                    isLogin:false
                }
            },
            //通过$emit在挂载完成后把用户已经登录的值发传递给父组件
            mounted:function(){
                    this.$emit("checkUserLogin",this.isLogin);
            },
            template:`
                <div>
                    <h1>这是页头</h1>
                </div>
            `
        })
//    new一个vue实例
        new Vue({
            el:"#container",
            data:{
                msg:"Hello VueJs"
            }
        })
    </script>
 </body>
</html>
时间: 2024-08-30 06:24:07

Vue组件------父子间通信的相关文章

vue组件父子间通信之综合练习--假的聊天室

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>组件父子间通信之综合练习</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>

vue组件父子间通信02

三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"></child-component> ②根据指定的引用的名字 找到子组件的实例对象this.$refs.mySon 子组件要想获取父组件的数据:①直接读取this.$parent :::通过this.$refs拿到子组件的数据 <!doctype html> <html>

vue组件之间的通信

vue组件间的通信有父--->子.子--->父.非父子之间的通信 虽然我们稍微复杂的项目都用vuex来管理了,但是还是想写一篇关于有父--->子.子--->父.非父子 之间通信的文章.下面通过代码来讲述 父--->子组件间的通信 父级页面: <template> <div id="app"> <Header :parentMsg='parentMsg' > </Header> </div> <

vue组件中的通信

一.组件间的关系 1.父子关系 2.兄弟关系 3.隔代关系 二.组件间的通信方式 1.props 2.$emit/$on 3.VUEX 4.$parent/$children 5.$attrs/$listeners 6.provide/inject 三.通信方式举例 新建了一个过程,采用webpack来管理项目.  方法一:props / $emit 1.props---父组件向子组件传值 子组件:sub1.vue 父组件:app.vue 父组件通过props向下传递给子组件.注:组件中的数据共

父子间通信四 ($dispatch 和 $broadcast用法)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js测试父子之间通信</title> <script type="text/javascript" src="lib/boot.js"></script> <style> .box{

Vue组件~父子组件之间的通信(传值)

1.父组件向子组件传参 父组件中通过v-bind对要传参数的绑定:(例如) :dataSourceName:是在子组件中要用到的值 "ctpSaveEchartSetting.dataSourceId":是在父组件中要传的值 子组件中的接收通过props(数据值单项绑定的)进行接收:(例如) 注:在props中有的变量就不能再data中再定义,或通过监测.计算属性直接去改变,因为js中的对象和数组是引用对象,指向同一个内存空间,如果在子组件中改变props中的值会影响到父组件中的值的状

vue组件父子之间相互通信案例

vue组件父子组件之间传递数据

举个栗子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../Vue.js"></script> <template id="tpl1"> <h3>我是父组件

vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> <child v-bind:my-message="parentMsg"></child>//注意传递参数时要用-代替驼峰命名,HTML不区分大小写 </div> 子组件: Vue.component('child', { // camelCase in Ja