Why is my router-link tag not working with my bootstrap framework? - vue.js

I am using vue.js to create a navigation task bar along with bootstrap for the frontend framework.
I configured the router in a router.js file I created.
router.js
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
//enter code here`import components
import levi from './containers/levi'
import product from './containers/product'
import price from './containers/price'
//application routes
const routes = [
{path: '/', component: levi },
{path: '/product', component: product },
{path:'/price', component: price }
]
//export router instance
export default new Router({
mode: 'history',
routes,
linkActiveClass:'is-active'
})
I created the containers with the files for the navigation bar.
price.vue
<template>
<div id = "price" >
What is the price!
</div>
</template>
<script>
export default{
name: 'price'
}
</script>
<style scoped>
</style>
product.vue
<template>
<div id = "product">
Understanding the levi product
</div>
</template>
<script>
export default {
name: 'product'
}
</script>
<style scoped>
</style>
The components folder has the navigation component
<template>
<div>
<div class="navbar navbar-default navbar-static-top">
<div class="container ">
levi
<button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse" name="button">
<span class="navbar-toggle-Menu">Menu</span>
</button>
<div class="collapse navbar-collapse"></div>
</div>
</div>
</div>
</template>
This div section contains the router-link tags that are not working properly
<div class="nav navbar-nav navbar-right">
<router-link to='/product'> product </router-link>
</div>
end tag for the router-link
<template></template>
<script>
export default {
name : 'navigation'
}
</script>
<style scoped = "true">
</style>
All the initial navigation links are no longer showing when I surround them with the router-link. How can I fix this?

You need to pass the instance of your router to your main Vue instance like so:
//your router is declared as default in its own file so
//in your main Vue instance...
import router from './my_router'
new Vue({
el: '#app',
router
})

Related

Vue Replace Navbar With Router

Vue newbie here. With the out-of-box default app created by Vue-CLI, including Vue Router, you have the top navbar with the Home and About links. What I want is: when you click on the About link, instead of updating the content below the navbar, it will update the entire page i.e. making the navbar disappear.
In App.vue:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view />
</template>
The router is set-up in router/index.js as:
import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/Home.vue";
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
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 = createRouter({
history: createWebHashHistory(),
routes,
});
export default router;
This is to simulate a typical login page where you often don't get navbar.
I played with nested routers but no luck. I'm using Vue 3.
I solved this by extracting the nav links into its own component, and only display the nav links on the pages that need it. This means in the sign-up page I do not include the nav links.
So after creating the default Vue-CLI starting app, I removed the nav components:
<template>
<!-- <div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
<router-link to="/signin">Sign In</router-link>
</div> -->
<router-view />
</template>
And created a new Nav.vue component:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
<router-link to="/signin">Sign In</router-link>
</div>
</template>
Added the Nav component to the pages where I want the nav to show, e.g. Home.vue:
<template>
<Nav />
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
</div>
</template>
I do not include the nav in my sign-in:
<template>This is the SIGN IN page - note there is no navigation links.</template>
Being a Vue noob, not sure if the above is the best approach, but it is working for now.

Cannot render list using V-for in VueJs

I am very new in Vue and I am trying to loop through an array. Don't exactly know what I am doing wrong but the list is not displaying on HTML. Here is the code below: This is an index file that is being rendered through a router view.
<template>
<div class="index container">
<div class="card" v-for="tournament in tournaments" :key="tournament.id">
<div class="card-content">
<h2 class="indigo-text">{{tournament.title}}</h2>
<ul class="tournaments">
<li v-for="(score,index) in tournamnet.scores" :key="index"></li>
<span class="chip">{{score}}</span>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'index',
data () {
return {
tournaments:[
{title:'Muthaiga golf Tournament',slug: 'muthaiga-golf-tournament',scores:['Round 1', 'Round 2', 'Round 3'],id:'1'},
{title:'Wilson Churchhill',slug: 'Wilson Churchhill',scores:['Round 1', 'Round 2', 'Round 3'],id:'2'},
]
}
},
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
</style>
Here is the router view index.js
import Vue from 'vue'
import Router from 'vue-router'
import index from '#/components/index'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'index',
component: index
}
]
})
And here is the app.vue
<template>
<div id="app">
<navbar />
<router-view/>
</div>
</template>
<script>
import navbar from '#/components/navbar'
export default {
name: 'App',
components:{
navbar
}
}
</script>
Any help will be highly appreciated.
Place your span inside the v-for
<ul class="tournaments">
<li v-for="(score,index) in tournament.scores" :key="index+'tournament'">
<span class="chip">{{score}}</span>
</li>
</ul>
Also, it is better not to use the index as a key, I added string 'tournament' to make it more unique.
Additionally, please make sure you got the spelling corrected for 'tournament'.
Link to official docs.
You have a typo on <li v-for="(score,index) in tournamnet.scores" :key="index"></li>
Mispelled tournament
If you look in the console you should see
vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "score" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
found in
---> <Index> at src/components/index.vue
<App> at src/App.vue
<Root>

Error : vue Maximum call stack size exceeded

