服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - Vue项目开发常见问题和解决方案总结

Vue项目开发常见问题和解决方案总结

2021-09-24 16:06大明的IT笔记 JavaScript

这篇文章主要介绍了Vue项目开发常见问题和解决方案总结,帮助大家更好的利用vue开发,感兴趣的朋友可以了解下

Vue Cli 打包之后静态资源路径不对的解决方法

cli2版本:

将 config/index.js 里的 assetsPublicPath 的值改为 './' 。

?
1
2
3
4
5
build: {
 ...
 assetsPublicPath: './',
 ...
}

cli3版本:

在根目录下新建 vue.config.js 文件,然后加上以下内容:(如果已经有此文件就直接修改)

?
1
2
3
module.exports = {
 publicPath: '', // 相对于 HTML 页面(目录相同)
}

Vue cli 3 报错 error: Unexpected console statement (no-console) 解决办法

在项目的根目录下的 package.json 文件中的 eslintConfig:{} 中的 "rules": { 加入"no-console":"off" }, 其它类似报错也是如此

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 示例:
"eslintConfig": {
  "root": true,
  "env": {
   "node": true
  },
  "extends": [
   "plugin:vue/essential",
   "eslint:recommended"
  ],
  "rules": {
   "no-console":"off"
  },
}

axios 取消请求 (如:用户登录失效,阻止其他请求)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
 
axios.interceptors.request.use(
  config => {
    config.cancelToken = source.token; // 全局添加cancelToken
      return config;
    },
    err => {
      return Promise.reject(err);
    }
  );
/** 设置响应拦截 **/
axios.interceptors.response.use(
  response => {
    // 登录失效101
    if ( response.data.code===101) {
      source.cancel(); // 取消其他正在进行的请求
      // some coding
    }
    return response;
  },
  error => {
    if (axios.isCancel(error)) { // 取消请求的情况下,终端Promise调用链
      return new Promise(() => {});
    } else {
      return Promise.reject(error);
    }
  }
);

vue-photo-preview图片预览失效问题记录

?
1
2
3
4
5
6
7
8
9
<img
  class="pic"
  v-for="(item,index) of imgList"
  :key="index"
  :src="item.smallFileName"
  :large="item.fileName"
  preview="0"
  preview-text="症状图片"
>

imgList是异步获取数据的时候在获取数据后需要调用this.$previewRefresh();刷新重置一下,否则~~不生效

以上就是Vue项目开发常见问题和解决方案总结的详细内容,更多关于vue 常见问题和解决方案的资料请关注服务器之家其它相关文章!

原文链接:https://www.lmkjs.com/article/detail?id=58

延伸 · 阅读

精彩推荐