孤竹
孤竹
  • 发布:2024-05-13 15:59
  • 更新:2024-05-13 16:38
  • 阅读:536

【报Bug】hbuilder 4.15 APP 使用 pinia 报错 vue.hasInjectionContext is not a function

分类:HBuilderX

产品分类: HbuilderX

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 10

HBuilderX类型: 正式

HBuilderX版本号: 4.15

手机系统: Android

手机系统版本号: Android 13

手机厂商: vivo

手机机型: iqoo11s

页面类型: vue

vue版本: vue3

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

main.js 导入pinia

import App from './App'  
import { createSSRApp } from 'vue'  
import * as Pinia from 'pinia';  
const pinia = Pinia.createPinia()  
export function createApp() {  
  const app = createSSRApp(App)  
    app.use(pinia)  
  return {  
    app,  
        Pinia  
  }  
}

store/index.js

import {  
    defineStore  
} from "pinia"  

export const useCounterStore = defineStore('counter', {  
    state: () => ({  
        count: 10,  
    }),  
    actions: {  
        increment() {  
            this.count++  
        },  
    },  
})

App.vue

<script>  
    import { mapState } from 'pinia'  
    import { useCounterStore } from './store'  
    export default {  
        onShow: function() {  
            console.log(this.count)  
        },  
        computed: {  
            ...mapState(useCounterStore, ['count'])  
        }  
    }  
</script>  

操作步骤:

在App中 使用 vue3 使用pinia 基于 hx 4.08版本或者之前 打包的自定义基座
然后hx 更新到最新版本(4.15)
main.js 正常导入 pinia
app.vue 使用 mapState

预期结果:

使用 pinia 后 正常运行

实际结果:

报错 vue.hasInjectionContext is not a function

bug描述:

使用 vue3 使用pinia 基于 hx 4.08版本或者之前 打包的自定义基座, 基于此基座,切换到最新版后 生成 wgt热更新 也会产生 下述 bug
更新/切换到最新版本 4.15

在APP运行 提示 vue.hasInjectionContext is not a function

2024-05-13 15:59 负责人:无 分享
已邀请:
DCloud_UNI_LXH

DCloud_UNI_LXH

4.14 调整了 vue 版本,请使用 hx 新版重新打包自定义基座

  • 孤竹 (作者)

    APP线上的版本也必须 整包更新 吗

    2024-05-13 17:16

  • DCloud_UNI_LXH

    回复 孤竹: 是的

    2024-05-14 14:40

  • DCloud_UNI_LXH

    回复 孤竹: 尝试一个新的方案:


    main.js 中写入以下代码:


    // #ifdef APP  
    import Vue from 'vue'
    if (!Vue.hasInjectionContext) {
    Vue.hasInjectionContext = () => {
    return Vue.getCurrentInstance()
    }
    }
    // #endif

    一个可能的完整代码,比如:


    import { createSSRApp } from 'vue'  
    import * as Pinia from 'pinia'

    // #ifdef APP
    import Vue from 'vue'
    if (!Vue.hasInjectionContext) {
    Vue.hasInjectionContext = () => {
    return Vue.getCurrentInstance()
    }
    }
    // #endif

    export function createApp() {
    const app = createSSRApp(App)
    app.use(Pinia.createPinia())
    return {
    app,
    Pinia
    }
    }

    注意: 详细测试一下 Pinia 的功能是否正常

    2024-05-16 17:09

  • 孤竹 (作者)

    回复 DCloud_UNI_LXH: 没发现问题,可以使用

    2024-05-21 14:58

要回复问题请先登录注册