i'm building an app, i tried to do router for login page but it give me this error :
vue.runtime.esm.js?2b0e:4484 Uncaught RangeError: Maximum call stack size exceeded, is there anything i did wrong
thank you for your help hopefully u can give me a hint on how to fix it
this is the Login Components
<template>
<div class="hello">
Login
</div>
</template>
<script>
export default {
name: '',
props: {
msg: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
Home Components
<template>
<div class="hello">
<header class="globalNav noDropdownTransition">
<div class="container-lg">
<ul class="navRoot">
<li class="navSection logo">
<a class="rootLink item-home colorize" href="/"><h1>Charity Finder</h1></a>
</li>
<li class="navSection primary">
<a class="rootLink item-products hasDropdown colorize" data-dropdown="products">
Products
</a>
<a class="rootLink item-developers hasDropdown colorize" data-dropdown="developers">
Developers
</a>
<a class="rootLink item-company hasDropdown colorize" data-dropdown="company">
Company
</a>
</li>
<li class="navSection secondary">
<a class="rootLink item-support colorize" href="">
Support
</a>
<router-link class="rootLink item-dashboard colorize" to="/login"> Sign in</router-link>
</li>
</ul>
</div>
</header>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
This the router Router
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/login',
name: 'login',
component: Login
}
]
const router = new VueRouter({
routes
})
export default router

vue-router: How to use view-router in more than one element?

A very simple example for using a vue-router in template is the following code:
<template>
<div id="app">
<router-view/>
</div>
</template>
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
what I understand is that the content of router-view will be switched by the relevant component to the path.
However, if I have a template with more than one element affected by router. For example,
<template>
<div id="app">
<h1> header </h1>
<router-view 1/>
<h1> Inner </h1>
<router-view 2/>
<h1> Footer </h1>
</div>
</template>
and let's say that router-view 1 and router-view 2 both can get different components based on the path.
In this case, how would you recommend me to use router?
Based on official doc, you have to use named-views.
Like that, you can have multiple router-view rendering differents components for the same path.
With your example, it becomes :
<template>
<div id="app">
<h1> header </h1>
<router-view /> // this will be the default
<h1> Inner </h1>
<router-view name="inner"/>
<h1> Footer </h1>
</div>
</template>
and your router will look like :
// Don't forget your imports
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: HeaderComponent, // Will render in default router-view
inner: InnerComponent // Will render in router-view named "inner"
}
}
]
})
More complex layouts are also describes in the official doc.

Bulma navbar and VueJS router active link

I've started using Bulma 0.7.1 and VueJs 2.5.17. Now, I'm using Vue router component and I'd like to make the buttons in the navigation bar to be set as active whenever I'm on the "page" represented by the link.
My code is the following
<template>
<div id="app">
<nav class="navbar" role="navigation" aria-label="main navigation" id="nav">
<div class="container">
<div id="navMenu" class="navbar-menu">
<div class="navbar-end">
<a class="navbar-item is-active">
<router-link to="/" exact>Home</router-link>
</a>
<a class="navbar-item">
<router-link to="/about" exact>About</router-link>
</a>
<a class="navbar-item">
<router-link to="/project" exact>Project</router-link>
</a>
</div>
<div class="navbar-end">
</div>
</div>
</div>
</nav>
<router-view/>
</div>
</template>
I know that router component uses class router-link-active to mark the active link. But Bulma probably requires the is-active class applied to the current button.
How should I automate this? I've read that probably I should bind the class is-active to the router-link-active, but I tried:
let routes = ...
new VueRouter({
mode: "history",
routes,
linkActiveClass: "is-active"
});
And did not worked.
Any hint is really appreciated.
You are on the right track and are right, 'is-active' is the answer. In router add.
export const router = new VueRouter({
mode: 'history',
routes,
linkActiveClass: 'is-active'
})
And construct your HTML like.
<nav class="navbar is-white">
<div class="container">
<div id="navMenu"
class="navbar-menu">
<div class="navbar-start">
<router-link :to="{ name: 'templateInfo' }"
class="navbar-item">Template info</router-link>
<router-link :to="{ name: 'thirdpartylibraries' }"
class="navbar-item">Third party libraries</router-link>
</div>
</div>
</div>
</nav>
This works for me so I guess there's some issue with the order of your div's or classes.
Check Bulma Guide, is-active is one modifier, you have to use it together with bulma element.
Most Bulma elements have alternative styles. To apply them, you only
need to append one of the modifier classes. They all start with is- or
has-.
So change the configuration to like linkActiveClass: 'tag is-active' it will work well.
Like below demo which uses tag (you can choose others like box, button etc):
let UserView = Vue.component('user', {
template: '<div>I am User</div>'
})
let AdminView = Vue.component('admin', {
template: '<div>I am Admin</div>'
})
const router = new VueRouter({
linkActiveClass: 'tag is-active',
routes: [
{
path: '/Admin',
name: 'Admin',
component: AdminView
},
{
path: '/User',
name: 'User',
component: UserView
}
]
})
Vue.use(VueRouter)
app = new Vue({
el: "#app",
router
})
.loading {
background-color:red;
font-weight:bold;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue#2.5.16/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h2>Test Vue Router</h2>
<router-link to="Admin">Admin</router-link>
<router-link to="User">User</router-link>
<router-view></router-view>
</div>