你好,我有两个模板:
Main.vue404.vue
如果用户进入正常页面,Main.vue则应使用该模板,每个页面都有嵌套路由。如果用户输入不存在的页面,404.vue则应使用有错误的模板。我尝试了以下操作,但它总是显示 404 错误,除了localhost:8080/(根路径访问):路由器.js:
export default new Router({
mode: 'history',
routes: [{
path: '/',
name: 'main',
component: Main,
childrens: [{
path: '/',
name: 'Home',
components: {
default: () =>
import ('@/views/Home'),
leftInfo: () =>
import ('@/views/DashboardAdditionalInfo'),
rightInfo: () =>
import ('@/components/common/MyTicketsList')
}
},
{
path: 'dashboard',
name: 'Dashboard',
components: {
default: () =>
import ('@/views/Dashboard'),
leftInfo: () =>
import ('@/views/DashboardAdditionalInfo'),
rightInfo: () =>
import ('@/components/common/MyTicketsList')
}
}
]
},
{
path: '*',
name: '404',
component: () =>
import ('@/templates/404.vue')
}
]
})
Run Code Online (Sandbox Code Playgroud)
关于如何处理这种情况有什么想法吗?当然,我可以将 404 句柄放在主路线内,但这会显示两个侧栏。
小智 5
您可以使用动态布局方法您可以使用shards ui vue 模板。它本质上将您的应用程序级别包装<router-view/>在正确的布局组件中,具体取决于您在router.js.
应用程序.vue:
<template>
<div id="app">
<component :is="layout">
<router-view/>
</component>
</div>
</template>
<script>
export default {
name: 'app',
computed: {
layout() {
// none-layout will be used if the meta.layout tag is not set
// computed may not be best place in vue lifecycle for this but it works ok
return `${this.$route.meta.layout || 'none'}-layout`;
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
路由器.js:
import Vue from "vue";
import Router from "vue-router";
import Errors from "./views/Errors.vue";
Vue.use(Router);
export default new Router({
mode: 'history',
routes: [
{
path: "/",
name: "dash",
component: () => import('./views/Dashboard.vue'),
meta: { layout: 'default-dash' }
},
{
path: '/signup',
component: () => import('./views/Signup.vue'),
// meta.layout tag not present so 'none' will be used
},
{
path: '*',
component: Errors,
meta: { layout: 'none' }
}
]
});
Run Code Online (Sandbox Code Playgroud)
main.js:
import Vue from 'vue'
import App from './App.vue'
import router from "./router";
// Layouts
import DefaultDash from '@/layouts/DefaultDash.vue';
import None from '@/layouts/None.vue';
// Layouts as usable components
Vue.component('default-dash-layout', DefaultDash);
Vue.component('none-layout', None);
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App),
}).$mount('#app')
Run Code Online (Sandbox Code Playgroud)
默认Dash.vue:
<template>
<div>
<NavBar/>
<SideBar/>
<slot/>
</div>
</template>
<script>
import NavBar from "../components/NavBar";
import SideBar from "../components/SideBar";
export default {
name: "default-dash",
components: { NavBar, SideBar }
};
</script>
Run Code Online (Sandbox Code Playgroud)
无.vue:
<template>
<div>
<slot/>
</div>
</template>
<script>
export default {
name: "none"
};
</script>
Run Code Online (Sandbox Code Playgroud)
现在你的 vue 404 错误将在没有任何侧边栏或导航栏的情况下加载。您可以将所有主要内容页面封装在您想要的任何外部布局中,您所要做的就是指定在元标记中使用哪个模板。
| 归档时间: |
|
| 查看次数: |
5696 次 |
| 最近记录: |