Skip to content

Commit

Permalink
'svgIcon组件/鉴权功能/动态设置页面标题/资源优化'
Browse files Browse the repository at this point in the history
  • Loading branch information
lujiehui committed Oct 15, 2019
1 parent cce97e3 commit 9d89d33
Show file tree
Hide file tree
Showing 30 changed files with 731 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VUE_CLI_BABEL_TRANSPILE_MODULES = true
NODE_ENV = 'development'
BASE_URL = '/'
VUE_APP_BASE_API = ''
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"axios": "^0.19.0",
"core-js": "2.6.5",
"js-cookie": "^2.2.1",
"lib-flexible": "^0.3.2",
"vant": "^2.2.5",
"vue": "^2.6.10",
Expand All @@ -28,12 +29,18 @@
"body-parser": "^1.19.0",
"chalk": "^2.4.2",
"chokidar": "^3.2.1",
"compression-webpack-plugin": "^3.0.0",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"mockjs": "^1.0.1-beta3",
"postcss-pxtorem": "^4.0.1",
"sass": "^1.18.0",
"sass-loader": "^7.1.0",
"vue-template-compiler": "^2.6.10"
"script-ext-html-webpack-plugin": "^2.1.4",
"svg-sprite-loader": "^4.1.6",
"svgo": "^1.3.0",
"uglifyjs-webpack-plugin": "^2.2.0",
"vue-template-compiler": "^2.6.10",
"webpack-bundle-analyzer": "^3.5.2"
}
}
8 changes: 8 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- <meta name="viewport" content="width=device-width,initial-scale=1.0"> -->
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<% if (process.env.NODE_ENV === 'production') { %> <% for(var css of
htmlWebpackPlugin.options.cdn.css) { %>
<link href="<%=css%>" rel="preload" as="style" />
<link rel="stylesheet" href="<%=css%>" as="style" />
<% } %> <% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
<link href="<%=js%>" rel="preload" as="script" />
<script src="<%=js%>"></script>
<% } %> <% } %>
<title>vue-cli-h5</title>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div id="app">
<transition :name="transitionName">

<keep-alive v-if="$route.meta.keepAlive">
<router-view class="router"></router-view>
</keep-alive>
Expand Down
62 changes: 62 additions & 0 deletions src/components/SvgIcon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
<svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
<use :xlink:href="iconName" />
</svg>
</template>

<script>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
import { isExternal } from '@/utils/validate'
export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
}
},
computed: {
isExternal () {
return isExternal(this.iconClass)
},
iconName () {
return `#icon-${this.iconClass}`
},
svgClass () {
if (this.className) {
return 'svg-icon ' + this.className
} else {
return 'svg-icon'
}
},
styleExternalIcon () {
return {
mask: `url(${this.iconClass}) no-repeat 50% 50%`,
'-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
}
}
}
}
</script>

<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
.svg-external-icon {
background-color: currentColor;
mask-size: cover!important;
display: inline-block;
}
</style>
9 changes: 9 additions & 0 deletions src/icons/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component

// register globally
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)
1 change: 1 addition & 0 deletions src/icons/svg/dashboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/eye-open.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/form.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/nested.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/password.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/table.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/svg/user.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/icons/svgo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# replace default config

# multipass: true
# full: true

plugins:

# - name
#
# or:
# - name: false
# - name: true
#
# or:
# - name:
# param1: 1
# param2: 2

- removeAttrs:
attrs:
- 'fill'
- 'fill-rule'
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Vue from 'vue'
import 'lib-flexible'
import App from './App.vue'
import router from './router'
import store from './store'
import router from '@/router'
import store from '@/store'
import 'utils/permission'
import '@/icons' // icon
import { mockXHR } from '../mock'
import '@/style/common.scss'

Expand Down
5 changes: 4 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ let routes = [
{
path: '/',
name: 'home',
component: Home
component: Home,
meta: {
keepAlive: true
}
}
]

Expand Down
16 changes: 16 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {

title: 'h5Vue-cli',

/**
* @type {boolean} true | false
* @description Whether fix the header
*/
fixedHeader: false,

/**
* @type {boolean} true | false
* @description Whether show the logo in sidebar
*/
sidebarLogo: false
}
4 changes: 4 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default new Vuex.Store({
loading (state) {
return state.loading
}
// vuex 全局getters引入局部
// token () {
// return store.getters['user/token']
// }
},
mutations: {
setMsg (state, msg) {
Expand Down
6 changes: 4 additions & 2 deletions src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { login } from 'api/user'
import { Toast } from 'vant'
import { getToken, setToken } from '@/utils/auth'

const LOGIN = 'LOGIN'// 获取用户信息

export default {
namespaced: true,
state: {
token: localStorage.getItem('token') || '',
token: getToken() || '',
user: JSON.parse(localStorage.getItem('userDate')) || {}
},
mutations: {
Expand All @@ -15,7 +16,8 @@ export default {
let userDate = data.data
state.token = userDate.token
state.user = userDate
localStorage.setItem('token', userDate.token)
setToken(userDate.token)
// localStorage.setItem('token', userDate.token)
localStorage.setItem('userDate', JSON.stringify(userDate))
}

Expand Down
15 changes: 15 additions & 0 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Cookies from 'js-cookie'

const TokenKey = 'h5-vue-cli_token'

export function getToken () {
return Cookies.get(TokenKey)
}

export function setToken (token) {
return Cookies.set(TokenKey, token)
}

export function removeToken () {
return Cookies.remove(TokenKey)
}
10 changes: 10 additions & 0 deletions src/utils/get-page-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import defaultSettings from '@/settings'

const title = defaultSettings.title || 'Vue Admin Template'

export default function getPageTitle (pageTitle) {
if (pageTitle) {
return `${pageTitle} - ${title}`
}
return `${title}`
}
Loading

0 comments on commit 9d89d33

Please sign in to comment.