• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
渴爱网のWebMaster 's Blog【Welcome to my website : KIIK.TOP 】
Go ahead man ! I just wanna achieve what i wanted man.
Tips:1.Do a excellent programmer who can hacking and build awesome program
     2.Became a designer who can make everything which nearby me looks like so charming.
博客园    首页    新随笔    联系   管理    订阅  订阅

vuejs中组件之间的数据传递

 

1.将全局数据传递至子组件中(先将数据从实例对象的全局传递至父组件,再将父组件数据传递到子组件)

//.html

<father :fr="fr"【第一步:首先将实例的全局变量传递到父组件的属性中】></father>

//js-father控件

Vue.component('father',{

  props:["fr"],【第二部:将该父控件的属性值写入props数组中表明要在子控件中使用】

  template: "<ul><li v-for='fr_item in fr'>{{fr_item}}</li></ul>"

});

//js-实例化vue对象

new Vue({

  el: "#app",

  data: {

    fr:[

      {name:"常见"},

      {name:"张嘴"}

    ]

  }

});

 

2.将子组件中的数据传递至父组件中

<father :fr="fr" @change-num="handle($event)" :num="num"></father>【第二步,因为触发子控件事件导致父控件自定义事件触发,触发事件函数,将子控件传输的数据传输给该函数,注意:固定要给其参数为$event】

//js-father控件

Vue.component('father',{

  props:["fr","num"],

  template: "<ul><li v-for='(fr_item,index) in fr' @click='$emit(\'change-num\',index)'【第一步,首先触发事件,用$emit(自定义事件,需要传递的值)传递】>{{fr_item}}</li></ul>{{num}}"

});

//js-实例化vue对象

new Vue({

  el: "#app",

  data: {

    num:0,

    fr:[

      {name:"常见"},

      {name:"张嘴"}

    ]

  },

  methods: {

    handle: function(val){

      this.num=val;

    }

  }

});

3.兄弟组件之间的数据传输

 

posted @ 2020-09-23 00:14  kiik  阅读(483)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3