uni-app nvue 下 plus.globalEvent.addEventListener('plusMessage') 事件不触发

uni-app nvue 下 plus.globalEvent.addEventListener(‘plusMessage’) 事件不触发

开发环境 版本号 项目创建方式
Windows
macOS 11.2.3
HBuilderX 3.2.11 HBuilderX
Android

产品分类:uniapp/App

PC开发环境操作系统:Windows

手机系统:Android

手机系统版本号:Android 12

手机厂商:华为

手机机型:MI 8

页面类型:vue

打包方式:云端


示例代码:

<hbb-webview src="https://baidu.com" />
<template>
  <view v-if="loading" class="spinner">
    <text class="spinner-txt">加载中</text>
  </view>
</template>

<script>
export default {
  name: 'HbbWebview',
  props: {
    src: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  created() {
    const web = plus.webview.create("", `custom-webview_${Date.now()}`, {
      "uni-app": "none",
      top: uni.getSystemInfoSync().statusBarHeight + 44,
      bottom: 0,
      opacity: 0,
      bounce: "vertical",
      userSelect: false,
      errorPage: "none",
    });
    this._web = web;
    web.addEventListener("loaded", () => {
      if (this._loaded) return;
      this._loaded = true;
      this.onLoading(false);
    });
    plus.globalEvent.addEventListener("plusMessage", this.onPlusMessage);
    plus.webview.currentWebview().append(web);
    web.loadURL(this.src);
    this._timer1 = setTimeout(() => this.onLoading(true), 500);
    this._timer2 = setTimeout(() => this.onLoading(false), 2000);
  },
  beforeDestroy() {
    plus.globalEvent.removeEventListener(this.onPlusMessage);
    clearTimeout(this._timer1);
    clearTimeout(this._timer2);
  },
  methods: {
    onLoading(show = true) {
      this.loading = show;
      this._web.setStyle({ opacity: show ? 0 : 1 });
      if (show) return;
      clearTimeout(this._timer1);
      clearTimeout(this._timer2);
    },
    onPlusMessage(event) {
      console.log("onPlusMessage", event);
    },
  },
};
</script>

<style lang="scss" scoped>
.spinner {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;

  &-img {
    width: 100rpx;
    height: 100rpx;
  }

  &-txt {
    font-size: 24rpx;
    color: #999;
  }
}
</style>

更多关于uni-app nvue 下 plus.globalEvent.addEventListener('plusMessage') 事件不触发的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

没太弄清楚 你写个demo吧提供一下我们看下

更多关于uni-app nvue 下 plus.globalEvent.addEventListener('plusMessage') 事件不触发的实战教程也可以访问 https://www.itying.com/category-93-b0.html


明天给你,今晚有其他事。页面事 demo.nvue, 组件事 hbb-webview.vue 这种情况下,webview 内注册plusMessage捕获不到消息,但是如果把页面改成demo.vue 就可以了。我安卓和苹果模拟器测试都是一样的。demo明天给你

主要是nvue项目中打算使用自定义navigation-bar,然后和web-view组合。webview需要一些hack逻辑(evalJS的注入,titleupdate的捕获等),所以需要自己给予 plus.webview.create 封装一个webview组件出来

回复 青阳_1900: 有解决了吗

回复 1***@sina.com: 由于没提供demo,官方还没处理,问题是有的

回复 青阳_1900: 还没解决?

回复 [已删除]: 不是没解决,是没处理

请问有解决方案了不?我也遇到了nvue 下 plus.globalEvent.addEventListener不触发的问题

我将plus.globalEvent.addEventListener封装成demo()方法写在APP.vue中,在nuve页面通过getApp().demo()调用可行

回到顶部