Exploring the depths of Vue.js routing through nesting

My Current Route is

function route(path, view) {
return {
    path: path,
    meta: meta[path],
    component: resolve => import(`pages/${view}View.vue`).then(resolve)
   }
}

route('/', 'Home'),
route('/help', 'Help),
route('/blog', 'BlogList'),
route('/blog/:slug', 'BlogDetails'),

Everything seems to be working properly. However, I noticed that when I navigate from the /blog/:slug route and then click on a button in that component to go back to /help, the route pattern shows up as /blog/help instead of just /help.

Answer №1

Take a look at my jsfiddle example using Vue.js routes, it might be helpful for you.

const Home = {
        template: '<h1>Home</h1>'
    };

    const Help = {
        template: '<h1>Help</h1>'
    };

    const Profile = {
        template: '<h1>Profile</h1>'
    };

    const User = {
        template: '<h1>User</h1>'
    };

    routes = [
        {path: '/', component: Home},
        {path: '/help', component: Help},
        {path: '/user', component: User},
        {path: '/user/:id', component: {
            render(h) {
                return h(
                    'h1',
                    { style: {color: 'skyblue'} },
                    `User id: ${this.$route.params.id}`
                );
            }
        }}
    ];

    const router = new VueRouter({
        routes
    });

    new Vue({
        router
    }).$mount('#app');
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
   <div id="app">
        <router-link :to='{path: "/"}'>/</router-link> <br>
        <router-link :to='{path: "/help"}'>/help</router-link> <br>
        <router-link :to='{path: "/user"}'>/user</router-link> <br>
        <router-link :to='{path: "/user/userid"}'>/user/userID</router-link>
        {{$route.path}}
        <router-view></router-view>
        <br>
        <button @click='$router.go(-1)'>go back</button>
        <button @click='$router.go(1)'>go forward</button>
    </div>

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

Scrolling automatically to a DIV is possible with jQuery, however, this feature is functional only on the initial

I'm currently working on a Quiz site and I've encountered a puzzling issue. Since the explanation only appears after an answer is selected - essentially a "hidden" element, I decided to wrap that explanation into a visible DIV. The code I'v ...

Troubleshooting the issue of React forms hook not functioning properly with Material UI Select input

How the Textfield below should load: https://i.sstatic.net/29Sz4.png How it actually loads: https://i.sstatic.net/TdPYM.png My Select component, created using Material UI and React form hook, is not loading the default value as expected. The component ...

Make the download window appear automatically when downloading a file

How can I use JavaScript/TypeScript to prompt the browser to open the download window? My goal is to give users the ability to rename the file and select the download folder, as most downloads are saved directly in the default location. This is how I curr ...

The tooltip being displayed is plain and lacks any design elements

When I hover over my a element, only a simple tooltip appears without any styling, unlike what is shown in the Bootstrap documentation. (I am creating the a element using JavaScript) HTML <!DOCTYPE html> <html lang="en"> <head> ...

Converting an array of date strings to a single string in JavaScript

Here is the JSON format I received with dynamic data: {"range":["2018-07-23T16:03:26.861Z","2018-07-23T16:03:26.861Z"]} Now, I need to convert this into the following format: range(20180723,20180723) Below is my code snippet : var data:Date[] = {"rang ...

Is there a clash between jquery_ujs and Kaminari AJAX functionality in Rails 4?

After some investigation, it seems that there is a conflict between jquery_ujs and Kaminari's AJAX support in my Rails 4 application. Within my application.js file, I have included the following: //= require jquery //= require jquery_ujs //= require ...

The curious case of looping and peculiar Vue actions

I've been working on a project where I am looking to dynamically add more input fields upon clicking a button. My initial attempt using jQuery.append ran into issues with detecting v-model. Switching gears, I decided to try achieving the same outcom ...

What is the best way to define the active <li> tab using JQuery?

Initially, my HTML code includes a set of <li> elements that I want to dynamically add to the existing <ul>. <ul class="tabs" id="modal_addquestion_ul"> <li class="tab-link current" data-tab="multipleChoice">Multiple Choice< ...

What could be causing the JQuery date picker to fail to load on the initial ng-click event?

I am encountering an issue with a JQuery UI date picker that is loaded through ng-click using the code below: Input: <input type="text" id="datepicker" autocomplete="off" ng-model="selectedDate" ng-click="showDatepicker()" placeholder="(Click to sele ...

What steps can be taken to identify the two highest numbers in an array, one being positive and the other

Seeking the optimal solution to resolve this issue Issue: Develop a function called ArrayChallenge (Javascript) that takes a single argument "arr" representing an array of numbers. The function should return the string true if there exist two numbers in t ...

Utilizing Angular2 Guard to Ensure False IdentityServer4 OIDC Security

After successfully authenticating a user and redirecting them back to the main site, the following code runs: <script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.2.2/oidc-client.min.js"></script> <h1 id="waiting">Waiting... ...

Trigger a click event on a nested Angular 13 component to remove a class from its grandparent component

In my Angular 13 project, I am working with 3 components: Child Component : Burger-menu Parent Component : Header Grand-Parent Component : app.component.html Within the burger-menu component, there is a close button. When this button is clicked, I want t ...

Implementing long-lasting login functionality in React using JSON Web Tokens

Currently, I have developed an application using React client/Express API with functioning Authentication. Users are able to register and login successfully. My next step is to implement persistent login using JWT tokens so that when a user accesses the U ...

Ways to avoid caching statically generated pages without having to enable ISR in Next.js

I'm currently exploring ways to tailor the Cache-Control headers for my NextJS server when delivering pages generated at build time (SSG). The objective is straightforward: since every CDN caches server responses based on their headers, and I want sta ...

Using a Javascript loop to showcase values from a database

As a Python developer who is new to JavaScript and AJAX, I am currently working on creating a pie chart that displays database values. $(document).ready(function () { google.charts.load('current', { 'packages': ['corechart&ap ...

Having trouble with the Angular router link suddenly "failing"?

app.routes.ts: import { environment } from './environment'; import { RouterModule } from "@angular/router"; import { ContactUsComponent } from './static/components/contact-us.component'; import { HomeComponent } ...

Change the size of a particular div upon hovering over another element

I'm trying to figure out how to scale my div with text when a user hovers over a button, but I've tried various operators like >, ~, +, - with no success. section { background: #000; color:#fff; height: 1000px; padding: 150px 0; font-family ...

Utilizing the jquery-confirm library in a Laravel 5.5/Vue.js application

Within my laravel 5.5/vue.js application, I wanted to integrate the jquery-confirm library. In order to do so, I added the following code snippet to the resources/views/layouts/app.blade.php file: ... @yield('content') &l ...

Issues with AJAX function not being successfully transmitted

When the timer ends, I want to display a "Not active" message instead of "Active". The timer and code appear to be functioning properly until this point $('.clock').eq().countdown(inputDate). After this line of code, the function stops working an ...

How can one leverage Node JS to effectively manage events?

Is it considered a best practice to utilize events for communication between functions within ExpressJS? If so, what is the proper way to send arguments along with my emit event? ...