Vue Router won't display components - vue.js

I'm setting up a new Vue.js + Vue Router app and when I click on the router links set up in the App.vue, the content isn't showing up. The router and app objects are well defined, I've tried printing them. The app in the index.html is working since I can see the Bootstrap navbar.
Only the content of my views isn't showing up.
Main.js
import BootstrapVue from 'bootstrap-vue';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';
import Vue from 'vue';
import app from './App.vue';
import router from './router';
Vue.config.productionTip = false;
Vue.use(BootstrapVue);
new Vue({
render: (h) => h(app),
router,
}).$mount('#app');
router.js
import Vue from 'vue';
import Router from 'vue-router';
import Companies from './views/Companies.vue';
Vue.use(Router);
export default new Router({
routes: [
{
path: '/Companies',
name: 'Companies',
component: Companies,
},
],
});
Companies.vue
<template>
<div id="a">
abcde
</div>
</template>
<script>
export default {
name: 'Companies',
};
</script>
Edit : Ok that was the missing router view in app.vue! thanks

Related

VUE 2 Unknown custom element: <router-view> - did you register the component correctly? For recursive components, make sure to provide the "name"

I want to route HomeView.vue view to App.vue and this is my code.
...............................................................
Main.js
import Vue from 'vue'
import router from './router/index'
import App from './App.vue'
import vuetify from './plugins/vuetify'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.config.productionTip = false
new Vue({
vuetify,
router,
render: h => h(App),
}).$mount('#app')
App.vue
<template>
<router-view />
</template>
<script setup></script>
<style lang="scss"></style>
HomeView.vue
<template>
<p>Home</p>
</template>
<script>
export default {}
</script>
<style lang=""></style>
Router/indes.js
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
component: HomeView,
},
]
const router = createRouter({
history: createWebHistory(),
routes,
})
export default router
I tride use ".$use(router)" before .$mount(#app)
Someone can help?

Use a vue component inside a router component

I use a route-view inside my App.vue, in my routes I'm using the component Base, what I want is call a 'teste' component inside a 'Base' component.
I already read about nested-named-views in vue-router, but not resolved my problem.
Route.js
import Base from './components/base/Base.vue';
export const routes = [
{
path: '/',
components: {
default: Base
}
},
App.vue
<div id="app">
<router-view></router-view>
</div>
Base.vue
<template>
<teste></teste> //this component dosent render
</template>
<script>
import teste from './SideBar.vue'
export default {
name: 'Base',
components: {
teste
}
}
SideBar.vue
<template>
<p>teste</p>
</template>
<script>
export default {
name: 'teste'
}
Main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
import Vuex from 'vuex';
import {routes} from './routes'
Vue.use(VueRouter)
Vue.config.productionTip = false
const router = new VueRouter({
routes
});
new Vue({
render: h => h(App),
router
}).$mount('#app')

My entire Vue app is created twice on page reload, why is this happening?

Putting a console.log() into the created() & mounted() methods of my App.vue component, the App is created & mounted twice on every page refresh.
I've been driving myself crazy trying to find the reason for this, I've stripped my App right to the bones and it's still happening
This is my App.vue:
<template>
<div id="app">
</div>
</template>
<script>
export default {
name: 'app',
mounted() {
console.log('mounted');
},
created() {
console.log('created');
}
}
</script>
And this is my main.js:
import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'
import Vuetify from 'vuetify'
import { store } from './store'
import router from './router'
import App from './App.vue'
import 'vuetify/dist/vuetify.min.css'
import axios from 'axios'
Vue.use(Vuetify)
Vue.use(Vuex)
Vue.use(VueRouter)
Vue.config.productionTip = false
axios.defaults.baseURL = process.env.API_URL;
new Vue({
el: '#app',
render: h => h(App)
});
Found the issue: My webpack config was pointing to same file twice
My webpack config was pointing to same file twice

How to use Vue Router in Vue 2

I'm learning Vue, and started of with the webpack template. The first thing I'm trying to do is to add support for Vue Router, but I've spent several hours on it now without being able to render a single route (I always end up with a blank page). Frustrating!
I simply want to have a single .vue file, acting as the layout file, into which different .vue files, acting as "pages", are rendered into. Can someone tell me how to do this, please? Here's my latest failed attempt:
main.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import Home from './components/Home'
import About from './components/About'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const app = new Vue({
router: new VueRouter({
routes
}),
component: App
}).$mount('#app')
App.vue (the layout file)
<template>
<div id="app">
<h1>Hello App!</h1>
<p>
<router-link to="/">Go to Foo</router-link>
<router-link to="/about">Go to Bar</router-link>
</p>
<router-view></router-view>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
components/About.vue (almost identical to components/Home.vue)
<template>
<div>
<h1>About</h1>
<p>This is the about page!</p>
</div>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
I finally got it to work. The main.js file should be written like this:
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import Home from './pages/Home'
import About from './pages/About'
Vue.use(VueRouter)
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = new VueRouter({
routes
})
const app = new Vue({
router,
template: '<App />',
components: {
App
}
}).$mount('#app')
I hope this saves hours of trouble for someone else.
EDIT
The following:
const app = new Vue({
router,
template: '<App />',
components: {
App
}
}).$mount('#app')
can preferably be replaced with:
const app = new Vue({
router,
render: function(createElement){
return createElement(App)
}
}).$mount('#app')
I found out how to get main.js to call the index.js file in folder router and work with the routes defined there:
I had created my app via the VUE UI (with VUE 3.1 and CLI/3)
In the VUE UI there is an option to add plugins. I chose the router plugin (route) and it asked if I want to install a new route or install the framework. (You first need to install the framework...)
It then changed my main.js file to have the following: (the additions are marked with comments)
import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router' // added by router plugin
Vue.config.productionTip = false
new Vue({
router, // added by router plugin
render: h => h(App)
}).$mount('#app')
It also added a new folder router and in it index.js with the code
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Hello from '#/components/HelloWorld' // #pashute: I added this later...
Vue.use(VueRouter)
const routes = [
// {
// path: '/',
// name: 'Hello',
// component: Hello
// }
]
// eslint-disable-next-line no-new
const router = new VueRouter({
routes
})
export default router
It also installed the latest router package and added a link in the HelloWorld component to the router documentation.
By the way, notice the extra name: in the route definitions.

vue 2.0 and vue-router 2.0 build SPA, router-view did not update

Build A Single page app with vue 2 and vue-router 2 and vuex 2, but do not update
/src/main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { sync } from 'vuex-router-sync'
sync(store, router)
new Vue({
router,
store,
...App
}).$mount('#app')
/src/router/index.js
import Vue from 'vue'
import routes from './routes'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
routes
})
export default router
/src/router/routes.js
import loader from './loader'
const routes = [
{ path: '/index', name: 'index', component: (r) => require(['./../views/index.vue'], r) }
]
export default routes
/src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {}
})
export default store
/src/view/index.vue
<template>
<div>
<h1>Hello index</h1>
</div>
</template>
<script>
import loader from './../../../router/loader'
export default {
name: 'index',
created () {
console.log('This is index')
}
}
</script>
/src/App.vue
<template>
<div id="app">
<router-link :to="'index'">index</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app',
mounted () {
console.log('Hello App')
}
}
</script>
I do not know what's wrong with the code, the <router-view> do not update when I change the route.
I think your app is not properly initialized yet. You are starting your app as follows:
new Vue({
router,
store,
...App
}).$mount('#app')
You have the spread operator ...App, which is not necessary - it is used to convert array items to function args, and not correct in this context. Instead you should use the render function as provided in webpack template :
/* eslint-disable-line no-new */
new Vue({
el: "#app",
store,
router,
render: h => h(App)
})
Also you have attempted to import loader in routes and index, which is not necessary if you are using vue-cli.
Maybe you can start with the webpack template as base and slowly add functionality one step at a time: start with route definitions and your route components, ensure that everything works, and finally add vuex.
$mount('#app') is fine, I think the problem is your routes.js
Try this instead:
import Index from './../views/index.vue'
const routes = [
{ path: '/index', name: 'index', component: Index }
]
And here is a working webpack template which is already configured vue-router and vuex properly, you can initialize this template via vue-cli, for your reference: https://github.com/CodinCat/vue-webpack-plus