利用路由,尤其是子路由是在 Vue 中实现通用布局的好方法。
所有这些代码都使用 Vue 2.x
首先有一个非常简单的名为 App 的 vue 组件,它没有布局。
应用程序.vue
<template>
<router-view></router-view>
</template>
然后有一个 Routes 文件,你将把它带入你的 Vue 实例。
路线。(ts|js)
import Vue from 'vue'
import VueRouter from 'vue-router'
const NotFoundComponent = () => import('./components/global/notfound.vue')
const Login = () => import('./components/account/login.vue')
const Catalog = () => import('./components/catalog/catalog.vue')
export default new VueRouter({
mode: 'history',
linkActiveClass: 'is-active',
routes: [
//Account
{ path: '/account', component: () => import('./components/account/layout.vue'),
children: [
{ path: '', component: Login },
{ path: 'login', component: Login, alias: '/login' },
{ path: 'logout',
beforeEnter (to: any, from: any, next: any) {
//do logout logic
next('/');
}
},
{ path: 'register', component: () => import('./components/account/register.vue') }
]
},
//Catalog (last because want NotFound to use catalog's layout)
{ path: '/', component: () => import('./components/catalog/layout.vue'),
children: [
{ path: '', component: Catalog },
{ path: 'catalog', component: Catalog },
{ path: 'category/:id', component: () => import('./components/catalog/category.vue') },
{ path: 'product', component: () => import('./components/catalog/product.vue') },
{ path: 'search', component: () => import(`./components/catalog/search.vue`)} ,
{ path: 'basket', component: () => import(`./components/catalog/basket.vue`)} ,
{ path: '*', component: NotFoundComponent }
]
}
]
})
该代码使用延迟加载(使用 webpack),所以不要让() => import(...)你扔。import(...)如果您想要急切加载,它可能只是。
重要的是儿童路线。所以我们设置了主要路径/account来使用,/components/account/layout.vue但是前两个孩子指定了主要内容 vue(登录)。我选择这样做是因为如果有人只是浏览到 /account 我想用登录屏幕问候他们。/account 可能适合您的应用程序作为登录页面,他们可以在其中检查订单历史记录、更改密码等...
我对目录做了同样的事情....../并且/catalog都加载catalog/layout了/catalog/catalog文件。
另请注意,如果您不喜欢拥有“子文件夹”(即帐户/登录名而不仅仅是/登录名)的想法,那么您可以使用我在登录名中显示的别名。
通过添加, alias: '/login'它意味着用户可以浏览到/login即使实际路线是/account/login.
这是整个事情的关键,但只是为了尝试使示例完整......
这是我的启动文件,它连接了我的 app.vue 和路由:
启动。(ts|js)
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import App from './components/app.vue';
import router from './routes';
new Vue({
el: '#app',
router,
render: h => h(App)
});
我为我的应用程序的每个主要部分(帐户、目录等)创建了一个 layout.vue 文件。
帐户/layout.vue
<template>
<div>
<cc-header></cc-header>
<div class="container">
<main>
<router-view></router-view>
</main>
<aside>
</aside>
</div>
<cc-footer></cc-footer>
</div>
</template>
<script lang="ts">
import ccHeader from "../common/cc-header.vue"
import ccFooter from "../common/cc-footer.vue"
export default {
components: {
ccHeader,
ccFooter
}
}
</script>
<style lang="scss" scoped>
.container {
display: flex;
}
main {
flex: 3;
order: 2;
}
aside {
flex: 1;
order: 1;
}
</style>
和目录的布局......
目录/layout.vue
<template>
<div>
<cc-header></cc-header>
<div class="catalog-container">
<main class="catalog">
<router-view></router-view>
</main>
<cc-categories></cc-categories>
</div>
<cc-footer></cc-footer>
</div>
</template>
<script lang="ts">
import ccHeader from "../common/cc-header.vue"
import ccFooter from "../common/cc-footer.vue"
import ccCategories from "./cc-categories.vue"
export default {
components: {
ccCategories,
ccHeader,
ccFooter
},
data : function() : any {
return {
search: ''
}
},
}
</script>
<style lang="scss" scoped>
.catalog-container {
display: flex;
}
.category-nav {
flex: 1;
order: 1;
}
.catalog {
flex: 3;
order: 2;
}
</style>
两种布局都使用常见的组件,如页眉和页脚,但它们不需要。目录布局在侧导航中有类别,而帐户布局没有。我把我的常用组件放在 components/common 下。
常见/页脚.vue
<template>
<div>
<hr />
<footer>
<div class="footer-copyright">
<div>© Copyright {{year}} GlobalCove Technologies, LLC</div>
<div>All rights reserved. Powered by CoveCommerce.</div>
</div>
</footer>
</div>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.component('cc-footer', {
data : function() : any {
return {
year: new Date().getFullYear()
}
},
})
</script>
<style lang="scss">
</style>
整体文件结构
src/
boot.ts
routes.ts
components/
app.vue
catalog/
layout.vue
catalog.vue
category.vue
product.vue
search.vue
basket.vue
account/
layout.vue
login.vue
register.vue
global/
notfound.vue
common/
cc-header.vue
cc-footer.vue
路由、简单的 app.vue 和特定布局文件的组合以及通用组件应该可以让您到达您想去的地方。