uni-app UTS插件编写的页面组件里面的method,如何才能给uview文件调用?
uni-app UTS插件编写的页面组件里面的method,如何才能给uview文件调用?
UTS插件编写与调用问题
问题描述
UTS插件编写的页面组件里面的method,如何才能给uview文件调用?
组件代码
<template>
<view>
</view>
</template>
<script lang="uts">
import FrameLayout from 'android.widget.FrameLayout'
import LayoutParams from 'android.widget.FrameLayout.LayoutParams'
import Camera2Helper from './Camera2Helper.uts'
import TextureView from "android.view.TextureView";
export default {
name: "hx-baidu-face",
props: {},
watch: {},
expose: ['startCapture'],
method: {
startCapture() {
Camera2Helper.getInstance().capture()
}
},
NVLoad() : FrameLayout {
let contentLayout = new FrameLayout(this.$androidContext!);
let textureView = new TextureView(this.$androidContext!);
textureView.setTag("cameraView");
contentLayout.addView(textureView, new LayoutParams(400, 400));
Camera2Helper.getInstance().setTextureView(textureView);
return contentLayout;
},
NVLayouted() {
Camera2Helper.getInstance().init()
},
NVBeforeUnload() {
Camera2Helper.getInstance().closeAll()
},
NVUnloaded() {
},
unmounted() {
}
}
</script>
uview页面调用代码
<template>
<view class="container">
<image class="bg" src="/static/bg/bg_splash.png" mode="aspectFill"></image>
<view class="content">
<image src="/static/logo.png" mode="aspectFill" class="logo"></image>
<hx-baidu-face ref="hxBaiduFace1" style="width: 400px;height: 400px;background-color: aqua;"></hx-baidu-face>
</view>
</view>
</template>
<script>
export default {
data() {
return {}
},
onLoad() {
setTimeout(() => {
console.log('开始拍照')
this.$refs['hxBaiduFace1'].startCapture()
}, 3000)
},
methods: {}
}
</script>
<style>
.container {
width: 1920px;
height: 1080px;
position: relative;
}
.bg {
width: 1920px;
height: 1080px;
}
.content {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
width: 100px;
height: 100px;
margin-bottom: 20rpx;
border-radius: 50px;
}
.title {
font-size: 50px;
}
.start-text {
margin-top: 100px;
font-size: 32px;
}
</style>
错误信息
19:10:49.587 [plugin:uni:app-uts] 编译失败
19:10:49.592 error: Unresolved reference: startCapture
解决方案
请检查 hx-baidu-face
组件是否正确引入并注册。确保 ref
引用正确,并且 startCapture
方法在组件中正确暴露。
目前在uvue下 需要调用组件内的方法 与 nvue 有一些差异。
下面我这边测试可以。 稍后也同步到文档
<template>
<uts-hello-view ref="helloView" buttonText="点击按钮内容" style="width:375px;height: 375px;background-color: aqua;"></uts-hello-view>
<button @tap=“callComponentMethod”>调用组件的方法</button>
onLoad里拿不到ref,onReady才能保证组件可用。
调用示例@DCloud_Android_DQQ 补充文档。
下面给你回复了一张图片,依旧是报错
@DCloud_heavensoft ,在onReady也还是报错 这个是调用页面:
这个是插件里面暴露的函数:
https://uniapp.dcloud.net.cn/plugin/uts-component.html#运行和测试
文档更新了,补充了 调用组件内置方法的示例
你再看准一点我上面的截图,我的是连编译都没法通过,我的插件已经暴露了一个startCapture的方法
回复 3***@qq.com: uvue 调用组件方法有些特殊,我回复在下面了。
回复 DCloud_Android_DQQ: 按你完善的文档那里的写法,我这里编译器直接报[plugin:alias] Cannot find module ‘F:/xxxx…到项目根目录/uni_modules/插件名称’ from ‘F:/xxxx…项目根目录/…具体的页面.uvue’
回复 DCloud_Android_DQQ: 是不是还缺少什么配置要写的到package.json里面的
回复 3***@qq.com: 你直接复制 简单的demo 能运行吗 就我上面贴出来的代码
回复 DCloud_Android_DQQ: 按照你的demo也没法运行,但是我大胆地试了一下,你的uvue 页面需要使用下面的引入代码,nvue 不需要这句话应该写反了,我的文件是uview,如果不注释import { UtsHelloViewElement } from '@/uni_modules/uts-hello-component’这段代码,就一直报上面这个错; 但是把import { UtsHelloViewElement } from '@/uni_modules/uts-hello-component’注释掉之后,保留开启(this.$refs[“helloView”] as UtsHelloViewElement).doSth(‘param doSth’);这段代码,就能触发插件里面的函数了
如下图所示就是正常的了,虽然有波浪线挺不好看的,但是能正常执行了
回复 3***@qq.com: 好的。文档同步处理了
@DCloud_Android_DQQ 如这图所属,一切正常了
在 uni-app 中使用 UTS 插件编写的页面组件时,如果你希望将组件中的方法暴露给 uview
或其他外部文件调用,可以通过以下几种方式实现:
1. 使用 ref
引用组件
你可以在父组件中使用 ref
引用子组件,然后通过 ref
调用子组件的方法。
子组件 (UTS 插件编写的组件)
<template>
<view>
<!-- 组件内容 -->
</view>
</template>
<script>
export default {
methods: {
myMethod() {
console.log('Method called from uview');
}
}
}
</script>
父组件 (使用 uview)
<template>
<view>
<my-component ref="myComponentRef"></my-component>
<u-button @click="callComponentMethod">调用组件方法</u-button>
</view>
</template>
<script>
import MyComponent from '@/components/MyComponent.vue';
export default {
components: {
MyComponent
},
methods: {
callComponentMethod() {
this.$refs.myComponentRef.myMethod();
}
}
}
</script>
2. 使用 $emit
事件通信
你可以在子组件中通过 $emit
触发事件,然后在父组件中监听该事件并调用相应的方法。
子组件 (UTS 插件编写的组件)
<template>
<view>
<u-button @click="triggerMethod">触发方法</u-button>
</view>
</template>
<script>
export default {
methods: {
triggerMethod() {
this.$emit('my-event');
}
}
}
</script>
父组件 (使用 uview)
<template>
<view>
<my-component @my-event="handleEvent"></my-component>
</view>
</template>
<script>
import MyComponent from '@/components/MyComponent.vue';
export default {
components: {
MyComponent
},
methods: {
handleEvent() {
console.log('Event received from component');
}
}
}
</script>
3. 使用 provide/inject
跨层级通信
如果你需要在多个层级之间传递方法,可以使用 provide
和 inject
。
父组件 (使用 uview)
<template>
<view>
<my-component></my-component>
</view>
</template>
<script>
import MyComponent from '@/components/MyComponent.vue';
export default {
components: {
MyComponent
},
provide() {
return {
callComponentMethod: this.myMethod
};
},
methods: {
myMethod() {
console.log('Method called from uview');
}
}
}
</script>
子组件 (UTS 插件编写的组件)
<template>
<view>
<u-button @click="callMethod">调用方法</u-button>
</view>
</template>
<script>
export default {
inject: ['callComponentMethod'],
methods: {
callMethod() {
this.callComponentMethod();
}
}
}
</script>
4. 使用全局事件总线
你还可以使用全局事件总线来实现跨组件通信。
创建事件总线
// eventBus.js
import Vue from 'vue';
export const EventBus = new Vue();
子组件 (UTS 插件编写的组件)
<template>
<view>
<u-button @click="triggerMethod">触发方法</u-button>
</view>
</template>
<script>
import { EventBus } from '@/eventBus.js';
export default {
methods: {
triggerMethod() {
EventBus.$emit('my-event');
}
}
}
</script>
父组件 (使用 uview)
<template>
<view>
<my-component></my-component>
</view>
</template>
<script>
import MyComponent from '@/components/MyComponent.vue';
import { EventBus } from '@/eventBus.js';
export default {
components: {
MyComponent
},
created() {
EventBus.$on('my-event', this.handleEvent);
},
methods: {
handleEvent() {
console.log('Event received from component');
}
},
beforeDestroy() {
EventBus.$off('my-event', this.handleEvent);
}
}
</script>