Vue-组件之间的v-ref和parent访问(十一)

1.父组件访问子组件,跟当初dom元素document.querySelector("元素节点名")dom元素获取是同个意思
在这里插入图片描述
父组件
a.ref="名字子组件1"取组件名
b.在时间方法中 this.@refs.组件名 获取相关数据
HelloWorld.vue5

<template>
  <div>
    <h1>{{msg}}</h1>

    <!-- 二个子组件和孙组件 -->
    <hello-world6 ref="名字子组件1"></hello-world6>
    <hello-world6 ref="名字子组件2"></hello-world6>

    <button @click="buttonClick">按钮</button>

  </div>
</template>

<script>
  //父组件中导入子组件
import HelloWorld6 from "./HelloWorld6";

export default {
  name: 'HelloWorld',
  // 父组件中可以注册多个子组件
  components: {HelloWorld6},
  data () {
    return {
      msg: '父组件',
      testmsg: ['bobo','lixiaobo','bocai','给子组件传数组']
    }
  },
  methods: {
    //document.querySelector("元素节点名")。dom元素获取
    //
    buttonClick: function () {
      //开发中不用,很容易出问题,在开发中页面中途要插入多个组件,这个索引就会变化,不是0索引,索引往后面加
      // console.log(this.$children)
      // //父组件访问第一个子组件方法
      // console.log(this.$children[0].msgData())
      // //父组件访问子组件变量
      // console.log(this.$children[0].msg)
      // //父组件访问孙组件变量
      // console.log(this.$children[0].$children[0].msg)
      console.log("---------------------refs------------------")
      console.log(this.$refs)
      console.log(this.$refs.名字子组件1)
      console.log(this.$refs.名字子组件1.msg)
      console.log(this.$refs['名字子组件1'].msg)
    }
  }
}
</script>
<style>
</style>

子组件
HelloWorld6.vue

<template>
  <div class="hello">
    <h1>{{msg}}</h1>
    <hello-world7></hello-world7>
  </div>
</template>

<script>
  import HelloWorld7 from "./HelloWorld7";
  export default {
    name: 'HelloWorld2',
    components: {HelloWorld7},
    data() {
      return {
        msg: '子组件'
      }
    },
    methods: {
      msgData: function () {
        return '子组件方法返回'
        console.log("子组件方法")
      }
    }
  }
</script>
<style>
</style>

孙组件

<template>
  <div class="hello">
    <h1>{{msg}}</h1>
  </div>
</template>

<script>
  export default {
    name: 'HelloWorld7',
    data() {
      return {
        msg: '孙组件'
      }
    },
    methods: {
    }
  }
</script>
<style>
</style>

index.js

import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
// import HelloWorld3 from '@/components/vue1/HelloWorld3'

import HelloWorld5 from "../components/vue1/HelloWorld5";
Vue.use(Router)

export default new Router({
  routes: [
    // {
    //   path: '/',
    //   name: 'HelloWorld',
    //   component: HelloWorld
    // },
    // {
    //   path: '/',
    //   name: 'HelloWorld3',
    //   component: HelloWorld3
    // },
    {
      path: '/',
      name: 'HelloWorld5',
      component: HelloWorld5
    }
  ]
})

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java语录精选

你的鼓励是我坚持下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值