Vue 3 Go home on refresh - vuejs3

I have a vuetify (vue 3) app. When I (hard) refresh my web app, I'd like to be redirected to my very first route (for isntance, Skills). Instead I go to the last route I was on.
Here are my routes.
const routes = [
{
path: '/',
name: "Skills",
component: Skills,
beforeEnter: checkIfAuthenticated,
},
{
path: '/myskills',
name: "MySkills",
component: MySkills,
beforeEnter: checkIfAuthenticated,
},
{
path: '/history',
name: "History",
component: History,
beforeEnter: checkIfAuthenticated,
},
{
path: '/conversations',
name: "Conversations",
component: Conversations,
beforeEnter: checkIfAuthenticated,
},
{
path: '/:catchAll(.*)*',
name: "PageNotFound",
component: PageNotFound,
},
]
const router = createRouter({
history: createWebHistory(),
routes,
})
Thanks

Related

vue-router doesn't get active class when navigating to routes

this is my router
const router = createRouter({
linkActiveClass: "active",
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: DefaultLayout,
children: [
{
path: 'tucs',
name: 'tucs',
component: () => import('../views/tuc/index.vue'),
},
{
path: 'tucs/:id',
name: 'tuc',
component: () => import('../views/tuc/_id.vue'),
},
]
},
]
})
and this is navigation bar
<div class="side-container">
<router-link to="/tucs">TUC</router-link>
<router-link to="/users">Users</router-link>
</div>
when I am in '/tuc' rout router link get both active and exact-active classes But when I navigate to '/tuc/1' it doesn't get active class
what's the problem ?

Angular 13 i18n switches to en-US when I try to access some of the routes in the other language

I hosted my angular 13 website on Github Pages and after I added i18n I have the following routing problem.
When I go to https://MyRepoName.github.io/base/en-US/ everything is fine and I get redirected to https://MyRepoName.github.io/base/en-US/home as it should.
But when I want to access some other route for example:
https://MyRepoName.github.io/base/en-US/welcome then I got redirected to https://MyRepoName.github.io/base/home
When I go to https://MyRepoName.github.io/base/welcome then everything is fine.
I have this code for routes:
const routes: Routes = [
{
path: 'welcome',
component: WelcomeComponent
},
{
path: 'home',
component: HomeComponent
},
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: '**', redirectTo: 'home', }
];
If someone had the same issue pls help.
I have 1 index/404 file with <base href="/base/">
and folder en-US with index/404 file <base href="/base/en-US">
I also receive this info when I access: https://MyRepoName.github.io/base/welcome
Router Event: Od
NavigationStart(id: 1, url: '/en-US/welcome')
Od {id: 1, url: '/en-US/welcome', navigationTrigger: 'imperative', restoredState: null}
Router Event: qR
RoutesRecognized(id: 1, url: '/en-US/welcome', urlAfterRedirects: '/home', state:Route(url:'', path:'') { Route(url:'home', path:'home') } )
qR {id: 1, url: '/en-US/welcome', urlAfterRedirects: '/home', state: ob}

Unknown vue router redirection

http://localhost:8080/#/ this is the local host port that i opened but when i logged in it redirects to http://localhost:8080/?#/ . Then i have to logged in again to have my router go to http://localhost:8080/?#/admin
what is the significance of the question mark
I am using firebase authentication
here is my router index
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Admin from "../views/Admin.vue";
import ManageUsers from "../views/ManageUsers.vue";
import Funds from "../views/Funds.vue";
import CompleteDetails from "../views/CompleteDetails.vue";
import UserFunds from "../views/UserFunds.vue";
import ViewPost from "../views/ViewPost.vue";
import ResetPassword from "../views/ResetPassword.vue";
import Profile from "../views/Profile.vue";
import Overview from "../views/Overview.vue";
import Users from "../views/Users.vue";
import { fb } from "../firebase.js";
import AllUsers from "../views/Allusers.vue"
//User
import User from "../views/User.vue";
import MyProfile from "../views/MyProfile.vue";
import UserDashboard from "../views/Userdashboard.vue";
import Posts from "../views/Posts.vue";
import postEdit from "../views/postEdit.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/admin",
name: "admin",
component: Admin,
meta: { requiresAuth: true },
children: [
{
path: "funds/:user",
name: "funds",
component: Funds,
},
{
path: "userfunds",
name: "userfunds",
component: UserFunds,
},
{
path: "completedetails/:user",
name: "completedetails",
component: CompleteDetails,
},
{
path: "overview",
name: "overview",
component: Overview,
},
{
path: "posts",
name: "posts",
component: Posts,
},
{
path: "postEdit/:postId",
name: "postEdit",
component: postEdit,
},
{
path: "ViewPost/:postId",
name: "ViewPost",
component: ViewPost,
},
{
path: "users",
name: "users",
component: Users,
},
{
path: "allusers",
name: "allusers",
component: AllUsers,
},
{
path: "profile/:user",
name: "profile",
component: Profile,
},
{
path: "manageusers",
name: "manageusers",
component: ManageUsers,
},
],
},
{
path: "/user",
name: "user",
component: User,
meta: { requiresAuth: true, requiresType: true },
children: [
{
path: "myprofile/:user",
name: "myprofile",
component: MyProfile,
},
{
path: "posts",
name: "postsUser",
component: Posts,
},
{
path: "ViewPost/:postId",
name: "ViewPostUser",
component: ViewPost,
},
{
path: "userfunds",
name: "userfundsUser",
component: UserFunds,
},
{
path: "userDashboard",
name: "UserDashboard",
component: UserDashboard,
},
],
},
{
path: "/",
name: "Home",
component: Home,
},
{
path: "/resetpassword",
name: "ResetPassword",
component: ResetPassword,
},
{
path: "/about",
name: "About",
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ "../views/About.vue"),
},
];
const router = new VueRouter({
routes,
});
router.beforeEach((to, from, next) => {
const requiresAuth = to.matched.some((x) => x.meta.requiresAuth);
const currentUser = fb.auth().currentUser;
if (requiresAuth && !currentUser) {
next("overview");
} else if (requiresAuth && currentUser) {
next();
} else {
next();
}
});
export default router;

