本实例是根据我司项目实际需求搭建,仅供参考, 本博主原创文章, 若有转载, 请备注原文连接javascript
$ npm install -g vue-cli
css
vue init <template-name> <project-name>
html
$ vue init webpack my-project
前端
$ npm install element-ui -S
vue
import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI)
$ npm install normalize.css -S
java
import 'normalize.css/normalize.css'
$ npm install --save-dev sass-loader
$ npm install --save-dev node-sass
<style lang="scss">
备注: 此vue-cli版本,utils已经配置好了sass,勿须像以前的版本,在build文件夹下的webpack.base.conf.js的rules里面添加配置node
// 配置sass全局变量 全局文件引入 固然只想编译一个文件的话能够省去这个函数 function resolveResource (name) { return path.resolve(__dirname, '../src/style/' + name) } function generateSassResourceLoader () { var loaders = [ cssLoader, 'sass-loader', { loader: 'sass-resources-loader', options: { // 多个文件时用数组的形式传入,单个文件时能够直接使用 path.resolve(__dirname, '../static/style/common.scss' resources: [resolveResource('theme.scss')] } } ] if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader' }) } else { return ['vue-style-loader'].concat(loaders) } }
备注: 若是要在全局使用公共变量, 必须执行上面两步,设置全局scss环境webpack
全局引用 ---- 在main.js文件中,引入theme.scss
import '@/style/theme.scss'
ios
全局使用
git
安装
$ npm i --save lodash
import 'lodash'
安装
$ npm install echarts --save
import echarts from 'echarts' Vue.prototype.$echarts = echarts
安装
$ npm install axios
module.exports = { proxyList: { '/apis': { target: 'http://google.com/', // 设置你调用的接口域名和端口号 别忘了加http // secure: false,// 若是是https接口,须要配置这个参数 changeOrigin: true, pathRewrite: { '^/apis': '' // 这里理解成用‘/api’代替target里面的地址,后面组件中咱们调接口时直接用api代替, 好比我要调用'http://google.com/user/login',直接写‘/api/user/login } } } }
import http from 'axios' import router from '@/router' import {Message} from 'element-ui' import {loadingInstance} from '@/utils/promptsMethods' http.defaults.headers = {'Content-Type': 'application/json;charset=utf-8'} http.defaults.responseType = 'json' http.defaults.withCredentials = true http.defaults.timeout = 5000 // 添加请求拦截器 http.interceptors.request.use(function (config) { loadingInstance() // 在发送请求以前作些什么 return config }, function (error) { loadingInstance().close() // 对请求错误作些什么 return Promise.reject(error) }) /* // 拦截器可按需设置, 不须要可删除 // 添加响应拦截器 http.interceptors.response.use(function (response) { loadingInstance().close() return response }, function (error) { loadingInstance().close() // 对响应错误 if (error.response && error.response.status === 1111) { Message.warning('登陆已过时! ) router.push({path: '/login'}) } else { Message.warning('网络异常') } return Promise.reject(error) }) */ export function request (url, data = {}, method = 'post') { return new Promise((resolve, reject) => { const options = { url, data, method } http(options) .then(res => { resolve(res.data) }) .catch(error => { reject(error) }) }) }
import {request} from '../request' const api = { login (params) { return request('/apis/user/login', params) } } export default api
目录结构以下:
使用 在App.vue中使用
npm run dev 运行查看 login接口调用状态
缘由, axios 是基于 promise 来实现的,IE 和低版本的设备不支持 promise。
$ npm install babel-polyfill -S
使用, ---- //main.js中引用
import 'babel-polyfill'
在我这个项目当中对axios进行了封装, 因此,移动到 src/api/resuest.js
接着在 webpack.base.conf.js 中,将原来的
entry: { app: './src/main.js' }
替换成
entry: ['babel-polyfill', './src/main.js'],
$ npm install nprogress
第一步: 在src文件夹下新建文件夹=->页面 login / dashboard / 404 /layout
第二步 配置路由
//src文件下, 新建router文件夹, 创建index.js
import Vue from 'vue' import Router from 'vue-router' import Layout from '@/pages/layout/layout' /* * hidden: true if `hidden:true` will not show in the sidebar(default is false) * redirect: noredirect if `redirect:noredirect` will no redirect in the breadcrumb * name:'router-name' the name is used by <keep-alive> (must set!!!) * meta : { title: 'title' the name show in submenu and breadcrumb (recommend set) icon: 'svg-name' the icon show in the sidebar breadcrumb: false if false, the item will hidden in breadcrumb(default is true) } */ export const constantRouterMap = [ { path: '/login', name: 'Login', component: () => import('@/pages/login/login'), hidden: true }, { path: '/404', component: () => import('@/pages/404/404'), hidden: true }, { path: '/', component: Layout, redirect: '/dashboard', name: 'Dashboard', hidden: true, children: [ { path: 'dashboard', component: () => import('@/pages/dashboard/dashboard') } ] }, { path: '/external-link', component: Layout, children: [ { path: 'https://panjiachen.github.io/vue-element-admin-site/#/', meta: { title: 'External Link', icon: 'link' } } ] }, { path: '*', redirect: '/404', hidden: true } ] Vue.use(Router) export default new Router({ // mode: 'history', //后端支持可开 scrollBehavior: () => ({ y: 0 }), routes: constantRouterMap })
import router from './router' import NProgress from 'nprogress' // Progress 进度条 import 'nprogress/nprogress.css'// Progress 进度条样式 const whiteList = ['/login'] // 不重定向白名单 router.beforeEach((to, from, next) => { NProgress.start() // next() if (whiteList.indexOf(to.path) !== -1) { next() } else { next(`/login`) // 不然所有重定向到登陆页 NProgress.done() } }) router.afterEach(() => { NProgress.done() // 结束Progress })
安装
$ npm install vuex
使用
//webpack.prod.conf.js
替换成
new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false, drop_debugger: true, drop_console: true, pure_funcs: ['console.log'] // 移除console.log } }, sourceMap: config.build.productionSourceMap, parallel: true })