router.js 4.78 KB
import Vue from 'vue'
import Router from 'vue-router'
import Index from './pages/index/index.vue'

import tc from '@common/lang/tc.js'
import zh from '@common/lang/zh.js'
import en from '@common/lang/en.js'

Vue.use(Router)

/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

const routes = [

	{
		path: '/',
		name: 'index',
		component: Index,
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('./pages/login/login.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('./pages/register/register.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/password/find',
		name: 'passwordFind',
		component: () => import('./pages/password-find/password-find.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/password/reset',
		name: 'passwordReset',
		component: () => import('./pages/password-reset/password-reset.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/infomation/improve',
		name: 'infomationImprove',
		component: () => import('./pages/infomation-improve/infomation-improve.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/customer/auth',
		name: 'customerAuth',
		component: () => import('./pages/customer-auth/customer-auth.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/profile',
		name: 'profile',
		component: () => import('./pages/profile/profile.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/terms',
		name: 'terms',
		component: () => import('./pages/terms/terms.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/privacy',
		name: 'privacy',
		component: () => import('./pages/privacy/privacy.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/personal/data/collection',
		name: 'personalDataCollection',
		component: () => import('./pages/personal-data-collection/personal-data-collection.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/protocol',
		name: 'protocol',
		component: () => import('./pages/protocol/protocol.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/news/list',
		name: 'newsList',
		component: () => import('./pages/news-list/news-list.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/news/detail',
		name: 'newsDetail',
		component: () => import('./pages/news-detail/news-detail.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},

	{
		path: '/product',
		name: 'product',
		component: () => import('./pages/product/product.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/custom/service',
		name: 'customService',
		component: () => import('./pages/custom-service/custom-service.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/custom/product',
		name: 'customProduct',
		component: () => import('./pages/custom-product/custom-product.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},

	{
		path: '/demo',
		name: 'demo',
		component: () => import('./pages/demo/index.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/about',
		name: 'about',
		component: () => import('./pages/About.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	{
		path: '/empty',
		name: 'empty',
		component: () => import('./pages/empty/empty.vue'),
		meta: {
			title: '平安人寿香港'
		}
	},
	// 404页面
	// {
	// 	path: '*', // * 表示上面路径匹配不到的都显示这个页面
	// 	name: '404',
	// 	component: Index
	// },
]

// add route path
routes.forEach(route => {
	route.path = route.path || '/' + (route.name || '');
});

const router = new Router({
	routes,
	// mode: 'history',
});


router.beforeEach((to, from, next) => {
	let langStr = localStorage.getItem("lang") || 'tc';
	let lang;
	// switch
	switch (langStr) {
		// 简体
		case "zh":
			lang = zh;
			break;

		// 英文
		case "en":
			lang = en;
			break;

		// 繁体
		default:
			lang = tc;
			break;
	}

	// lang对象为当前语言配置 见 en.js,tc.js,zh.js
	let title = "";
	lang.nav.navList.forEach(e1 => {
		if (e1.list && e1.list.length > 0) {
			e1.list.forEach(e2 => {
				if(e2.path == to.fullPath){
					title = e2.name;
				}
			});
		} else {
			if(e1.path == to.fullPath){
				title = e1.name;
			}
		}
	});
	// to参数可以获取要前往页面的信息,包括参数
	title = title || to.meta && to.meta.title;
	if (title) {
		document.title = title;
	}
	// const scrollTopList = [
	// 	"newsList", "newsDetail", "protocol", "privacy", "terms", "paymentType", "reservation"
	// ]
	document.documentElement.scrollTop = 0;
	document.body.scrollTop = 0;
	next();
});

export default router;