将vue-cli 2.x的项目升级到3.x

尝试将vue-cli 2.x的项目升级到3.x,记录一下升级过程,和遇到的坑vue

1. 直接复制替换src文件夹
2. 安装项目须要的依赖 (能够将原来package.json dependencies下须要的直接复制过来,而后运行npm i)
3. 安装完后运行npm run serve (若是启动服务不习惯npm run serve,能够将package.json的serve改为dev)jquery


项目是启动了,页面确是空白的,出现以下信息
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included buildwebpack

报错缘由:引入vue时,采用里runtime形式的文件

要将这个错误引用纠正成vue/dist.vue.esm.jsweb

查看cli2.X的版本,在webpack.base.conf.js配置文件中已经添加了这个代码,以下图
vue-cli

解决方法:
在项目的根目录下添加配置文件vue.config.js,将上图这段代码复制粘贴到vue.config.js中,而后重启服务
代码以下:npm

const path = require('path')
function resolve (dir) {
    return path.join(__dirname,dir)
}

module.exports = {
    configureWebpack: config => {
        config.resolve = {
           extensions: ['.js', '.vue', '.json'],
            alias: {
              'vue$': 'vue/dist/vue.esm.js',
              '@': resolve('src'),
            }
        }
    },
}

使用jquery报错,原来2.x添加第三方插件的方法不行了json

解决方案
main.js 里原来用import $ from "jquery"引用的改为ui

const $ = require("jquery");
window.$ = $;

缘由请看下图:

基于jquery的插件使用require()插件


远行后,jquery能够用了,但eslint一直报'$' is not defined
解决方法1:在.eslintrc.js文件里env:{}里加jquery: true
eslint

解决方法2: 在使用$的页面加上/* global $ */


更新中...

相关文章
相关标签/搜索