Skip to content

Commit

Permalink
提取layout,,统一代码风格,优化路由
Browse files Browse the repository at this point in the history
  • Loading branch information
sillence committed Jun 1, 2020
1 parent 367c251 commit 842a3eb
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 88 deletions.
1 change: 0 additions & 1 deletion src/api/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import api from './index'

// axios
import request from '@/utils/request'

Expand Down
13 changes: 6 additions & 7 deletions src/assets/css/index.scss
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
@import './variables.scss';
@import './mixin.scss';

html,
body
.app {
body .app {
color: #333333;
font-family: Arial, Helvetica, 'STHeiti STXihei', 'Microsoft YaHei', Tohoma, sans-serif;
background-color: $background-color;
}
.app-container{
padding-bottom:50px;
}

.app-container {
padding-bottom: 50px;
}
41 changes: 0 additions & 41 deletions src/config/router.config.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/layouts/AppLayout.vue

This file was deleted.

5 changes: 2 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import App from './App.vue'
import router from './router'
import store from './store'

// 引入全局样式
import '@/assets/css/index.scss'
// 设置 js中可以访问 $cdn
import { $cdn } from '@/config'
Vue.prototype.$cdn = $cdn

// 全局引入按需引入UI库 vant
import '@/plugins/vant'

// 引入全局样式
import '@/assets/css/index.scss'
// 移动端适配
import 'lib-flexible/flexible.js'

Expand Down
19 changes: 10 additions & 9 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from '@/config/router.config'
import { constantRouterMap } from './router.config.js'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
Router.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const createRouter = () => new Router({
mode: 'history',
base: process.env.BASE_URL,
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})
const createRouter = () =>
new Router({
mode: 'history',
base: process.env.BASE_URL,
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
export function resetRouter() {
const newRouter = createRouter()
router.matcher = newRouter.matcher // reset router
}
Expand Down
29 changes: 29 additions & 0 deletions src/router/router.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 基础路由
* @type { *[] }
*/
export const constantRouterMap = [
{
path: '/',
component: () => import('@/views/layouts/index'),
redirect: '/home',
meta: {
title: '首页',
keepAlive: false
},
children: [
{
path: '/home',
name: 'Home',
component: () => import('@/views/home/index'),
meta: { title: '首页', keepAlive: false }
},
{
path: '/about',
name: 'About',
component: () => import('@/views/home/about'),
meta: { title: '关于我', keepAlive: false }
}
]
}
]
8 changes: 4 additions & 4 deletions src/views/home/about.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- home -->
<template>
<div class="app-container">
<div class="about-container">
<div class="warpper">
<div class="list">
<div class="logo"></div>
Expand Down Expand Up @@ -46,8 +46,8 @@ export default {
// 请求接口数据,仅作为展示,需要配置src->config下环境文件
const params = { user: 'sunnie' }
getUserInfo(params)
.then(() => {})
.catch(() => {})
.then(() => { })
.catch(() => { })
},
// Action 通过 store.dispatch 方法触发
doDispatch() {
Expand All @@ -60,7 +60,7 @@ export default {
}
</script>
<style lang="scss" scoped>
.app-container {
.about-container {
background: #fff;
height: 100vh;
box-sizing: border-box;
Expand Down
6 changes: 3 additions & 3 deletions src/views/home/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- home -->
<template>
<div class="app-container">
<div class="index-container">
<div class="warpper">
<h1 class="demo-home__title"><img src="https://imgs.solui.cn/weapp/logo.png" /><span> VUE H5开发模板</span></h1>
<h2 class="demo-home__desc">
Expand Down Expand Up @@ -39,13 +39,13 @@ export default {
computed: {},
mounted() {},
mounted() { },
methods: {}
}
</script>
<style lang="scss" scoped>
.app-container {
.index-container {
.warpper {
padding: 12px;
background: #fff;
Expand Down
8 changes: 4 additions & 4 deletions src/layouts/TabBarLayout.vue → src/views/layouts/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<div class="tabbar-layout-containter">
<div class="tabbar-layout-content">
<div class="app-container">
<div class="layout-content">
<keep-alive v-if="$route.meta.keepAlive">
<router-view></router-view>
</keep-alive>
<router-view v-else></router-view>
</div>
<div class="tabbar-layout-footer">
<div class="layout-footer">
<TabBar :data="tabbars" @change="handleChange" />
</div>
</div>
Expand All @@ -15,7 +15,7 @@
<script>
import TabBar from '@/components/TabBar'
export default {
name: 'TabBarLayout',
name: 'AppLayout',
data() {
return {
tabbars: [
Expand Down
3 changes: 1 addition & 2 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ module.exports = {
// }
},
css: {
extract: IS_PROD,
extract: IS_PROD, //是否将组件中的 CSS 提取至一个独立的 CSS 文件中 (而不是动态注入到 JavaScript 中的 inline 代码)。
sourceMap: false,
loaderOptions: {
scss: {
// 向全局sass样式传入共享的全局变量, $src可以配置图片cdn前缀
// 详情: https://cli.vuejs.org/guide/css.html#passing-options-to-pre-processor-loaders
prependData: `
@import "assets/css/index.scss";
@import "assets/css/mixin.scss";
@import "assets/css/variables.scss";
$cdn: "${defaultSettings.$cdn}";
Expand Down

0 comments on commit 842a3eb

Please sign in to comment.