uni.createLivePusherContext中第二个参数是this,在setup里面不存在this,我参试传递getCurrentInstance()但是会报TypeError: Cannot read property 'meta' of undefined!那么请教一下如何在setup中正确使用uni.createLivePusherContext Api?

- 发布:2022-09-22 23:09
- 更新:2025-04-25 16:06
- 阅读:2203

同样的问题 null is not an object (evaluating 'vue.getCurrentInstance().proxy') __ERROR
<template>
<view class="container">
<button @click="qrBtn">QR扫描</button>
<view >
<live-pusher
class="livePusher"
id='livePusher'
ref="livePusher"
:url=url
mode="FHD"
muted="true"
min-bitrate="6000"
device-position="front"
@statechange="statechangeBtn"
></live-pusher>
</view>
</view>
</template>
<script setup>
import {reactive, ref,onMounted} from 'vue'
import {onReady} from '@dcloudio/uni-app'
const url = ref(null)
const statechangeBtn = (code,message) => {
console.log(code,'statechangeBtn')
console.log(message,'statechangeBtn')
}
onMounted(() => {
console.log('111')
this.context = uni.createLivePusherContext("livePusher", getCurrentInstance().proxy);
})
const qrBtn = () => {
uni.scanCode({
onlyFromCamera: true,
success: function (res) {
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
}
});
}
const barcode = ref(null);

推荐你去看这个代码:https://ext.dcloud.net.cn/plugin?id=12947,他用的就是setup,不股票这个live-pusher只有再nvue下能正常抓拍,vue不行。
我摘抄一部分他的源码吧。
<script setup lang="ts">
import { reactive, ref, onMounted, nextTick, getCurrentInstance } from 'vue';
const data = reactive({
_this: null,
snapshotsrc: null, //快照
})
onReady(() => {
data._this = getCurrentInstance().proxy;
data.livePusher = uni.createLivePusherContext('livePusher', data._this);
})
//抓拍
const snapshot = () => {
data.livePusher.snapshot({
success: e => {
console.log("拍照", e);
data.snapshotsrc = e.message.tempImagePath;
}
});
}
</script>

我用vue3写后报错[JS Framework] Failed to invoke the event handler of "click" on view (10):
TypeError: context.start is not a function
13:08:39.771 reportJSException >>>> exception function:WEEX_CALL_JAVASCRIPT, exception:JavaScript execute error!Uncaught TypeError: context.start is not a function
at start (/pages/called/called.nvue:96:17)

主页面(路由页)的子组件 在 setup 只能获取到 当前组件的实例,而当前子组件的实例中不包含$page对象存在,$page 对象只能在主页面(路由页)能获取到。
参考官方源码:
const createLivePusherContext = defineSyncApi(API_CREATE_LIVE_PUSHER_CONTEXT, (id, vm) => {
if (vm.$page.meta.isNVue) {
if (!vm) {
return console.warn('uni.createLivePusherContext: 2 arguments required, but only 1 present');
}
const elm = findElmById(id, vm);
if (!elm) {
return console.warn('Can not find ' + id + '
');
}
return new LivePusherContext(id, elm);
}
return new LivePusherContextVue(id, vm.$page.id);
}, CreateLivePusherContextProtocol);
在调用该方法时,会去访问 $page 对象,而在页面子组件中的 getCurrentInstance().proxy 中的$page是undefined的,故会报TypeError: Cannot read property 'meta' of undefined!
此时:可以通过 手动构建 $page 对象,来绕过该问题。
同时希望官方可以修复这个错误。同时这段代码存在逻辑错误,望修正。
应该先判断 vm 是否存在,再输出警告
8***@qq.com
你好,请问创建地图组件实例的时候 uni.createMapContext(id,this) 中的this 再setup中 如何传呢? 我试了上面的getCurrentInstance().proxy 无效。
2023-03-27 17:21
DCloud_UNI_WZF
回复 8***@qq.com: 无效具体指什么,详细描述你的问题,如有必要,提供下测试工程
2023-03-28 10:58
d***@ohimy.com
我遇到了同样的问题,null is not an object (evaluating 'vue.getCurrentInstance().proxy') __ERROR,请问import {
getCurrentInstance
} from 'vue' 是这样用的吗?
2023-04-05 03:27
DCloud_UNI_WZF
回复 d***@ohimy.com: 附件提供下最简测试工程
2023-04-05 17:49