uni app 问题记录
<p><strong>一、当我在调用store时报错: Cannot read property 'state' of undefined</strong></p><p><strong><strong>具体代码如下图</strong></strong></p>
<p><strong><img src="https://img2023.cnblogs.com/blog/23836/202302/23836-20230226104912813-915811207.png" alt="" loading="lazy"></strong></p>
<p> </p>
<p><strong>解决过程:</strong></p>
<p><span style="color: rgba(255, 0, 0, 1)">各种尝试有点无计可施,感谢网友哪种分享,给了我解决的思路,所以我把我的解决过程及最终方案也分享给大家,希望能帮忙到别人。</span></p>
<p><strong>1、新建简单test页。先test页直接引用store.js</strong></p>
<p> 先是报错,发现原来 vue3 不可以直接以 import Vue from 'vue' 引用。</p>
<p>在store.js中去掉 import Vue from 'vue' 、Vue.use(Vuex) ,修改如下,成功调用。</p>
<div class="cnblogs_code">
<pre>import store <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@/store/store.js</span><span style="color: rgba(128, 0, 0, 1)">'</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">methods: {
increment () {
store.commit(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">increment</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
console.log(store.state.count)
}
}</span></pre>
</div>
<p><strong>2、在mian.js 挂载,通过this.$store.state.count 调用。</strong></p>
<p> 一直会报 Cannot read property 'state' of undefined</p>
<p>先是在mian.js中增加打印:console.log('main.js来了。不是vue3环境 - 执行代码块') 、console.log('是vue3环境 - 执行代码块')</p>
<p>打印结果说明我是vue3版本。之前我一直在// #ifndef VUE3 块中尝试说明地方就搞错了。</p>
<p>一直报错,说明就是挂载不成功。最后了如下修改解决。</p>
<pre><span>app.use(store) 挂载。要注意挂载的地方。如果有帮助到你,多多支持,能帮助到更多的人。</span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef VUE3</span>
console.log(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">是vue3环境 - 执行代码块</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
import {createSSRApp} </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">vue</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
import App </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./App.vue</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
import store </span><span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">@/store/store.js</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">mian中:</span><span style="color: rgba(128, 0, 0, 1)">'</span>+<span style="color: rgba(0, 0, 0, 1)">store.state.count)</span>
<span style="color: rgba(0, 0, 0, 1)">export function createApp() {
</span><span style="color: rgba(0, 0, 255, 1)">const</span> app =<span style="color: rgba(0, 0, 0, 1)"> createSSRApp(App)
app.use(store)
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
app,
store
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span></pre>
</div>
<p><strong> 二、module namespace not found in mapState()</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从 vuex 中按需导出 mapState 辅助方法</span>
import {mapState} <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">vuex</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
computed:{
...mapState(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">m_cart</span><span style="color: rgba(128, 0, 0, 1)">'</span>,[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">cart</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">])
},</span></pre>
</div>
<p>如上图代码报错,原因:找到 cart 对应的js文件,在当中开启命令空间</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">为当前模块开启命令空间</span>
namespaced:<span style="color: rgba(0, 0, 255, 1)">true</span>,</pre>
</div>
<p> 三、发行到微信小程序,发现有些模块不显示</p>
<p> 余是在不显示的页面中的methods 方法里开始注释排除查找原因。要注释methods是因为看到网上有人是computed中写法有问题,也遇到这情况,因为我这个页面没有computed,所以就注释了methods方法。</p>
<p><span style="color: rgba(255, 0, 0, 1)">奇怪的是,原因在微信开发者工具里页面也不显示的。我把加载数据的那个方法注释了,就开始有显示,然后再去掉注释就显示了。</span></p>
<p><strong> 四、报错 chooseAddress:fail the api need to be declared in\</strong></p>
<ul>
<ul>
<li>解决方案<br>一、uni-app项目<br>在项目根目录中找到 manifest.json 文件,在左侧导航栏选择源码视图,找到mp-weixin 节点,在节点后面加上:可能要重新运行。</li>
</ul>
</ul>
<p><strong> </strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">requiredPrivateInfos</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">getLocation</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">onLocationChange</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">startLocationUpdateBackground</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">chooseAddress</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
]</span></pre>
</div>
<p><strong> 五、request-miniprogram 配置网络请求 拦截器</strong></p>
<p>有时无效,看看有没有安装好。看看你项目中有没有他</p>
<p><strong><img src="https://img2023.cnblogs.com/blog/23836/202304/23836-20230410204921978-153456537.png" alt="" loading="lazy"></strong></p>
<p> </p>
<p> </p>
<p> </p>
<div class="cnblogs_code">
<pre>npm init -<span style="color: rgba(0, 0, 0, 1)">y 在项目根目录初始化package.json文件
npm i @escook</span>/request-miniprogram -S 下载网络请求包</pre>
</div>
<p> <strong>https://blog.csdn.net/weixin_66556453/article/details/125435281</strong></p>
<p> </p>
<p> </p>
<p> </p>
<h1 id="articleContentId" class="title-article"> </h1><br><br>
来源:https://www.cnblogs.com/xbding/p/17156328.html
頁:
[1]