uni-app main.js 注册全局全局属性错误
uni-app main.js 注册全局全局属性错误
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | w11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
PC开发环境操作系统版本号:w11
HBuilderX类型:正式
HBuilderX版本号:4.61
手机系统:Android
手机系统版本号:Android 14
手机厂商:华为
手机机型:11
页面类型:nvue
vue版本:vue2
打包方式:云端
App下载地址或H5网址:1
示例代码:
main.js
import store from ‘./store’ app.config.globalProperties.$adpid = “1111111111” app.config.globalProperties.$store= store
component.nvue console.log(123,123) console.log(123, this.$adpid)
H5 能正常使用 app错报 reportJSException >>>>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught TypeError: (0 , import_vuex.createStore) is not a function at (pages/tabBar/component/component.js.nvue:156:43) at (pages/tabBar/component/component.js.nvue:570:3)
### 操作步骤:
1
预期结果:
1
### 实际结果:
1
bug描述:
main.js
import store from ‘./store’ app.config.globalProperties.$adpid = “1111111111” app.config.globalProperties.$store= store
component.nvue console.log(123,123) console.log(123, this.$adpid)
H5 能正常使用 app错报 reportJSException >>>>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught TypeError: (0 , import_vuex.createStore) is not a function at (pages/tabBar/component/component.js.nvue:156:43) at (pages/tabBar/component/component.js.nvue:570:3)
更多关于uni-app main.js 注册全局全局属性错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
你这个是vue3的写法
看你用的是vue2吧
你要想挂载,Vue.prototype.$adpid = “1111111111”
这样用用呢
更多关于uni-app main.js 注册全局全局属性错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我怎么感觉是vuex 的版本问题,注释下app.config.globalProperties.$store= store 应该不会报错了吧,看下你store怎么写的,估计用的是2.0
10:24:09.429 onLoad at pages/index/index.nvue:137
10:24:09.430 onShow at pages/index/index.nvue:167
10:24:09.675 onReady at pages/index/index.nvue:119
10:24:09.920 onLaunch at App.vue:7
10:24:10.042 onShow at App.vue:101
index.nuve 不可以使用 uni.$store.state
App.vue 不可以使用 uni.$store.state
非主页面 可以使用 uni.$store.state
应该是 主页面启动太快了
vue3,在vue页面能取到globalProperties里面定义的变量,但在nvue页面,却直接显示的是undefined
根据错误信息,问题出在Vuex store的创建方式上。错误提示createStore is not a function
表明在App端无法正确导入Vuex的createStore方法。
在uni-app的Vue2项目中,正确的全局属性注册方式应该是:
- 确保store/index.js中使用Vuex的正确写法:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
// ...配置
})
- main.js中应这样注册:
import Vue from 'vue'
import store from './store'
Vue.prototype.$adpid = "1111111111"
Vue.prototype.$store = store