这是cnpm
的锅.原因不详.解决办法是换用npm安装此模块.(我试过移除node_modules文件,报错依旧)
如果你用了Ajax相关的库,比如vue-resource/axios的话
去掉它,自己实现一个Ajax库吧.
比如我的项目中只涉及了get,post
,那么vue-resource/axios对我来说就很没必要了.
所以我就封装个库(支持Promise,不支持IE)在Vue中当作插件使用:
/* xhr.js */ class XHR { get(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304)) { if (xhr.responseText) { resolve(JSON.parse(xhr.responseText)); } else { resolve(xhr.responseText); } } else { reject(`XHR unsuccessful:${xhr.status}`); } } }; xhr.open('get', url, true); xhr.setRequestHeader('content-type', 'application/json'); xhr.send(null); }); } post(url, data) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304)) { resolve(JSON.parse(xhr.responseText)); } else { reject(`XHR unsuccessful:${xhr.status}`); } } }; xhr.open('post', url, true); xhr.setRequestHeader('content-type', 'application/json'); xhr.send(JSON.stringify(data)); }); } } /* Vue插件要求提供install方法:https://cn.vuejs.org/v2/guide/plugins.html */ XHR.install = (Vue) => { Vue.prototype.$get = new XHR().get; Vue.prototype.$post = new XHR().post; }; export default XHR;
这种方法一般能缩小文件几十KB.比如vue-resource有35KB,我的这个xhr.js只有1.9KB.
3.代码分块/Code Splitting
顾名思义,就是讲代码分成块,按需加载.这样,如果首屏不需要的块,就不用加载了.
对于大型项目可能更有用,因为在我的这个项目中首页需要的文件和其他页面需要的基本一样,所以代码分块对我这个项目而言,就没必要了.
4. 路由组件懒加载
当打包构建应用时,Javascript 包会变得非常大,影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了
结合 Vue 的异步组件 和 Webpack 的 code splitting feature,可以轻松实现路由组件的懒加载.
我们要做的就是把路由对应的组件定义成异步组件:
const Foo = resolve => { /* require.ensure 是 Webpack 的特殊语法,用来设置 code-split point (代码分块)*/ require.ensure(['./Foo.vue'], () => { resolve(require('./Foo.vue')) }) } /* 另一种写法 */ const Foo = resolve => require(['./Foo.vue'], resolve);
不需要改变任何路由配置,跟之前一样使用 Foo:
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo } ] })
4. Webpack2 Tree-shaking
Tree-shaking
用来消除没有用到的代码.
个人小项目一般用不到tree-shaking.因为你不会写没用到的代码.规模很大的项目或许可以尝试使用它.
5. 减少XHR中不必要的数据.
比如我的项目中,首页只需要博客Title,id和Tags.Time和Content不需要了,况且Content一般还很大(一般一篇10KB左右).
6. SSR(Server Side Render/服务端渲染)
这个有点难搞.但效果貌似挺不错.我之前在Vue文档中简单看了一边,打算以后有需求了再搞不迟.
7. 其他诸如图片懒加载就不赘述了,前端开发者应该有的常识.