最近重构了我以前项目 qq 音乐移动端,使用的技术是 vue,vuex,vue-router,和 typescript,在这期间,遇到的问题仍是蛮多的,一下子我会把我遇到的问题以及解决方法列出来,避免忘记。
重构完成的项目 ===> vue-qq-musicjavascript
TypeScript与Vue全家桶的配置能够参考如下两篇文章(在这里由衷感谢两位做者):vue
为何我要将TypeScript
和 Vue
集成呢?由于TypeScript 有如下几个优点:java
ts 没法识别$refwebpack
解决方法
① 直接在 this.$refs.xxx 后面申明类型如:git
this.$refs.lyricsLines as HTMLDivElement;
② 在export default class xxx extends Vue
里面声明所有的$ref 的类型es6
$refs: { audio: HTMLAudioElement, lyricsLines: HTMLDivElement }
ts 没法识别 requiregithub
解决方法web
安装声明文件vue-router
yarn add @types/webpack-env -D
运行npm run build
出现vuex
解决方法
You can fix this by using the most recent beta version ofuglifyjs-webpack-plugin
. Our team is working to remove completely the UglifyJsPlugin from within webpack, and instead have it as a standalone plugin.If you do
yarn add uglifyjs-webpack-plugin@beta --dev
ornpm install uglifyjs-webpack-plugin@beta --save-dev
you should receive the latest beta which does successfully minify es6 syntax. We are hoping to have this released from beta extremely soon, however it should save you from errors for now!
也就是说升级你的uglifyjs-webpack-plugin版本:yarn add uglifyjs-webpack-plugin@beta --dev
vue-property-decorator 装饰器写法不对。当时我是要把 mixins,注入到组件里,我就这样写:
ts提示找不到 mixin。我就很纳闷为何找不到名字,因为官网vue-property-decorator例子太少,只好一步一步摸索?
解决方法
把mixins写在@Component里面...,像这样:
Property '$xxx' does not exist on type 'App'
的话,那么能够在vue-shim.d.ts
增长declare module 'vue/types/vue' { interface Vue { $xxx: any, } }
通过几天的折腾,终于把项目重构完成,我我的认为加上 TypeScript
,确实效率挺高了许多,不过 Vue+TypeScript
仍是没 Angular
支持那么完善,相信以后 vue 对于 ts 的集成会更加完善!