I'm having trouble using Vueitify and the router - whenever I try to change components with Vue router, the URL updates but the component remains

Currently, I am working on setting up a single-page Vue application that utilizes Vuetify and the Vue Router.

It seems like my routes are properly configured as:

  1. I can see them in the Vue tools, under both history and routes
  2. The URL and parameters update correctly in the address bar

However, despite all this, the component linked to the route does not show up. It just remains on the home page component without any console errors appearing.

I suspect there might be some confusion in how my main app.js file is structured. As someone who is fairly new to Vue, I may not fully understand how everything interacts. I've tried adjusting where I import things, renaming routes, even removing Vuetify, but I still cannot get the component to switch along with the route.

app.js

import Vue from 'vue'
import vuetify from './plugins/vuetify'
import router from "./router";

import myDefaultView from './Views/myDefaultView'

Vue.component('myDefaultView', myDefaultView)

Vue.config.productionTip = false

//I have a hunch something might be off in this section
new Vue({
    router,
    vuetify,
    render: h => h(myDefaultView)
}).$mount('#app')

router.js

import Vue from "vue";
import Router from "vue-router";
import myDefaultView from './Views/myDefaultView'
import userPage from "./Views/userPage";

Vue.use(Router);

export default new Router({
    routes: [
        {
            path: "/",
            name: "home",
            component: myDefaultView
        },
        {
            path: "/user/:userId",
            name: "userPage",
            component: userPage

        },
    ]
});

myDefaultView.vue

//relevant code snippets only
<template>
        <v-main>
            <template>
                <v-data-table
                        @click:row="goToUserPage"
                        :headers="headers"
                        :items="users"
                        :items-per-page="15"
                        class="elevation-1"
                >
                </v-data-table>
            </template>
</template>