Vue Routes URL: username/pagename

I create a website template with mock data by using vue & vuetify.
I have different pages like:
localhost:8080/home
localhost:8080/exp
localhost:8080/skills
localhost:8080/about
I would like to use this template for each user and change the structure to this:
localhost:8080/username/home
localhost:8080/username/exp
localhost:8080/username/skills
localhost:8080/username/home
I don't know whether this is the best practice and also I don't know how to do it.
I think that I will use firebase to store and get user related data. Also I am open for any suggestions.
When I go localhost:8080/user/1 I can see everything in User.vue however when I go localhost:8080/user/1/home nothing is rendered. (localhost:8080/home works. I can see everything inside User.vue & Home.vue as well.
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/user/:id',
component: User,
children: [
{
path: '/home',
name: 'home',
component: Home
},
{
path: '/exp',
name: 'exp',
component: Exp
},
{
path: '/skills',
name: 'skills',
component: Skills
},
{
path: '/about',
name: 'about',
component: About
}
]
}
]
})
User.Vue
<template>
<div>
user
<div>This is Bar {{ $route.params.id }}</div>
{{personal.name}}
<router-link :to="{ name: 'personal' }">Personal Info</router-link>
<router-view></router-view>
</div>
</template>
You need to do this in the router. like this:
{
path: 'username',
meta: { label: 'Username'},
component: {
render (c) { return c('router-view') }
},
children: [
{
path: 'home',
name: 'Home',
component: Home
},
{
path: 'exp',
name: 'Exp',
component: Cards
},
{
path: 'about',
name: 'About',
component: About
},
{
path: 'other',
name: 'Other',
component: Other
}
]
}
So u add childs to your path.. this makes the url look like /username/exp
The main problem was using ' / home' in the path. This is why it goes localhost:8080/home instead of localhost:8080/user/1/home
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/user/:id',
component: User,
children: [
{
path: 'home',
name: 'home',
component: Home
},
{
path: 'exp',
name: 'exp',
component: Exp
},
{
path: 'skills',
name: 'skills',
component: Skills
},
{
path: 'about',
name: 'about',
component: About
}
]
}
]
})
User.Vue
<template>
<div>
user
<div>This is Bar {{ $route.params.id }}</div>
{{personal.name}}
<router-link :to="{ name: 'personal' }">Personal Info</router-link>
<router-view></router-view>
</div>
</template>

Angular 2 RC5 router - nested routing

Structure
app |- admin
|- users
|- userList.Component.ts
|- userDetails.Component.ts
|- admin.component.html (contains 'router-oulet')
|- admin.component.ts
|- admin.routes.ts
|- admin.module.ts
|- dogs
|- app.component.html (contains 'router-oulet')
|- app.component.ts
|- app.routes.ts
|- app.module.ts
app.routes.ts
const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'dogs', component: DogListComponent, canActivate: [AuthGuard] },
{ path: 'dog/:id', component: DogDetailsComponent, canActivate: [AuthGuard]},
];
export const routing = RouterModule.forRoot(appRoutes);
admin.routes.ts
const adminRoutes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AdminGuard],
children: [
{ path: '', redirectTo: '/admin/users' },
{ path: 'users', component: AdminUserListComponent },
{ path: 'user/:id', component: AdminUserDetailsComponent },
]
},
];
export const adminRouting = RouterModule.forChild(adminRoutes);
admin.userList.Component.ts
this._router.navigate(['/admin/user/', id]);
trying to navigate from AdminUserList ('admin/users') to AdminUserDetails ('admin/user/someId') but with no success.
(it seems like the scroller is going further down whenever i try to navigate to the user details)
any solution ?
you need to add pathMatch: 'full' in admin routes for default redirect,
admin.routes.ts
const adminRoutes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AdminGuard],
children: [
{ path: '', redirectTo: '/admin/users', pathMatch: 'full' },
{ path: 'users', component: AdminUserListComponent },
{ path: 'user/:id', component: AdminUserDetailsComponent },
]
},
];
export const adminRouting = RouterModule.forChild(adminRoutes);
Update
To navigate back you have couple of ways,
// In the HTML
<a [routerLink]="['/admin']" >Back</a> or <a [routerLink]="['/admin/users']" >Back</a>
// Or create a back function and use in click binding in HTML
goBack = () => {
this.router.navigate(['/admin']);
or this.router.navigate(['/admin/users']);
}

Resources