parcel import Vue失败

293 阅读1分钟

复现

安装: yarn add vue
重启: parcel src/index.html
导入:import Vue from 'vue' 
//import Vue from '../node_modules/vue/dist/vue.global.js //开发版本`

console.log(Vue) //undefined

测试import Vue是否成功时,发现打印的是undefined,说明导入失败。尝试把路径换成了绝对路径但由于是开发版本还是不符合需求。

image.png


最终猜测可能是版本后缀名的问题。于是指定安装版本成功解决。

安装:yarn add vue@2.6.10 //指定版本

image.png

parcel快速搭建Vue项目

1.导入Vue

先安装: yarn add vue@2.6.10 //指定版本
重启: parcel src/index.html
导入:import Vue from 'vue' //app1.js

//console.log(Vue) //函数,导入成功

image.png

2.去除警告
新建Vue时报错:提示要切换为Vue完整版(默认是不完整版)

image.png 方法:  在package.json添加

"alias": {
  "vue" : "./node_modules/vue/dist/vue.common.js"
}

最后重启parcel

image.png