Vuejs - router children - vue.js

I'm trying to figure out how child routes in VueJS work. I would think that if I had a news overview with links to each news item, I could then use a child route to view the news item, but it doesn't works as I expects.
Is it me that is doing it wrong or?
const router = new VueRouter({
routes: [
{
path: '/news',
name: 'news',
component: News,
children: [
{
path: ':id',
name: 'newsitem',
component: Newsitem
}
]
}
]
});
I have created a jsfiddle to show how I would expect it to work.
If I uncomment the router in the javascript, then it works fine, but not with children.

Like Moersing.Lin said you forgot to put a <router-view> in your News Component.
Here is an working example of your fiddle:
const News = {
template: `<div>
<h1>News</h1>
<br><br>
<router-view></router-view>
</div>
`
}
const Newsitem = {
template: "<h2>News {{this.$route.params.id}}</h2>"
}
const router = new VueRouter({
routes: [{
path: '/news',
name: 'news',
component: News,
children: [{
path: ':id',
name: 'newsitem',
component: Newsitem,
}]
}]
});
new Vue({
el: '#app',
router,
}).mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<ul>
<li>
<router-link :to="{ name: 'news'}">News list</router-link>
</li>
<li>
<router-link :to="{ name: 'newsitem', params: { id: 'item-1' }}">Item 1</router-link>
</li>
<li>
<router-link :to="{ name: 'newsitem', params: { id: 'item-2' }}">Item 2</router-link>
</li>
</ul>
<router-view></router-view>
</div>

I got it, A Vue Router is required <router-view></router-view>,but in your code, The root component is there, but you forgot the parent,it needs a <router-view></router-view> too.
https://jsfiddle.net/v28yw3g5/

const News = {
template: `<div>
<h1>News</h1>
<br><br>
<router-view></router-view>
</div>
`
}
const Newsitem = {
template: "<h2>News {{this.$route.params.id}}</h2>"
}
const router = new VueRouter({
routes: [{
path: '/news',
name: 'news',
component: News,
children: [{
path: ':id',
name: 'newsitem',
component: Newsitem,
}]
}]
});
new Vue({
el: '#app',
router,
}).mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<ul>
<li>
<router-link :to="{ name: 'news'}">News list</router-link>
</li>
<li>
<router-link :to="{ name: 'newsitem', params: { id: 'item-1' }}">Item 1</router-link>
</li>
<li>
<router-link :to="{ name: 'newsitem', params: { id: 'item-2' }}">Item 2</router-link>
</li>
</ul>
<router-view></router-view>
</div>

Related

Prevent transition from child component to parent component with vue

I'm doing Router with Vue Js, but the thing I want to do is:
Sample:
domain.com/post/1
while the page is open,
again
I don't want it to switch to /post via the menu
Is there a way to prevent this
import Home from "./website/Home.vue";
import Post from "./website/Post.vue";
import PostDetail from "./website/PostDetail.vue";
export default [
{
path: '',
component: Home,
name: 'home',
},
{
path: 'post',
component: Post,
name: 'post',
children: [
{
path: ':id',
name: 'PostDetail',
component: PostDetail,
props: true
}
]
},
]
app.vue
<div>
<router-link to="/">Home</router-link>
<router-link to="/post">Post</router-link>
</div>
<router-view/>
post.vue
<div>
<router-link :to="{name: 'PostDetail', params: {id: 1}}"/>
</div>
<div class="child">
<router-view/>
</div>
postdetail.vue
<div>
Child Component
</div>

Optional route params lost when access child route components Vue 2

