以前已經介紹了關於Vue的腳手架vue-cli
的安裝,以及一些文件目錄介紹。具體能夠查看《vue 入坑教程(一)——搭建vue-cli腳手架》css
下面簡單說一下具體的文件介紹html
package.json
是項目的配置文件,裏面是項目的相關的包依賴,npm運行命令,位於項目根目錄。vue
{ "name": "ddlcwecaht", ---------------------項目名稱 "version": "1.0.0", ---------------------項目版本號(以上二者是必須的,不然沒法執行install) "description": "A Vue.js project", ---------------------項目描述 "author": "wujy", ---------------------做者名稱(以上這些都和vue-cli搭建的時候填寫的信息是一致的,固然這裏也能夠修改) "private": true, "scripts": { ---------------------定義npm命令 "dev": "webpack-dev-server --inline --progress --open --config build/webpack.dev.conf.js", ------------cnpm run dev 運行項目 "start": "npm run dev", ----------------------和上面的相同,運行項目 "build": "node build/build.js" ----------------------cnpm run build項目打包構建 }, "dependencies": { ---------------------運行時的依賴 "axios": "^0.18.0", "vue": "^2.5.2", "vue-router": "^3.0.1", "vuex": "^3.0.1", "vux": "^2.9.0" }, "devDependencies": { -----------------------開發時的依賴 "autoprefixer": "^7.1.2", "babel-core": "^6.22.1", "babel-helper-vue-jsx-merge-props": "^2.0.3", "babel-loader": "^7.1.1", "babel-plugin-import": "^1.7.0" …… }, "engines": { -------------------------環境的版本號 "node": ">= 6.0.0", "npm": ">= 3.0.0" }, "browserslist": [ ------------------------瀏覽器的版本號 "> 1%", "last 2 versions", "not ie <= 8" ] }
指定了運行腳本命令的npm
命令行縮寫,好比start
指定了運行npm run start
時,所要執行的命令。html5
dev: "webpack-dev-server --inline --progress --open --config build/webpack.dev.conf.js"
執行命令:cnpm run dev
,其中webpack-dev-server
啓動本地的服務器, --inline --progress
啓動時分行展現啓動的進度條,--open
啓動完成後自動打開瀏覽器,--config build/webpack.dev.conf.js
啓動時調用的配置文件node
"build": "node build/build.js"
執行的命令: cnpm run build
,build/build.js
運行時調用的配置文件,命令執行以後會生成一個dist
文件夾,裏面存放的是打包構建後的文檔,用於正式環境。webpack
這兩項分別是項目運行所依賴的模塊、項目開發所依賴的模塊。他們的成員好比"vue": "^2.5.2",
分別由模塊名和對應的版本號組成,表示依賴的模塊及其版本範圍。ios
這些依賴在執行了cnpm install
以後都會添加到node_modules
文件夾中。es6
能夠經過命令添加本身須要的依賴:(這裏使用的是淘寶鏡像)web
#將該模塊寫入dependencies屬性 cnpm install <name> --save #該模塊寫入devDependencies屬性 cnpm install <name> --save-dev
命令執行結束以後,刷新,能夠看到package.json
裏面對應的增長了剛纔下載的依賴,一樣的在node_modules
文件夾裏也增長了相同對應的依賴vue-router
分別表示項目所須要的node.js
版本號、瀏覽器的版本號。
注意:
- vue不支持IE8及其如下的版本。
- 注意本地的node的版本號是否符合。 能夠打開命令行控制面板,經過
node -v
和npm -v
查看本地的版本號package.json
文檔中不能加註釋,否則會報錯
/* 使用模塊化機制編程,導入Vue和VueRouter,要調用 Vue.use(VueRouter)*/ import Vue from 'vue' import Router from 'vue-router' Vue.use(Router); /*定義路由*/ export default new Router({ /*當切換到新路由時,想要頁面滾到頂部。不設置頁面將在默認的地方*/ scrollBehavior(to, from, savedPosition) { return {x: 0, y: 0} }, routes: [{ ------------------------配置路由 path: '/', ------------------------路由路徑 redirect: '/home' ------------------------路由重定向 }, { path: '/home', name: 'home', -------------------------路由須要的組件 component: (resolve) => require(['@/views/home/home.vue'], resolve), }, { path: '/login', name: 'login', component: (resolve) => require(['@/views/login/login.vue'], resolve), meta: { ---------------路由的元信息 title: '登陸' } }, }] })
具體的能夠參考官網vue-router
代碼中能夠經過this.$router
來使用,實現頁面跳轉,路由信息的傳值。
這是webpack
最爲基礎的配置文件,主要是來定義入口文件,處理vue,babel
等各類模塊。由此還有兩個配置文件分別是開發環境配置文件 webpack.dev.conf.js
和生產模式配置文件 webpack.prod.conf.js
/*定義變量,引入須要的配置*/ 'use strict' const path = require('path') const utils = require('./utils') const config = require('../config') const vueLoaderConfig = require('./vue-loader.conf') const vuxLoader = require('vux-loader') var PostCompilePlugin = require('webpack-post-compile-plugin') var TransformModulesPlugin = require('webpack-transform-modules-plugin') /*處理路徑的函數*/ function resolve(dir) { return path.join(__dirname, '..', dir) } module.exports = { context: path.resolve(__dirname, '../'), //基礎路徑 entry: { app: './src/main.js' //入口文件 }, output: { path: config.build.assetsRoot, //輸出文件,默認是打包編譯以後會生成一個dist文件夾來存儲輸出文件 filename: '[name].js', //輸出文件名字 publicPath: process.env.NODE_ENV === 'production' ? //生產環境和開發環境判斷,來肯定引用的publicpath config.build.assetsPublicPath : config.dev.assetsPublicPath }, resolve: { //解析肯定的擴展名,方便模塊的導入 extensions: ['.js', '.vue', '.json'], alias: { //建立別名 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src'), //方便模塊的引用,好比@/components/HelloWorld = src/components/HelloWorld } }, module: { //模塊的相關配置 rules: [{ test: /\.vue$/, loader: 'vue-loader', options: vueLoaderConfig }, { test: /\.js$/, loader: 'babel-loader', //babel,用來將es6轉換爲es5,解決部分瀏覽器不支持es6的問題 include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')] }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, //圖片類型 loader: 'url-loader', //url-loader 文件大小低於指定的限制時,可返回 DataURL,即base64 options: { limit: 10000, //默認無限制 name: utils.assetsPath('img/[name].[hash:7].[ext]') } }, { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, //音頻類型 loader: 'url-loader', options: { limit: 10000, name: utils.assetsPath('media/[name].[hash:7].[ext]') } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, //字體類型 loader: 'url-loader', options: { limit: 10000, name: utils.assetsPath('fonts/[name].[hash:7].[ext]') } } ] }, node: { setImmediate: false, dgram: 'empty', fs: 'empty', net: 'empty', tls: 'empty', child_process: 'empty' }, plugins:[ //能夠添加自定義的插件 new PostCompilePlugin(), new TransformModulesPlugin() ] }
開發環境的配置文件在項目啓動的時候就會運用的,比較重要。
'use strict' const utils = require('./utils') const webpack = require('webpack') const config = require('../config') //基礎配置的參數 const merge = require('webpack-merge') //用來合併webpack配置文件的 const path = require('path') const baseWebpackConfig = require('./webpack.base.conf') //引入webpack基礎的配置文件 const CopyWebpackPlugin = require('copy-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') //幫你自動生成一個html5文件, 這個文件中自動引用了你打包後的JS文件。每次編譯都在文件名中插入一個不一樣的hash值。能夠去打包後的dist文件夾中查看html文件 const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') //能在終端更好的查看webpack的警告和錯誤 const portfinder = require('portfinder') //自動檢索下一個可用端口,好比8080端口被佔用,啓動後會自動調用8081端口打開項目 const HOST = process.env.HOST //讀取系統環境變量host const PORT = process.env.PORT && Number(process.env.PORT) //讀取系統環境變量端口號 const devWebpackConfig = merge(baseWebpackConfig, { //合併基礎配置文件 module: { rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true }) }, devtool: config.dev.devtool, devServer: { //webpack-dev-server服務器配置 clientLogLevel: 'warning', historyApiFallback: { rewrites: [ { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') }, ], }, hot: true, //開啓熱加載,熱加載是指代碼啓動後,修改代碼,會自動檢測代碼的更新,瀏覽器自動渲染更新的部分 contentBase: false, compress: true, host: HOST || config.dev.host, port: PORT || config.dev.port, open: config.dev.autoOpenBrowser, //啓動項目的時候自動打開瀏覽器,默認的是false。能夠在config/index.js中修改成true,啓動的時候就會自動打開瀏覽器 overlay: config.dev.errorOverlay ? { warnings: false, errors: true } : false, publicPath: config.dev.assetsPublicPath, proxy: config.dev.proxyTable, //代理設置,先後端分離,解決跨域問題 quiet: true, // necessary for FriendlyErrorsPlugin watchOptions: { poll: config.dev.poll, } }, plugins: [ //webpack一些構建用到的插件 new webpack.DefinePlugin({ 'process.env': require('../config/dev.env') }), new webpack.HotModuleReplacementPlugin(), //模塊熱替換它容許在運行時更新各類模塊,而無需進行徹底刷新 new webpack.NamedModulesPlugin(), new webpack.NoEmitOnErrorsPlugin(), /*打包後會自動生成一個html文件,並自動將 打包過程當中輸出的js、css的路徑添加到html文件中,css文件插入到head中*/ new HtmlWebpackPlugin({ filename: 'index.html', // 指定編譯後生成的html文件名 template: 'index.html', inject: true }), new CopyWebpackPlugin([ { from: path.resolve(__dirname, '../static'), to: config.dev.assetsSubDirectory, ignore: ['.*'] } ]) ] }) module.exports = new Promise((resolve, reject) => { portfinder.basePort = process.env.PORT || config.dev.port portfinder.getPort((err, port) => { if (err) { reject(err) } else { process.env.PORT = port; devWebpackConfig.devServer.port = port; devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ //webpack錯誤提示的插件 compilationSuccessInfo: { messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`], }, onErrors: config.dev.notifyOnErrors ? utils.createNotifierCallback() : undefined })) resolve(devWebpackConfig) } }) })
<template> <div id="app"> <img src="./assets/logo.png"> <router-view></router-view> </div> </template> <script> export default { name: 'app', data(){ return {} }, methods:{} } </script> <style lang="scss" scoped> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
<template></template>
標籤內存放的是 HTMLDOM 結構,注意:只能有一個頂級標籤<script></script>
標籤內存放的是 js 內容,用來寫邏輯代碼。裏面有data,methods,props,components……
等,具體的能夠參考Vue官網<style></style>
標籤內放的是 CSS 樣式,加上scoped
表示該樣式只在這個.vue
文件中有效,還能夠用scss
來寫,具體請自行百度查詢。import Vue from 'vue' import router from './router' Vue.config.productionTip = false; //生產環境提示,這裏設置成了false // 引入reset.scss import './assets/styles/reset.scss' // 引入小圖標 import 'font-awesome/css/font-awesome.css' /*引入vue-awesome-swiper組件和樣式,swsiper參考4.x的API,前提,先安裝vue-awesome-swiper插件*/ import VueAwesomeSwiper from 'vue-awesome-swiper' import 'swiper/dist/css/swiper.css' Vue.use(VueAwesomeSwiper); /*引入vuex*/ import store from '@/utils/vuex.js' new Vue({router, store}).$mount('#app');
項目的入口文件main.js
,全局的設置能夠在這裏添加。
.editorconfig
.babelrc
utils.js
build.js
webpack.prod.conf.js
寫的有點亂,本文的參考文章:《vue-cli項目結構詳解》,做者博客:tanzhenyan的博客