JavaScript/vue.js/error:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的7个中间版本)
第59行: 第59行:
<div v-if="BospitalSearchFilters[0]">
<div v-if="BospitalSearchFilters[0]">
             <span class="sub-nav-title" :value="BospitalSearchFilters[0].key">{{BospitalSearchFilters[0].title}}:</span></syntaxhighlight>
             <span class="sub-nav-title" :value="BospitalSearchFilters[0].key">{{BospitalSearchFilters[0].title}}:</span></syntaxhighlight>
* 页面在手机浏览器报错 apiv2.chujingyi.cn/v2/body_parts/all:1 Failed to load resource: net::ERR_NAME_NOT_RESOLVED
* 页面在手机浏览器报错 Failed to load resource: net::ERR_NAME_NOT_RESOLVED
*: axios在部分手机的微信浏览器中无法发起请求
*: 尝试解决给服务器添加目标网站 host,这个问题确切说不是手机浏览器不能发送请求,而是手机本地没有设置代理(电脑开发机通过修改 /etc/hosts 可以发送请求)。
* 手机端浏览器 Uncaught (in promise) Error: Network Error
*: 先确定问题在哪里
*: <syntaxhighlight lang="javascript" highlight='3-5'>axios.get('/disease/footer_recommend').then((req) => {
  _self.Disease_footer_recommend = req.data.data;
}).catch((error)=>{
  console.log(error);
  _self.errorMessage = error;
});</syntaxhighlight>
*: <pre>
Error: Network Error
    at e.exports (spread.js:25)
    at XMLHttpRequest.l.onerror (spread.js:25)</pre>
:* 这个问题源于 我设置的 <code>axios.defaults.baseURL='http://localhost:3000';</code>,网络请求地址指向了开发机自己,当用其他机器测试时请求地址指向测试机地址,所以抛错。
:* 解决办法分三步
:* {{hc|package.json|<syntaxhighlight lang="javascript">
"sshbuild": "export NODE_ENV=production HOST=152.32.132.166 && webpack --mode production --config webpack.config.js",
</syntaxhighlight>}}
:* {{hc|webpack.config.js|<syntaxhighlight lang="javascript">
const AXIOSHOST = process.env.HOST;
/**
* 配置全局常量
*/
new webpack.DefinePlugin({
  'axios_host':JSON.stringify(AXIOSHOST),
}),
</syntaxhighlight>}}
:* {{hc|router/index.js|<syntaxhighlight lang="javascript">
axios.defaults.baseURL='http://'+axios_host+':3000';
</syntaxhighlight>}}