I got a problem with optional route params, that route param (id) is lost when I access the child component
this is my route config on the router
{
path: "route/:id?",
component: () => import("*****"),
props: true,
children: [
{
path: "/",
redirect: { name: "*****" }
},
{
path: "information",
component: () => import("****")
},
]
},
this is my route expected companies/1/description etc
is there something wrong with my code?
this is how I push the router
it shows success like that companies/1/information but if i go through to another child route.. this param is lost like companies/direction
If I understand it correctly you expect the <router-link> placed inside your Company component to somehow "inherit" route params from the current route automatically.
Unfortunately this is not how it works with optional params - it would work with non-optional params but optional params must be passed explicitly...
:to="{ name: link.routeName, params: { id: $route.params.id } }"
const companies = Vue.component('companies', {
template: `
<div id="app">
<router-link to="/companies/1">Company 1</router-link>
<router-link to="/companies/2">Company 2</router-link>
<router-view></router-view>
</div>
`
})
const company = Vue.component('company', {
props: ['id'],
template: `
<div id="app">
<h4> {{ id }} </h4>
<h4> {{ $route.fullPath }} </h4>
<router-link :to="{ name: 'adminBusinessProfileInformation', params: { id: $route.params.id } }">Information</router-link>
<router-link :to="{ name: 'adminBusinessProfileDescription', params: { id: $route.params.id } }">Description</router-link>
<router-view></router-view>
</div>
`
})
const child = Vue.component('child', {
template: `
<div>
Child component
</div>
`
})
const router = new VueRouter({
mode: 'history',
routes: [{
path: '/',
redirect: 'companies'
},
{
name: 'companies',
path: '/companies',
component: companies
},
{
path: '/companies/:id',
component: company,
props: true,
children: [{
path: "/",
redirect: {
name: "adminBusinessProfileInformation"
}
},
{
path: "information",
component: child,
name: "adminBusinessProfileInformation"
},
{
path: "description",
component: child,
name: "adminBusinessProfileDescription"
}
]
}
]
})
new Vue({
el: '#app',
router,
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<router-link to="/companies/1">Company 1</router-link>
<router-link to="/companies/2">Company 2</router-link>
<router-view></router-view>
</div>

vue-router should route on click, not working

I am trying to create a navigation and when someone clicks. It should navigate to another Vue.component.
Note: This app is Electron+Vue based, but it should work as normal Vue Project, as i've tried same concept in other Electron+Vue Project too.
Right now it shows no Content because of error of Router.
Am i missing anything?
Error Shows like this in Screenshot
===================================
----HelloWorld.vue-----
<template>
<div class="collapse navbar-collapse nav-item-outer" id="navbarNav">
<ul class="nav navbar-nav nav-list">
<li class="nav-item active" #click="navigate('information')">
<span class="nav-item-icon nav-item-icon-info"></span>
<span class="nav-item-title">Info</span>
</li>
<li class="nav-item" #click="navigate('map')">
<span class="nav-item-icon nav-item-icon-location"></span>
<span class="nav-item-title">Location</span>
</li>
<li class="nav-item" #click="navigate('videocontent')">
<span class="nav-item-icon nav-item-icon-video"></span>
<span class="nav-item-title">Video</span>
</li>
<li class="nav-item" #click="navigate('surfstation')">
<span class="nav-item-icon nav-item-icon-surf"></span>
<span class="nav-item-title">Surf</span>
</li>
<li class="nav-item" #click="navigate('telephone')">
<span class="nav-item-icon nav-item-icon-telephone"></span>
<span class="nav-item-title">Telefon</span>
</li>
<li class="nav-item" #click="navigate('/')">
<span class="nav-item-icon nav-item-icon-home"></span>
<span class="nav-item-title">Home</span>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
methods: {
navigate(route) {
this.$router.push(route)
},
}
}
</script>
===================================================
----main.js-------
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.config.productionTip = false
Vue.use(VueRouter)
export default new VueRouter({
routes: [
{
path: '/',
name: 'dashboard',
component: require('#/views/Dashboard').default
},
{
path: '/information',
name: 'information',
component: require('#/views/Information').default
},
{
path: '/map',
name: 'map',
component: require('#/views/Map').default
},
{
path: '/surfstation',
name: 'surfstation',
component: require('#/views/Surfstation').default
},
{
path: '/telephone',
name: 'telephone',
component: require('#/views/Telephone').default
},
{
path: '/videocontent',
name: 'videocontent',
component: require('#/views/VideoContent').default
},
{
path: '*',
redirect: '/'
}
]
})
new Vue({
render: function (h) { return h(App) },
}).$mount('#app')
=====================================================
----App.vue------
<template>
<div id="app">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<main>
<router-view></router-view>
</main>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
A help would be Appreciated.
Instead of exporting the router, put in a const and specify it inside new Vue({
main.js
const router = new VueRouter({
routes: [
...
],
// Prevent white screen on reload (in production build)
mode: process.env.IS_ELECTRON ? 'hash' : 'history'
})
new Vue({
router,
render: h => h(App),
mounted () {
// Prevent white screen when the app is launched (in production build)
this.$router.push('/').catch(error => { })
}
}).$mount('#app')

How do I style an active route that has query params?

.router-link-exact-active class is not added to the active route when navigating to that route via this.$router.push(...).
For example:
// ...
this.$router.push('/?foo=bar');
Will not result in <router-link :to="'/'">Home</router-link> being assigned the .router-link-exact-active class.
Is this a bug, or an intended behaviour?
How should the active link be styled in this case?
P.S
This is a fiddle illustrating the issue: https://jsfiddle.net/alexlomm/dumeejyy/7/
May be you should use params or query separately and use query or params value as template data, As in below example
<router-link :to="{ path: '/', query: url_query }">home</router-link>
Check in docs
<script type="text/javascript" src="https://unpkg.com/vue#2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://unpkg.com/vue-router#2.7.0/dist/vue-router.js"></script>
<div id="app"></div>
<script type="text/javascript">
Vue.use(VueRouter)
const Home = {template: ''}
const router = new VueRouter({
mode: 'history',
routes: [
{path: '/', name: 'home', component: Home},
]
})
new Vue({
router,
template: `
<div id="app">
<p>Current path: {{ $route.fullPath }}</p>
<ul>
<li><router-link :to="{ path: '/', query: url_query }">home</router-link></li>
</ul>
</div>
`,
data:{
url_query:{}
},
mounted() {
this.url_query = { foo: 'bar' };
}
}).$mount('#app')
</script>
In your Fiddle

Passing route params in vue router v-link

I have this anchor in Parent.vue:
<a v-link="{ path: '/somepath/somesubpath', query: { messageId: 999}}"> Here </a>
or also this
<a v-link="{ path: '/somepath/somesubpath', params: { messageId: 999}}"> Here </a>
My Child.vue is like this:
<template>
{{messageId}}
// other html..
</template>
<script>
export default {
props: ['messageId']
}
</script>
In Child.vue I can not see the value of {{messageId}} being rendered. This means that the value is not getting passed.
I am using vue router 0.7.5 and the docs here do not specify how to specify params in v-link (without named routes).
So whats the proper way to pass in params using vue router with v-link?
Route params are available in the this.$route.params property. They do not need to be passed as a component property.
In your case you can access the messageId param in your template like so:
{{ $route.params.messageId }}
However, you also need to make sure that the template for your Child.vue component has a single root element and not just text. Otherwise, the component will not render.
So, you'd at least need something like this:
<template>
<div>
{{ $route.params.messageId }}
</div>
</template>
Here's a simple example:
Vue.component('child', {
template: `
<div>
message id: {{ $route.params.messageID }}
</div>
`
});
const Home = {
template: '<div>Home</div>'
};
const Message = {
template: `
<div>
<span>Message View</span>
<child></child>
</div>
`
};
const router = new VueRouter({
routes: [
{ path: '/', component: Home, name: 'home' },
{ path: '/message', component: Message, name: 'message' }
]
})
new Vue({
router,
el: '#app'
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<router-link :to="{ name: 'home' }">home</router-link>
<router-link :to="{ name: 'message', params: { messageID: 123 } }">message view</router-link>
<router-view></router-view>
</div>