Vuejs route based on payload - javascript

Im using vuerouter and want to route to a component based on payload.
{
path: '/foobar',
name: 'foobar',
component: foobar,
}
{
path: '/foobar',
name: 'foobarSuccess',
component: foobarSuccess,
query: { status: 'success' }
}
Example above, first route is step one. We do some stuff there then we go to another page and get redirected back with the payload status=succcess. How do i use the query object to route to the correct component?

You have to define your router with parameters
{
path: '/foobar',
name: 'foobar',
component: foobar,
}
{
path: '/foobar/:status',
name: 'foobarSuccess',
component: foobarSuccess,
query: { status: 'success' }
}
here status will contain like success, finally /foobar/success
based on this parameter also you can display the sccuess message
For more information you can refer the simple docs https://router.vuejs.org/en/essentials/dynamic-matching.html ,

Related

Routing with if condition in vue.js

This is my code within in router.js file
{
name: "Admin",
path: "/admin/",
component: () => import("#/views/admin/Index"),
children: [
// Dashboard
{
name: "Dashboard",
path: "dash",
component: () => import("#/views/admin/Dash"),
},
{
name: "Campaign Management",
path: "campaign",
component: () =>
import("#/views/CampaignManagementNew/CampaignManagment"),
},
]
}
I set a variable 'status' in the localStorage.
I want to route only, if 'status' is true (localStorage item).
Otherwise, I not need to routing to child components.
How I use if conditions to routing?
You want to implement a pretty common pattern for 'protecting' routes behind a flag. In order to accomplish this, you should add a meta property to the routes you want to 'protect' behind a secondary check (the localStore value). Then you will need to create a router guard that checks the meta flag and routes the user based on those conditions.
You can see an example of it here: https://router.vuejs.org/guide/advanced/meta.html#route-meta-fields
In your case you would want to replace the auth.loggedIn() with your function that can check the localStorage value. So something like this:
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresLocalStorageValue)) {
if (!checkLocalStorageValue()) {
next({ path: '/not-authorized-page-missing-local-store'})
} else {
next()
}
} else {
next() // make sure to always call next()!
}
})

Redirect to 404 page without modifying the URL in Vue.js

In my Vue.js project, I want to display my 404 page, if a route parameter is invalid. For now, I'm using the following code for that:
this.$router.replace({ path: '/404' });
Is there a way to do that without modifying the URL? I want the user to still be able to copy the browser's original URL line. Is there some kind of a silent: true parameter?
With vue-router, the URL is the source of truth. If the URL changes, so does the rendering. You can't "pause" the router. (This is a flaw in vue-router that has been bugging me for ages, but I digress.)
You just have to display the 404 page without modifying the route. Have some display404 data property in your root component that you can set to display the 404 page manually in the template instead of the <router-view>, e.g:
<div>
<my-404-page v-if="display404"/>
<router-view v-else/>
</div>
To display the 404 page from any component:
this.$root.display404 = true
Of course this is just a basic example to demonstrate what I mean, you might want to use Vuex to share the state, or use an event bus, or you can display the 404 page in some other way that works for you, etc.
This was fixed in Vue Router 4 which you can see on the second example in the docs.
Build your NotFound route like this:
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: NotFound
},
Then you can use a beforeEnter navigation guard on your dynamic Vue like so:
// In your router/index.js file...
{
path: 'users/:id',
name: 'User Detail',
component: UserDetail,
beforeEnter(to, from) {
// See if that query exists in your data...
const exists = data.users.find(
user => user.id === parseInt(to.params.id)
)
if (!exists) {
// THE IMPORTANT PART
// Return your not found view...
return {
name: 'NotFound',
// Match the path of your current page and keep the same url...
params: { pathMatch: to.path.split('/').slice(1) },
// ...and the same query and hash.
query: to.query,
hash: to.hash,
}
}
}
}
Haven't tested this in a Component yet, but I'd assume it'd be the same logic in the beforeRouteEnter navigation guard.
Not 100% sure what you are asking, but is either of these any help?
A catch all route:
From Vue.js docs "Catch all route"
Or if you are managing a response form a call (method/fetch/ etc): Use a combination of try/catch and a "loading" data value to change the display or what component is loaded.
Based on Decade Moon's solution, I did the following:
main.js
import Error404 from './views/error/404.vue'
Vue.component('error-404', Error404)
404.vue
<template>
<div>
<h1>Page not found</h1>
<p>Whatever...</p>
</div>
</template>
<script>
export default {
name: 'Page not found'
}
</script>
router --> index.js
const PageNotFound = () => import('#/views/error/404')
function configRoutes() {
return [
{
path: '/',
name: 'Home',
component: TheContainer,
children: [
// ...
{
path: '404',
name: 'Page not found',
component: PageNotFound,
alias: '*'
}
]
}
]
}
My Page which should display the 404 error
<template>
<div class="animated fadeIn" v-if="clientSettings">
...
</div>
<error-404 v-else></error-404>
</template>
<script>
export default {
name: 'Test',
data() {
return {
clientSettings: null
};
},
async created() {
this.setClientConfig();
},
watch: {
'$route.params.id': function (id) { this.setClientConfig(id);}
},
methods: {
setClientConfig(id) {
if (!id) {
id = this.$route.params.id;
// Redirect to the first valid list, if no parameter is proviced
if (!id) {
this.$router.push({ name: 'Test', params: { id: this.$root.clientConfiguration[0].name } });
return;
}
}
// Set client settings
this.clientSettings = this.$root.clientConfiguration.find(cc => cc.name === id);
// This will return null, if no entry was found, therefore the template will jump into the v-else
}
}
}
</script>

