vue-cli 中文官网

正文(3.x)

1. 初次创建项目,控制台报错(一直循环,真的恶心)

解决之后不知为何,错误无法再次还原了

  1. 报错文字 ``` Invalid Host/Origin header [WDS] Disconnected!
  1. 2. 解决参考地址
  2. > [Vue thinks local IP address is global, causing several issues · Issue #1616 · vuejs/vue-cli](https://github.com/vuejs/vue-cli/issues/1616#issuecomment-402744915)
  3. 3. 具体解决方法
  4. - `vue.config.js` 配置文件中,添加 `devServer.disableHostCheck` 配置即可

module.exports = { /**

  1. ### 2. 根据官网配置的 `devServer.proxy` 访问失败,提示 `404`
  2. > [官网地址](https://cli.vuejs.org/zh/config/#devserver-proxy)
  3. 1. 依照官网的设置出现问题

devServer: { disableHostCheck: true, proxy: { ‘/dictpc’: { target: ‘http://183.129.5.16:8080/dictpc‘, ws: true, // proxy websockets changeOrigin: true // needed for virtual hosted sites } } },

  1. > 依照如上设置,启动项目,直接 `404`
  2. 2. 解决问题
  3. - `proxy` 增加 `pathRewrite` 设置即可,之后重启项目(注意每次修改配置后必须得重新启动项目)

devServer: { disableHostCheck: true, proxy: { ‘/dictpc’: { target: ‘http://183.129.5.16:8080/dictpc‘, ws: true, // proxy websockets changeOrigin: true, // needed for virtual hosted sites pathRewrite: { ‘^/dictpc’: ‘’ } } } },

  1. ### 3. `sourcemap` 默认是开启的,导致打包出来的 `dist -> js` 文件夹过大
  2. 1. 发现问题是在看今天(2019.1.15)新出的一款匿名社交产品 [马桶MT](https://www.mtoilet.com/) 官网的源码时,下面是查看的截图
  3. > ![马桶源码查看截图](https://upload-images.jianshu.io/upload_images/9064013-d3d5d17199ea70a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  4. - 后来发现我之前用的时候也没注意过这个问题耶,/尴尬
  5. 2. 解决问题
  6. - 官网提供了专门的配置解决这个问题
  7. > [配置参考 | Vue CLI 3](https://cli.vuejs.org/zh/config/#productionsourcemap)
  8. - 具体实施:在 `vue.config.js` `module.exports` 中加入下面的配置

productionSourceMap: false

  1. ### 4. `.vue` 文件中使用了没写内容的 `script` 和 `style` 空标签导致的 `bug`
  2. 1. `script` 没写内容
  3. - 源码示例
  1. - `bug`文字描述

Cannot set property ‘render’ of undefined

  1. - 解决方法
  2. - 在里面写上内容 `export default {}`
  3. - 删掉 `script` 标签
  4. 2. `script` 没写内容
  5. - 源码示例
  1. - 这样导致的问题
  2. > ![打包出现空的 `css` 文件](https://upload-images.jianshu.io/upload_images/9064013-d06c232c9430593f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)<br />
  3. 后端 `java` 一起上传至服务器时,空文件并没有被上传上去,页面访问时该 `css` 资源 `404`,且使用该资源的路由对应的页面脚本会因为文件找不到而报错,该路由页面会直接打不开
  4. - 解决方法
  5. - 直接删掉 `style` 标签即可
  6. ### 5. 依照官网结构写的 `store` 目录结构,导致页面出错
  7. > [官网链接的购物车示例代码](%5Bhttps://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/store/index.js%5D(https://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/store/index.js))
  8. - 错误文本

[vuex] unknown mutation type: updateVipCard

  1. - 错误原因
  2. > [官方文档 - 命名空间](%5Bhttps://vuex.vuejs.org/zh/guide/modules.html#%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%5D(https://vuex.vuejs.org/zh/guide/modules.html#%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4))<br />
  3. 购物车示例中,由于需要 "具有更高的封装度和复用性,你可以通过添加 `namespaced: true` 的方式使其成为带命名空间的模块。",但是我的业务暂时没有这个需求也用这个了,造成了报错

export default { state, getters, actions, mutations }

  1. - 解决方法
  2. > 删掉代码中的 `namespaced: true`
  3. ### 6. 每次 `yarn serve` 运行项目时,控制台会出现一些警告
  4. ##### 1. 问题一
  5. -
  6. 警告内容

No parser and no filepath given, using ‘babylon’ the parser now but this will throw an error in the future. Please specify a parser or a filepath so one can be inferred.

  1. -
  2. 问题解析
  3. > [参考文章](https://blog.csdn.net/shan1991fei/article/details/81838115)<br />
  4. )<br />
  5. 参考如上文章的内容,结论是 `vue-loader` 的版本问题,我看了下项目内部 `package.json` 使用的版本是 `14.2.2`
  6. -
  7. 问题解决
  8. > 先去 [github - vue-loader ](https://github.com/vuejs/vue-loader/blob/master/package.json)上看了下最新的版本号,在命令行中升级 `vue-loader` 的版本

yarn upgrade vue-loader@15.7.1

  1. -
  2. 重新启动项目问题已解决了
  3. ##### 2. 问题二
  4. -
  5. 警告内容

caniuse-lit e is outdated. Please run next command yarn upgrade caniuse-lite browserslist

  1. -
  2. 问题解析
  3. > [参考文章](08d9ad35747e4da0c394668bcd8b96a2)<br />
  4. 参考如上文章的内容,结论是 删除 `yarn.lock` `yarn install`
  5. -
  6. 问题解决
  7. > 删除 `yarn.lock` 文件和 `node_modules` 目录,再执行 `yarn` 重新安装项目中的依赖(需要注意的是这里默认安装的是所有依赖的最新版本)

yarn

```

  • 重新启动项目问题已解决了

正文(4.x)旧项目升级问题记录

1. 文件命名造成的引用失败的问题

image.png

  • 如上所示的报错,具体代码以及目录结构如下

image.png
image.png

  • 之前旧项目是没有这样的错误的,所以我猜测是命名的问题导致的。
  • 首先我改成如下这样时就是好用的

image.png

  • 接着我尝试修改组件目录中文件的名称 Index => index 就是把首字母的大写改成小写,然后再重启代码,bingo!!!问题果然没有了

    2. Critical dependency: the request of a dependency is an expression

参考文章:如何解决 Critical dependency: the request of a dependency is an expression ?
依照上述文章内容,这个错误源自 webpack 的版本问题,高版本的不可以在 import 中使用变量

  • 下图中是修复前和修复后的代码对比图

image.png

image.png