<script>
    import axios from 'axios';
    import router from "../router";

    export default {
        props: {
            source: String,
        },
        data () {
            return {
                users: [],
                headers: [
                    {
                        text: 'User Name',
                        align: 'left',
                        sortable: true,
                        value: 'user.name',
                        class: 'text-subtitle-1'
                    },
                    { text: 'Date joined', value: 'user.joined'},
                    { text: 'Points ', value: 'user.points' },
                    
                ],
            }
        },
        created () {
            this.$vuetify.theme.dark = true
        },
        mounted () {
            this.getUsers();
        },
        methods: {

            getUsers() {
                axios.get("http://local.getusers.com)
                    .then(response => {
                        this.users = response.data;
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },

            goToUserPage(item,row) {
                router.push({ name: 'userPage', params: { userId: row.item.user.id } })
            }
        }

    }


</script>



userPage.vue

<template>
    <span>This vue is arbitrary, if I could just get it to display I could make it more cool</span>
</template>

<script>
    export default {
        name: "userPage"
    }
</script>

<style scoped>

</style>

Answer №1

In Vue, the routing system works by having an app component that includes a <router-view>. This placeholder will be swapped out with the component corresponding to the current route.

If there are other elements you want to always display (like a sidebar, menu, or footer) regardless of the current route, make sure to place them in the app component as well.
By including these elements in the app component, they can still react to changes in the route without being re-rendered each time – allowing for smoother transitions between states.

Any content that should be re-rendered (or not rendered) when the route changes should be placed within the components resolved by the router.

On another note, starting a new Vue project using @vue/cli's create command will automatically set up a basic functioning router if you choose this option during setup.
Additionally, remember that you can include multiple <router-view> tags in a single route to show sub-pages within a page.

For more information, refer to the documentation here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular Validation displays ng-valid when the form is actually invalid

I am currently working on a wedding RSVP form https://i.stack.imgur.com/Ct8Ux.png My objective is to hide the DONE submit button and only display it when the form is considered valid. <form method="POST" action="http://l.bheng.com:8888/wedding" acce ...

Has getCurrentInstance() in Vue 3 been phased out?

In my research, I've come across mentions of the getCurrentInstance() function in older documents and code, but it doesn't seem to be included in the current Vue 3 documentation. Is the getCurrentInstance() function deprecated? If it is, what i ...

Just beginning my journey with node.js and looking for guidance on implementing an npm module into my front-end vanilla JavaScript project

Starting off, I must admit that my knowledge of node.js and web development is quite limited. Despite searching through stackoverflow and Google for a solution, I've come up empty-handed. My main concern is figuring out the necessary steps to take sin ...

My code gets disrupted when I use classes instead of IDs

My HTML code works fine when I use IDs and select them in my javascript with getElementByID. However, if I switch to using classes instead of IDs, my code stops working. I want to switch to using classes because I am collaborating on a project with someon ...

Obtaining cookies from a separate domain using PHP and JavaScript

Imagine you have a cookie set on first.com, called "user". Now, the goal is to retrieve that cookie on second.com using JavaScript and AJAX. Unfortunately, it's not working as expected and you're receiving xmlHttp.status=0. Here is a sample code ...

What is the best way to create a sequential number pattern of 1, 2, 3, 4 in Mongoose?

Is there a way to create a similar pattern without coding experience? I'm not familiar with coding, so looking for guidance. Here is the code snippet in question: array = 1,2,3,4,5 const note = new notedModel ({ _id: array, note: args[1] ...

What is the best way to utilize a single AngularJS 1.5 component multiple times within a single view?

While working on a project using AngularJS 1.5's new components, I encountered an issue with widget isolation. It seems that when I use the same widget multiple times, they end up sharing their controller or scope. I was under the impression that comp ...

What is the best way to add a button to every row in Titanium Studio?

Is there a way to add a different button inside each row (createTableViewRow)? I have created five buttons using Titanium.UI.createButton, but I'm struggling to figure out how to place all five buttons in every row. Can someone provide some guidance o ...

Prevent scaling in CSS3DRenderer

For a detailed explanation of what I am attempting to achieve, please refer to my previous question here. In summary: My goal is to have HTML elements rotate in sync with OrbitControls to give the illusion that these elements are attached to a globe and m ...

A guide to accessing the sibling of each selector with jQuery

Imagine you have the following HTML structure: <div class="parent"> <div class="child"></div> <div class="sibling">content...</div> </div> <div class="parent"> <div class="child"></div> <div ...

Delay the HTTPs request until the password encryption is complete

Hey there! I'm working on a post request where I need to collect login data (name, email, password) from users, validate it, encrypt the password, and then store the data. However, I'm facing an issue with the encryption function taking time to r ...

Checking authentication globally using Vue.js

In the main blade file, I have the following code snippet: <script> window.App = {!! json_encode([ 'csrfToken' => csrf_token(), 'user' => Auth::user(), 'signedIn' => Auth::check() ...

Canceling pending asynchronous actions in React/Redux: A step-by-step guide

Imagine the scenario where a user navigates to a page and two asynchronous Redux actions are dispatched simultaneously to fetch related sets of data. If one of these fetches fails, the component will detect it and render an error component on the next cycl ...

Tips for updating one-time binding data in AngularJS

I am currently facing an issue with a div that displays details such as mobile number, name etc. in the format: {{::mobilenumber}}, {{::name}} Within this div, there is a button that when clicked should populate the same values in a new form However, des ...

Tips on flipping a texture in Three.js along the horizontal axis

Currently developing a 360 viewer with textures contained within a cylinder. Encountering an issue where textures are appearing horizontally inverted. Although I am aware of the texture.flipY option, I have not been able to locate a texture.flipX function ...

Keep your Vue site/PWA fresh and up-to-date with automatic updates for every new release

My Vue application serves both as a browser app and as a PWA. I want to make sure that users always have the most recent version of the app whenever updates are pushed to the server. My tech stack includes Nginx, Django, vue-cli, and @vue/cli-plugin-pwa. ...

The object does not recognize the property or method 'dataTable'; even when jQuery is included

I have been troubleshooting this issue within the realm of jQuery and data tables, but none of the recommended solutions have resolved it (in case this appears to be a duplicate right away!) Within my .Net project (C#), I have references to jQuery, Datata ...

I am experiencing difficulties with getting Google Analytics to function properly within my VuePress project

Recently, I started a new vuepress project I am attempting to implement Google Analytics to track its progress. However, there seems to be an issue connecting my project with Google Analytics I followed the Documentation Guide ( ) to include yarn add - ...

The HTML elements in my JSX code seem to constantly shift around whenever I resize my webpage

Using react.js, I'm currently developing a website that appears like this before resizing: pre-resize_screenshot_website However, upon vertical or diagonal resizing, the layout becomes distorted especially in the 'availability search bar' ...

Handling concurrent requests in DjangoDiscover how Django manages multiple simultaneous requests

Handling multiple requests in a web application can be a challenging task, especially when the server needs to perform complex operations like making API requests and executing database queries. In this post, we will explore how Django can effectively mana ...