uni-app 在子组件的renderjs的methods里写函数父组件无法refs调用子组件里的函数

发布于 1周前 作者 ionicwang 来自 Uni-App

uni-app 在子组件的renderjs的methods里写函数父组件无法refs调用子组件里的函数

开发环境 版本号 项目创建方式
Mac 13.5.2 (22G91) HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Mac

HBuilderX类型:正式

HBuilderX版本号:3.97

手机系统:iOS

手机系统版本号:iOS 17

手机厂商:模拟器

手机机型:15

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX

示例代码:

<template>  
    <view>  
    </view>  
</template>  

<script module="test" lang="renderjs">  
    export default {  
        data() {  
            return {  
            };  
        },  
        methods: {  
            test() {  
            }  
        }  
    }  
</script>

//index.vue父组件

<template>  
    <button style="width: 100px; height: 100px;" @click="test">测试</button>  
    <test ref="test" style="width: 100px; height: 100px;">测试</test>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
            };  
        },  
        methods: {  
            test() {  
                this.$refs.test.test()  
            }  
        }  
    }  
</script>

操作步骤: 如上

预期结果: 可以调用子组件renderjs里的函数

实际结果: 无法调用子组件renderjs里的函数

bug描述: 在text.vue子组件中定义

<template>  
    <view>  
    </view>  
</template>  

<script module="test" lang="renderjs">  
    export default {  
        data() {  
            return {  
            };  
        },  
        methods: {  
            test() {  
            }  
        }  
    }  
</script>

//在index.vue父组件中

<template>  
    <button style="width: 100px; height: 100px;" @click="test">测试</button>  
    <test ref="test" style="width: 100px; height: 100px;">测试</test>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
            };  
        },  
        methods: {  
            test() {  
                this.$refs.test.test()  
            }  
        }  
    }  
</script>

1 回复

uni-app 中,renderjs 是一个用于处理复杂逻辑和性能优化的模块,它运行在 WebViewJavaScript 环境中,而不是 Vue 的上下文中。因此,renderjs 中的方法和数据与 Vue 组件中的方法和数据是隔离的,父组件无法直接通过 refs 调用 renderjs 中的方法。

如果你需要在父组件中调用子组件中的方法,可以考虑以下几种解决方案:

1. 使用 Vue 组件中的方法

将需要在父组件中调用的方法定义在子组件的 Vue 上下文中,而不是 renderjs 中。这样,父组件可以通过 refs 直接调用这些方法。

<template>
  <view>
    <child-component ref="child"></child-component>
    <button @click="callChildMethod">调用子组件方法</button>
  </view>
</template>

<script>
export default {
  methods: {
    callChildMethod() {
      this.$refs.child.childMethod();
    }
  }
}
</script>

子组件:

<template>
  <view>
    <!-- 子组件内容 -->
  </view>
</template>

<script>
export default {
  methods: {
    childMethod() {
      console.log('子组件方法被调用');
    }
  }
}
</script>

2. 通过事件通信

如果方法必须在 renderjs 中定义,可以通过事件机制在 Vue 组件和 renderjs 之间进行通信。

子组件:

<template>
  <view>
    <!-- 子组件内容 -->
  </view>
</template>

<script>
export default {
  mounted() {
    this.$renderjs.on('callRenderjsMethod', () => {
      this.renderjsMethod();
    });
  },
  methods: {
    renderjsMethod() {
      console.log('renderjs 方法被调用');
    }
  }
}
</script>

父组件:

<template>
  <view>
    <child-component ref="child"></child-component>
    <button @click="callRenderjsMethod">调用 renderjs 方法</button>
  </view>
</template>

<script>
export default {
  methods: {
    callRenderjsMethod() {
      this.$refs.child.$renderjs.emit('callRenderjsMethod');
    }
  }
}
</script>

3. 使用 globalDataVuex

如果需要在多个组件之间共享状态或方法,可以使用 uni-appglobalDataVuex 来管理。

// main.js
import Vue from 'vue';
import App from './App';

Vue.prototype.$globalData = {
  renderjsMethod: null
};

new Vue({
  render: h => h(App)
}).$mount('#app');

子组件:

<template>
  <view>
    <!-- 子组件内容 -->
  </view>
</template>

<script>
export default {
  mounted() {
    this.$globalData.renderjsMethod = this.renderjsMethod;
  },
  methods: {
    renderjsMethod() {
      console.log('renderjs 方法被调用');
    }
  }
}
</script>

父组件:

<template>
  <view>
    <button @click="callRenderjsMethod">调用 renderjs 方法</button>
  </view>
</template>

<script>
export default {
  methods: {
    callRenderjsMethod() {
      this.$globalData.renderjsMethod();
    }
  }
}
</script>
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!