Vue Router 2.6 Navigation Issue, cannot navigate back (browser button)

I’m working on a Vue.js (v. 2.6) application that uses Vue Router for navigation between different pages. After logging in, the user is redirected to the home page with the following code:

this.$router.push(‘/app’);

The issue is that after navigating to other pages within the app (by clicking buttons that load other routes), the user is unable to use the browser’s back button to return to the previous page or the home page. Route changes but the view doesn’t. It seems like the browser history isn’t being handled correctly. In my browser first I typed https://example.com, and everything works fine but example.com/view/#/something doesn’t. I’m working with hash mode

What I’ve tried:

  • I’ve confirmed that this.$router.push correctly adds a new entry to the browser’s history.

  • I used this.$router.replace in some cases to avoid adding multiple entries to the history, but the behavior remains the same.

  • I have 0 errors in the console

Problem:
When I navigate between different pages in the app (e.g., /app/dashboard, /app/events), the routing works fine. However, if I try to use the browser’s back button to go back to the previous page, the navigation doesn’t work as expected, and the app doesn’t go back to the home page or the previously visited route.

  • My main.js is configured as follows:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const router = new VueRouter({
mode: 'hash',
base: ROUTERPATH,
routes: [{
path: '/',
name: 'Login',
component: Login
},
{
path: '/loginusers',
name: 'LoginUsers',
component: LoginUsers
},
{
path: '/user',
name: 'User',
component: MenuUser,
children: [{
path: '/',
name: 'DashBoardUser',
component: DashBoardUser
},
{
path: ':section(dashboard|events)',
name: 'DashBoardUserSection',
component: DashBoardUser,
props: true
},
]
},
{
path: '/app',
name: 'App',
component: Menu,
children: [{
path: '/',
name: 'Dashboard',
component: Dashboard
},
{
path: 'logout',
name: 'Logout',
component: Logout
},
]
},
]
})
</code>
<code>const router = new VueRouter({ mode: 'hash', base: ROUTERPATH, routes: [{ path: '/', name: 'Login', component: Login }, { path: '/loginusers', name: 'LoginUsers', component: LoginUsers }, { path: '/user', name: 'User', component: MenuUser, children: [{ path: '/', name: 'DashBoardUser', component: DashBoardUser }, { path: ':section(dashboard|events)', name: 'DashBoardUserSection', component: DashBoardUser, props: true }, ] }, { path: '/app', name: 'App', component: Menu, children: [{ path: '/', name: 'Dashboard', component: Dashboard }, { path: 'logout', name: 'Logout', component: Logout }, ] }, ] }) </code>
const router = new VueRouter({
    mode: 'hash',
    base: ROUTERPATH,
    routes: [{
            path: '/',
            name: 'Login',
            component: Login
        },
        {
            path: '/loginusers',
            name: 'LoginUsers',
            component: LoginUsers
        },
        {
            path: '/user',
            name: 'User',
            component: MenuUser, 
            children: [{
                    path: '/',
                    name: 'DashBoardUser',
                    component: DashBoardUser
                },
                {
                    path: ':section(dashboard|events)',
                    name: 'DashBoardUserSection',
                    component: DashBoardUser,
                    props: true
                },
            ]
        },
        {
            path: '/app',
            name: 'App',
            component: Menu,
            children: [{
                    path: '/',
                    name: 'Dashboard',
                    component: Dashboard
                },
                {
                    path: 'logout',
                    name: 'Logout',
                    component: Logout
                },
            ]
        },
    ]
    })
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>router.beforeEach((to, from, next) => {
if (to.path !== '/' &&
!to.path.startsWith('/loginusers')
) {
var isUser = false;
if (to.path.startsWith('/ute/') || to.path == '/ute') {
isUser = true;
}
if ((localStorage.getItem('token') || '') === '') {
(isUser ? router.push('/loginusers') : router.push('/'))
//router.push('/')
} else {
var token = localStorage.getItem('token')
axios.get(NAMESPACE + 'rest/logged', {
headers: {
'Content-Type': 'application/json',
'Authorization': token
}
})
.then(response => {
// JSON responses are automatically parsed.
try {
if (!response.data.error) {
if (response.data.token != null) {
localStorage.setItem('token', response.data.token)
next()
} else {
(isUser ? router.push('/loginusers') : router.push('/'))
}
} else {
// not logged
(isUser ? router.push('/loginusers') : router.push('/'))
}
} catch (e) {
(isUser ? router.push('/loginusers') : router.push('/'))
}
})
.catch(e => {
(isUser ? router.push('/loginusers') : router.push('/'))
e
})
}
} else {
next()
}
})
--> So, you go to example.com, you login, homepage, it works. You go to https://example.com/view/#/something, you login, homepage, click on a button, other page, route change, view change, you want to go back with arrows, you cannot (only route change)
I'm looking for a solution that ensures proper browser history handling using hash mode.
Thanks in advance for any help!`
</code>
<code>router.beforeEach((to, from, next) => { if (to.path !== '/' && !to.path.startsWith('/loginusers') ) { var isUser = false; if (to.path.startsWith('/ute/') || to.path == '/ute') { isUser = true; } if ((localStorage.getItem('token') || '') === '') { (isUser ? router.push('/loginusers') : router.push('/')) //router.push('/') } else { var token = localStorage.getItem('token') axios.get(NAMESPACE + 'rest/logged', { headers: { 'Content-Type': 'application/json', 'Authorization': token } }) .then(response => { // JSON responses are automatically parsed. try { if (!response.data.error) { if (response.data.token != null) { localStorage.setItem('token', response.data.token) next() } else { (isUser ? router.push('/loginusers') : router.push('/')) } } else { // not logged (isUser ? router.push('/loginusers') : router.push('/')) } } catch (e) { (isUser ? router.push('/loginusers') : router.push('/')) } }) .catch(e => { (isUser ? router.push('/loginusers') : router.push('/')) e }) } } else { next() } }) --> So, you go to example.com, you login, homepage, it works. You go to https://example.com/view/#/something, you login, homepage, click on a button, other page, route change, view change, you want to go back with arrows, you cannot (only route change) I'm looking for a solution that ensures proper browser history handling using hash mode. Thanks in advance for any help!` </code>
router.beforeEach((to, from, next) => {
    if (to.path !== '/' &&
        !to.path.startsWith('/loginusers')
    ) {

        var isUser = false;
        if (to.path.startsWith('/ute/') || to.path == '/ute') {
            isUser = true;
        }

        if ((localStorage.getItem('token') || '') === '') {
            (isUser ? router.push('/loginusers') : router.push('/'))
            //router.push('/')
        } else {

            var token = localStorage.getItem('token')
            axios.get(NAMESPACE + 'rest/logged', {
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': token
                    }
                })
                .then(response => {
                    // JSON responses are automatically parsed.
                    try {
                        if (!response.data.error) {
                            if (response.data.token != null) {
                                localStorage.setItem('token', response.data.token)
                                next()
                            } else {
                                (isUser ? router.push('/loginusers') : router.push('/'))
                            }
                        } else {
                            // not logged
                            (isUser ? router.push('/loginusers') : router.push('/'))
                        }
                    } catch (e) {
                        (isUser ? router.push('/loginusers') : router.push('/'))
                    }
                })
                .catch(e => {
                    (isUser ? router.push('/loginusers') : router.push('/'))
                    e
                })
        }
    } else {
        next()
    }
})


--> So, you go to example.com, you login, homepage, it works. You go to https://example.com/view/#/something, you login, homepage, click on a button, other page, route change, view change, you want to go back with arrows, you cannot (only route change)

I'm looking for a solution that ensures proper browser history handling using hash mode.

Thanks in advance for any help!`

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật