vue子组件调用父组件父页面的方法「建议收藏」

vue子组件调用父组件父页面的方法「建议收藏」如图:选择城市后,页面重新请求数据!(城市选择是子组件)选择完后不刷新页面,重新调用接口渲染主页面!第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法父组件<template><div><child></child></div></template&gt…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

如图:选择城市后,页面重新请求数据!(城市选择是子组件)选择完后不刷新页面,重新调用接口渲染主页面! 

vue子组件调用父组件父页面的方法「建议收藏」

 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法

父组件

<template>
  <div>
    <child></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$parent.fatherMethod();
      }
    }
  };
</script>

第二种方法是在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了。

父组件

<template>
  <div>
    <child @fatherMethod="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$emit('fatherMethod');
      }
    }
  };
</script>

第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法

父组件

<template>
  <div>
    <child :fatherMethod="fatherMethod"></child>
  </div>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件

<template>
  <div>
    <button @click="childMethod()">点击</button>
  </div>
</template>
<script>
  export default {
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
        if (this.fatherMethod) {
          this.fatherMethod();
        }
      }
    }
  };
</script>

三种都可以实现子组件调用父组件的方法,但是效率有所不同,根据实际需求选择合适的方法

更多技巧请查看vue专栏   https://blog.csdn.net/qq_42221334/column/info/27230/1

vue子组件调用父组件父页面的方法「建议收藏」

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186571.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • blue light filter_soundsnap

    blue light filter_soundsnapglassfish的主要操作文档。http://www.docin.com/p-141009636.html

    2022年8月20日
    9
  • XGBoost:参数解释

    XGBoost:参数解释XGBoost参数在运行XGboost之前,必须设置三种类型成熟:generalparameters,boosterparameters和taskparameters:Generalparameters:参数控制在提升(boosting)过程中使用哪种booster,常用的booster有树模型(tree)和线性模型(linearmodel)。Boosterparameters:这取

    2022年5月27日
    46
  • Vue中 $attrs、$listeners 详解及使用

    Vue中 $attrs、$listeners 详解及使用1.前言多级组件嵌套需要传递数据时,通常使用的方法是通过vuex。如果仅仅是传递数据,而不做中间处理,使用vuex处理,未免有点杀鸡用牛刀。Vue2.4版本提供了另一种方法,使用v-bind=”$attrs”,将父组件中不被认为props特性绑定的属性传入子组件中,通常配合interitAttrs选项一起使用。之所以要提到这两个属性,是因为两者的出现使得组件之间跨组件的通信在不依赖vuex和事件总线的情况下变得简洁,业务清晰。首先分析以下应用场景A组件与B组件

    2022年10月18日
    4
  • selenium无界面操作自动操作浏览器了解一下

    selenium无界面操作自动操作浏览器了解一下

    2021年9月17日
    53
  • 人工智能猴子摘香蕉问题的逻辑表示_猴子拿香蕉实验感悟

    人工智能猴子摘香蕉问题的逻辑表示_猴子拿香蕉实验感悟猴子摘香蕉问题:一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,箱子位置为B,香蕉位置在C),如何行动可摘取到香蕉。代码样例:#includestructState{ intmonkey;//-1:MonkeyatA

    2022年9月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号