How to redirect in Vue.js to different url when clicked?

I'm making a post board with Vue.js and trying to make the page redirected when each post is clicked.
I've installed vue-route and axios.
In index.js,
export default new Router({
route: [
{
path: '/',
name: 'Post',
component: Post
},
{
path: '/:req_no',
name: 'Detail',
component: Detail
},
]
})
In post.vue
<div #click="detailPost(post.no)">{{post.title}}</div>
.
.
.
detailPost(req_no) {
this.$router.push({
path: `https://dataURL/detail.php/${req_no}`
})
}
In Detail.vue
<template>
<div>
{{contents}}
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'Datail',
data() {
return {
req_no: this.$route.params.req_no,
contents: {}
}
},
created() {
axios.get('https://dataURL/detail.php/', {
params: {
req_no: this.req_no
}
}).then(res => {
this.contents = this.res.data
});
}
}
</script>
I'm not sure where to put the url (in the function in post.Vue, detailPost() or in Detail.vue)
If I put it in the function, I get
http://localhost:8080/#/http://dataURL/detail.php/2
The API guide says I must use the params.
Could you please help me where to fix? Thanks alot!!
You cannot use the router for a different domain.
See this answer: https://stackoverflow.com/a/41654493/146656
What you can do is simply use vanilla JS to do it:
window.location = `https://dataURL/detail.php/${req_no}`;
Is https://dataURL/detail.php on same domain with your main app?
Please try:
this.$router.push({
path: `/detail.php/${req_no}`
})
If it's different domain, you can use window.location
window.location = https://dataURL/detail.php/${req_no};

Load a VUE component with parameters

I have
<a role="button" v-on:click="setMemberName(item)">
And it calls a method:
methods:{
setMemberName(item) {
alert(item.email);
this.$router.push('about');
}
},
The alert gets fired and the router gets called but I need to send the parameter of item.email and I need to capture that when the 'about' vue gets loaded. I have a simple alert being called using:
,
mounted:function () {
alert("Hello");
},
But I would like it to say "Hello " then the email address like "Hello Smith#jmail.com". I really need the email address so I can call a webservice but Hello is fine for this problem. As you can tell VUE is new to me.
I have tried:
this.$router.push({ name: 'about', params: { itemEmail: item.email } })
but it seems that it never loads the 'about' vue. Thanks for the help.
OK-- edit-- It does get fired if I use the proper case 'About' instead or 'about' but I still need help on the capture side
Code for the about vue: a simple div and some script code:
<script>
export default {
name: 'About',
data() {
return {
}
},
methods:{
},
mounted:function () {
alert("Hello");
},
created(){
},
}
</script>
There are many ways of solving this issue, your are using route params so you need to define the param in the route:
routes: [
{ path: '/about/:email', component: About }
]
Then you can access the param in the About component
In the template:
<div>Email: {{ $route.params.email }}</div>
In the script:
sayHello() {
alert($route.params.email);
}
Note that you could also use route query params or route props, read the docs at: https://router.vuejs.org/guide/
If you update your push to (see docs for full options):
this.$router.push({ name: 'about', query: { itemEmail: item.email } })
You can access the query parameters (See docs here):
this.$router.query.itemEmail

Redirect error when using $router.push to component with props vue-router

I've got an issue with vue-router, trying to redirect from a composant to another with $router.push and props.
Here's the code :
this.$router.push({path: '/badges/search', query: {filter: item.tag_id}})
In the route definition :
{ path: '/badges',
name: 'Badges',
component: require('./components/pages/Badges.vue'),
beforeEnter: requireAuth,
children: [{
path: 'search',
name: 'Badge-id',
components: require('./components/pages/Badges.vue'),
props: (route) => ({filter: route.query.filter})
}]
}
And finally, here's the error I get :
vue-router.esm.js:1697 TypeError: Cannot read property '_c' of undefined
at render (http://localhost:8080/app.js:27823:91)
at http://localhost:8080/app.js:30450:17
at http://localhost:8080/app.js:30477:66
at Array.map (native)
at http://localhost:8080/app.js:30477:38
at Array.map (native)
at flatMapComponents (http://localhost:8080/app.js:30476:26)
at http://localhost:8080/app.js:30415:5
at iterator (http://localhost:8080/app.js:30220:7)
at step (http://localhost:8080/app.js:30103:9)
Does someone have an idea why I get this error ? Also, trying to log "this" object in the new component (Badges) in the created method, it has never been different of "undefined", even with a while loop (I got it infinite)
Thanks a lot for help !
use params for send the props:
this.$router.push({path: '/badges/search', params: {filter: item.tag_id}})

Categories