import Vue from 'vue'
import VueRouter from 'vue-router'
// import Recommend from "views/recommend/Recommend.vue"

Vue.use(VueRouter)

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
	return VueRouterPush.call(this, to).catch(err => err)
}
//标题js
import getPageTitle from "@/utils/title-config";


const Index = () => import('views/index/index')
const Recommend = () => import('views/index/recommend/Recommend')
const Collect = () => import('views/index/collect/Collect')
const AllSections = () => import('views/index/allSections/AllSections')
const search = () => import('views/search/search')
const SearchResult = () => import('views/search/searchResult/SearchResult')

const routes = [
	{
		// 首页
		path: '/',
		name: 'Index',
		redirect: "/recommend",
		component: Index,
		children: [
			{
				path: '/recommend',
				name: 'Recommend',
				component: Recommend,
				meta: {
					title: "推荐版块"
				}
			},
			{
				path: '/collect',
				name: 'Collect',
				component: Collect,
				meta: {
					title: "收藏的版块"
				}
			},
			{
				path: '/allSections',
				name: 'AllSections',
				component: AllSections,
				meta: {
					title: "全部版块"
				}
			},

		]
	}, {
		path: '/searchResult',   // 搜索结果
		name: 'search',
		redirect: "/searchResult",
		component: search,
		children: [
			{
				path: '/searchResult',
				name: 'Recommend',
				component: SearchResult,
				meta: {
					title: "搜索帖子"
				}
			},
		]
	}

]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})
router.beforeEach(async (to, from, next) => {
	if (to.meta.title) document.title = getPageTitle(to.meta.title);

	next();
});

export default router