最近在用vue搭一個后臺管理的單頁應用的demo,因為之前只用過vue-cli2+javascript進行開發,而vue-cli3早在去年8月就已經發布,并且對于typescript有了很好地支持。所以為了熟悉新技術,我選擇使用vue-cli3+typescript進行新應用的開發。這里是新技術的學習記錄。
初始化項目
卸載老版本腳手架,安裝新版本腳手架后,開始初始化項目。初始化的命令跟2.x版本的略有不同,以前是 vue init webpack project-name ,而現在是 vue create project-name 。vue-cli3已經完全把webpack綁定了,這也就意味著無法像以前那樣選擇別的打包工具比如webpack-simple。如果一定要用webpack-simple,可以額外安裝 @vue/cli-init ,可以在不卸載cli3的情況下使用init命令進行初始化。輸入create命令后,可以選擇初始配置。為了學習,我選擇自定義,并把所有可選內容都勾選上。其余配置項基本就按默認的來,最終的配置情況如下。
? Please pick a preset: Manually select features? Check the features needed for your project: (Press <space> to select, <a> to toggle all, <i> to invert selection)Babel, TS, PWA, Router, Vuex, CSS Pre-processors, Linter, Unit, E2E? Use class-style component syntax? Yes? Use Babel alongside TypeScript for auto-detected polyfills? Yes? Use history mode for router? (Requires proper server setup for index fallback in production) No? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)? Pick a linter / formatter config: Basic? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i> to invert selection)Lint on save? Pick a unit testing solution: Jest? Pick a E2E testing solution: Cypress? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In package.json? Save this as a preset for future projects? (y/N) n
然后需要一點時間來下載npm包,初始化完成后,看一下工程目錄,可以看到跟vue-cli2的還是有很多不一樣的地方。router和store都變成了單獨的文件,而不是以前的文件夾,當然如果有需要的話可以自己建這兩個文件夾。
最大的區別在于webpack配置都被隱藏起來了,默認沒有了那些config文件,現在如果需要修改webpack配置項,可以在根目錄新建一個 vue.config.js進行配置。這種的配制方法在2.x版本也可以用,內容也跟之前的類似。
module.exports = { baseUrl: '/', devServer: { before: app => { }, proxy: { '/api': { target: 'http://api.com', changeOrigin: true } } }, configureWebpack: { resolve: { alias: { 'coms': '@/components' } } }}
新聞熱點
疑難解答
圖片精選