vue中一个组件调用另一个组件的方法(函数)

vue中一个组件调用另一个组件的方法(函数)

    在工作中遇到一个问题,页面整体框架是一个侧边菜单栏,点击选项后会生成一个类似于浏览器的标签页,但是有一些路由需要在页面上点击,也要生成一个标签页,这就需要views的组件来调用侧边栏组件的添加标签的方法,同时还需要将路由为这个方法传过去。

    一开始我尝试将侧边菜单栏组件引入页面组件,再通过"引入组件名.metheds.引入组件中的方法"的方式来调用方法并传值,但是经调试后发现值根本没有传过去,在大佬的指导下我学会了一种别的方式来调用并成功实现

这是侧边菜单栏添加标签的方法,需要被外部组件调用:

<script>
    export default {
        provide() {
            return {
                moduleclicked: this.moduleclicked,
            };
        },
        methods: {
            moduleclicked(routePath) {
              let routeDef = this.$router.match(routePath);
              let componentInfo =
                routeDef.matched[routeDef.matched.length - 1].components.default;
              let componentProps = routeDef.matched[routeDef.matched.length - 1].props.default;
              componentProps =
                componentProps === true
                  ? componentProps
                  : typeof componentProps == "object"
                  ? componentProps
                  : typeof componentProps == "function"
                  ? componentProps(this.$route)
                  : "";

              let module = {};
              for (let resItem of this.auth.instance.getPermission().resList.values()) {
                if (resItem.resPvalue == routePath) {
                  module = resItem;
                  break;
                }
              }
              this.addTab(routeDef.meta.title, routePath, componentInfo, componentProps);
            },
        },
    };
</script>

这是页面的组件的调用:

<script>
    export default {
        inject:['moduleclicked'],
        methods: {
            xndgxTz(){
                this.moduleclicked('/project/virtualstart');
            },
            gscplxTz(){
                this.moduleclicked('/project/productregistration');
            },
            gsnblxTz(){
                this.moduleclicked('/project/nbxmdjd');
            }
        },
    };
</script> 

通过在被调用组件中添加provide(),在里面返回需要传出去的方法名称

provide() { //注意,provide()与method、data、created这些同级
    return {
      moduleclicked: this.moduleclicked,  //前面的是要传出去的方法名,后面是本地的方法
    };
  },

然后在页面组件中使用inject[‘传入方法名’]

inject:['moduleclicked'],//注意,inject与method、data、created这些同级

在调用时只需要

this.moudleclicked("/project/index"); //通过this.传入方法名(‘需要传递的参数’)即可传参调用
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 一个组件可以调用一个组件函数,可以通过以下几种方式实现: 1. 使用 `$refs`:在父组件组件添加一个 `ref`,然后通过 `$refs` 来访问组件方法。例如: ```vue <template> <div> <child-component ref="childRef"></child-component> <button @click="callChildFunction">调用组件函数</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { callChildFunction() { this.$refs.childRef.childFunction(); } } } </script> ``` 2. 使用事件:在组件定义一个触发事件,并在父组件监听该事件来调用组件函数。例如: ```vue <!-- ChildComponent.vue --> <template> <div> <button @click="triggerEvent">触发事件</button> </div> </template> <script> export default { methods: { triggerEvent() { this.$emit('child-event'); } } } </script> <!-- ParentComponent.vue --> <template> <div> <child-component @child-event="callChildFunction"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { callChildFunction() { // 调用组件函数 } } } </script> ``` 3. 使用 Vuex:如果你使用了 Vuex 来进行状态管理,可以在组件触发一个 action 来调用其他组件函数。具体的代码逻辑会根据你的项目结构和需求而有所不同,这里无法提供具体示例。 希望这些方法能够帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值