vue+element-ui項目搭建實戰

1.使用vue ui創建vue工程

利用vue-cli提供的圖形化工具快速搭建vue工程:
命令行運行:vue ui




工程結構說明

build:項目構建webpack(打包器)相關代碼
config:配置目錄,包括端口號等
node_modules:npm加載的項目依賴模塊
src:主要代碼開發目錄:
|----assets:放置一些圖片
|----components:放置組件文件
|----App.vue:項目入口文件
|----main.js:項目的js核心文件
|----router:Vue路由文件目錄, 在router/index.js中可以定義不同url訪問不同的內容
static:靜態資源目錄,如圖片,字體等
test:測試目錄


index.html:首頁入口文件,可以添加meta信息或統計代碼
package.json:項目配置文件
README.md:項目的說明文檔

2.安裝element-ui

<code>npm install element-ui --save 1/<code>

main.js中添加引用

<code>import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) 1234/<code>

3.配置vscode,setting.json

打開vscode, 文件–首選項–設置,添加如下配置:

<code>{ //主題設置 "workbench.colorTheme": "Monokai", // 默認編輯器字號 "editor.fontSize": 14, //是否自動換行 "editor.wordWrap": "on", // tab幾個縮進 "editor.tabSize": 2, // 文件自動保存 "files.autoSave": "off", // 自動格式化粘貼的代碼 "editor.formatOnPaste": true, // 在資源管理器刪除內容時候是否進行用戶提醒 "explorer.confirmDelete": false, // 控制在資源管理器內拖放移動文件或文件夾時是否進行確認 "explorer.confirmDragAndDrop": false, // 在資源管理器拖拽文件是否進行用戶提醒 "workbench.statusBar.visible": true, // 工作區縮放級別 "window.zoomLevel": 0, // 重命名或移動文件時,啟用或禁用自動更新導入路徑 "javascript.updateImportsOnFileMove.enabled": "always", // 啟用/禁用導航路徑 "breadcrumbs.enabled": true, // 終端cmd字號 "terminal.integrated.fontSize": 16, // 不檢查縮進,保存後統一按設置項來設置 "editor.detectIndentation": false, // 編輯器初始界面 "workbench.startupEditor": "newUntitledFile", // 工作臺狀態欄是否可見 "workbench.statusBar.feedback.visible":false, // 添加多個光標時候需要的快捷鍵 "editor.multiCursorModifier": "ctrlCmd", // 自定義代碼片段顯示的位置 "editor.snippetSuggestions": "top", "window.menuBarVisibility": "toggle", // 啟用後,按下 TAB 鍵,將展開 Emmet 縮寫。 "emmet.triggerExpansionOnTab": true, // 控制編輯器在空白字符上顯示符號的方式 "editor.renderWhitespace": "all", // 控制編輯器是否應呈現空白字符 "editor.renderControlCharacters": false, // 在文件和文件夾上顯示錯誤和警告 "problems.decorations.enabled": false, // html文件格式化程序 "[html]": { "editor.defaultFormatter": "vscode.html-language-features", // 禁止eslint對html進行校驗 "editor.codeActionsOnSave": { "source.fixAll.eslint": false } }, // "[javascript]": { // "editor.defaultFormatter": "vscode.typescript-language-features" // }, // vscode-fileheader -----settings begin----- // 文件作者 "fileheader.Author": "laoxu", // 文件最後修改者 "fileheader.LastModifiedBy": "laoxu", // vscode-fileheader -----settings end----- //stylelint -----settings begin----- "css.validate": false, "less.validate": false, "scss.validate": false, "stylelint.enable": false, //stylelint -----settings end----- // eslint -----settings begin----- // 是否為JavaScript文件開啟eslint檢測 "eslint.enable": true, // 保存之後進行lint "eslint.run": "onSave", // 是否啟用eslint的調試模式 "eslint.debug": true, // 保存文件時進行eslint修復(MacOS:快捷鍵是 command + s ),並不能修復所有問題,多數還是需要手動修復 "editor.codeActionsOnSave":{ "source.fixAll.eslint": true } // eslint -----settings end----- } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192/<code>

4.添加vue.config.js

在項目根路徑添加。

<code>module.exports = { /** 區分打包環境與開發環境 * process.env.NODE_ENV==='production' (打包環境) * process.env.NODE_ENV==='development' (開發環境) * baseUrl: process.env.NODE_ENV==='production'?"https://cdn.didabisai.com/front/":'front/', */ // 項目部署的基礎路徑 // 我們默認假設你的應用將會部署在域名的根部, // 例如 https://www.my-app.com/ // 如果你的應用部署在一個子路徑下,那麼你需要在這裡 // 指定子路徑。比如將你的應用部署在 // https://www.foobar.com/my-app/ // 那麼將這個值改為 '/my-app/' outputDir: 'dist', // where to put static assets (js/css/img/font/...) // 是否在保存時使用‘eslint-loader’進行檢查 // 有效值: true | false | 'error' // 當設置為‘error’時,檢查出的錯誤會觸發編譯失敗 lintOnSave: true, // 使用帶有瀏覽器內編譯器的完整構建版本 // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only runtimeCompiler: false, // babel-loader默認會跳過`node_modules`依賴. // 通過這個選項可以顯示轉譯一個依賴 transpileDependencies: [ /* string or regex */ ], // 是否為生產環境構建生成sourceMap? productionSourceMap: false, // 調整內部的webpack配置. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md chainWebpack: () => {}, configureWebpack: () => {}, // CSS 相關選項 css: { // 將組件內部的css提取到一個單獨的css文件(只用在生產環境) // 也可以是傳遞給 extract-text-webpack-plugin 的選項對象 extract: true, // 允許生成 CSS source maps? sourceMap: false, // pass custom options to pre-processor loaders. e.g. to pass options to // sass-loader, use { sass: { ... } } loaderOptions: {}, // Enable CSS modules for all css / pre-processor files. // This option does not affect *.vue files. modules: false }, // use thread-loader for babel & TS in production build // enabled by default if the machine has more than 1 cores parallel: require('os').cpus().length > 1, // PWA 插件相關配置 // see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa pwa: {}, // configure webpack-dev-server behavior devServer: { open: process.platform === 'darwin', disableHostCheck: false, host: '0.0.0.0', port: 9527, https: false, hotOnly: false, // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#configuring-proxy // 跨域配置 proxy: { '/api': { target: 'http://localhost:9000/api/', // 設置你調用的接口域名和端口號 別忘了加http changeOrigin: true, pathRewrite: { '^/api': '' // 這裡理解成用‘/api’代替target裡面的地址,後面組件中我們掉接口時直接用api代替 比如我要調用'http://40.00.100.100:3002/user/add',直接寫‘/api/user/add’即可 } } } // string | Object // before: app => {} }, // 第三方插件配置 pluginOptions: { // ... } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859/<code>

5.安裝axios

<code>npm install axios 1/<code>

main.js引入axios

<code>import axios from 'axios' 1/<code>

6.添加view

6.1 添加views/HelloPage.vue

<code>

{{msg}}

Element UI Button

默認按鈕 主要按鈕 文字按鈕

12345678910111213141516171819202122/<code>

6.2 添加views/BookList.vue

<code> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/<code>

6.3 修改路由router/index.js

<code>import HelloPage from '@/views/HelloPage' import BookList from '@/views/BookList' ... ... { path: '/hello', name: 'Hello', component: HelloPage }, { path: '/bookList', name: 'BookList', component: BookList } 1234567891011121314/<code>

7 測試

訪問:localhost:9527/#/hello