vue 使用 Element-ui 组件库 - 按需引入
按需引入就是一不同于全部引入,不会一上来引入全局的组件和样式,而是看使用了哪些组件,根据使用的组件进行注册,并且只引入这些组件相关的样式,下面开始操作
首先就是先把完整引入的代码进行注释,然后查看官网

点击这个蓝色的地址,会跳向 github,值得注意的是这个按需引入的作者是 ant design 团队进行开发的(ant design 也推出了可以给 vue 使用的 ui 组件库)

接下来回到文档,安装文档说明进行操作
安装 babel-plugin-component

打开终端,输入:
复制代码

修改.babelrc(babel.config.js)
最新的脚手架已经不叫了.babelrc,而是 babel.config.js

引入需要的组件


解决报错
这时候启动,发现报错了,这个错误不是我们的锅,是由于脚手架和组件库不统一造成的

提示什么东西 not found 就使用 npm i xxx 安装即可

这时候又报错了

这个错误是因为脚手架版本的问题,更换即可 babel 预设环境即可,这个错误比较恶心
如果说缺什么安装什么即可
复制代码

改过之后就解决了

再次访问页面发现样式还在,并且体积变小了

版权声明: 本文为 InfoQ 作者【我搬去水星了】的原创文章。
原文链接:【http://xie.infoq.cn/article/bc2dcb3b7b87e50dcdc9dc16c】。文章转载请联系作者。
评论