开发者问题收集

如何防止 Vue.js 中的子路由在页面刷新时丢失样式

2019-04-20
2466

我的网站路由工作正常,但是当我点击刷新按钮时出现问题。

在基本路由(例如 http://localhost:8080/employers )上,页面或组件样式保持不变,但是当我刷新子路由(例如 http://localhost:8080/employers/google )时,此组件的所有样式都将丢失。

如能提供任何有关如何解决此问题的帮助,我们将不胜感激

import Vue from 'vue'
import Router from 'vue-router' 
// import store from './store.js'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [        
        {
            path: '/',
            component: () => import('./views/Home.vue'),
            children: [
                {
                    path: "",
                    component: () => import("./views/HomePage.vue"),
                },
                {
                    path: '/jobs',
                    name: 'jobs',
                    component: () => import('./views/JobListings.vue')
                },
                {
                    path: '/job/:id',
                    name: 'job',
                    component: () => import('./views/JobDetails.vue')
                },
                {
                    path: '/login',
                    name: 'login',
                    component: () => import('./views/Login.vue')
                },
                {
                    path: '/register',
                    name: 'register',
                    component: () => import('./views/Register.vue')
                },
                {
                    path: '/forgotpassword',
                    name: 'forgotpassword',
                    component: () => import('./views/ForgotPassword.vue')
                },
                {
                    path: '/verify',
                    name: 'verify',
                    component: () => import('./views/Verify.vue')
                }, 
            ],
        },


        {
            path: '/employer',
            component: () => import('@/views/Employers.vue'),
            children: [
                {
                    path: '', 
                    component: () => import('./views/Employers/Profile.vue')                    
                },
                {
                    path: 'profile', 
                    component: () => import('./views/Employers/Profile.vue')
                },
                {
                    path: 'post',
                    component: () => import('./views/Employers/PostJob.vue')
                },
                {
                    path: 'listings', 
                    component: () => import('./views/Employers/Listings.vue')
                }, 
                {
                    path: 'settings', 
                    component: () => import('./views/Employers/Listings.vue')
                },
                {
                    path: 'editresume', 
                    component: () => import('./views/Employers/Listings.vue')
                },
                {
                    path: 'closeaccount', 
                    component: () => import('./views/Employers/Listings.vue')
                },
            ]
        },

        // jobseekers route
        {
            path: '/jobseeker', 
            component: () => import('@/views/Jobseekers/Home.vue'),
            children: [
                {
                    path: '', 
                    component: () => import('@/views/Jobseekers/Profile.vue')
                },
                {
                    path: 'resume', 
                    component: () => import('@/views/Jobseekers/Resume.vue')
                },
                {
                    path: 'profile', 
                    component: () => import('@/views/Jobseekers/Profile.vue')
                },
                {
                    path: 'settings', 
                    component: () => import('@/views/Jobseekers/Settings.vue')
                },
                {
                    path: 'applications', 
                    component: () => import('@/views/Jobseekers/Applications.vue')
                },                
                {
                    path: 'close', 
                    component: () => import('@/views/Jobseekers/Close.vue')
                },
            ]
        },

        {
            path: '/jobseeker/:page',
            component: () => import('@/views/Jobseekers/Profile.vue'),
        },

        {
            path: '/search/:region/:keyword',
            component: () => import('./views/JobListings.vue')
        },

        // not found route
        {
            path: '*',
            name: '404',
            component: () => import('./views/404.vue')
        }
    ]
})

export default router
3个回答

问题不在于您的路线 ,而在于您如何编写 CSS。

  1. 我建议在组件样式中使用范围样式(只有此组件将使用这些样式)。

  2. 如果多个组件要共享样式,您可以单独使用 CSS 文件。

arora
2019-04-20

我注意到您正在按需加载组件。

当您从 /employers 导航到 /employers/google 路由时, /employers 路由中的一些 CSS 样式正在您的 /employers/google 路由中重复使用。 因此,当您重新加载 http://localhost:8080/employers/google 路由时,您无法从 /employers 获取样式,这会导致 CSS 中断。

我的建议是将通用样式移动到一个特定文件中,并将其导入到主文件(如 App.vue )中,以便无论重新加载哪个组件,都会加载它们。

Arthur
2019-04-25

我也遇到了同样的问题,但我已经全局包含了 CSS,但它不起作用, 最后,我尝试将路由器模式从历史记录更改为哈希值。 在我的情况下尝试它工作正常。

const router = new Router({
  mode: "hash",
  base: process.env.BASE_URL,
  routes: []
})
Fikremariam Asmro
2020-10-26