Navigating with vue-router using guard redirections

I am currently working with vue.js and I'm looking to implement a functionality where I can redirect a user to another URL within the navigation guard called beforeRouteEnter.

The main objective is to check if the current city of the user is included in the URL, and if not, redirect them accordingly.

For example, if a user enters london.example.com, everything is fine. However, if they just type example.com, I want to automatically redirect them to london.example.com

Current Issue

It appears that using window.location.replace has no impact on the redirection process.

Furthermore, when using next(), it appends the response to the URL instead of replacing it.

Code Snippet

beforeRouteEnter (to, from, next) {
const subdomain = window.location.hostname.split('.')[0]
if (!subdomain) {
  axios.get('/api/getCity').then((response) => {
    if (response.data) {
      window.location.replace(response.data + '.localhost:8080')
      next({ path: 'http://' + response.data + '.localhost:8080' })
    }
  })
}

}

Answer №1

redirectWindow('http://' + result.onlineData + '.localhost:8080')

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

Receiving information from a promise within an if statement in Vue

Recently, I encountered an issue with my login function. I needed to store some data in my session for future use but faced a problem where the data was being pushed into the session before I could retrieve the result from a promise. Here's the snipp ...

Encountering an npm error: How can I install compiler-sfc that satisfies the peer dependency requirement for @babel/core?

Review of my packageJSON "devDependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.35", "@fortawesome/free-solid-svg-icons": "^5.15.3", "@vue/compiler-sfc": "^3. ...

The process of passing $refs in Vue explained

I have a feature where all the data is passed to the child component. Currently, I am able to pass $attrs and $listeners successfully: <template> <el-form v-on="$listeners" v-bind="$attrs" :label-position="labelPosition"> <slot /> ...

At random intervals, a ReferenceError is triggered stating that Vue is not defined

While working on an application that uses templates rendered by Apache Velocity, I encountered the error "Uncaught ReferenceError: Vue is not defined" when trying to incorporate vue.js components. Oddly enough, this error is not consistent - it occurs most ...

The component cannot be created using shallowMount because vm.$refs['VTU_COMPONENT'] is not defined

Not sure if it's a bug or if I'm making a mistake. I attempted to use shallowMount to mount my main App component, but it doesn't seem to work. The error message I receive is: Cannot set properties of undefined (setting 'hasOwnProper ...

Which method is better for presenting data: PHP or JavaScript?

Currently, I am diving into vue.js on laracasts.com where Jeffrey Way demonstrates 2 ways to showcase data on a webpage. One method involves displaying data using Laravel foreach loops, while the other utilizes vue.js. This has led me to ponder: is there ...

utilize computed properties to automatically update components when Vuex state changes

Currently, I'm in the process of developing a basic movie application using Vue and Vuex. My goal is to allow users to add movies to a favorites list by clicking a button. The movies are stored in the Vuex state; however, I'd like the text on the ...

Update the headers of the axios.create instance that is exported by Axios

I have a single api.js file that exports an instance of axios.create() by default: import axios from 'axios' import Cookies from 'js-cookie' const api = axios.create({ baseURL: process.env.VUE_APP_API_URL, timeout: 10000, headers ...

What causes the initial text to briefly reappear for half a second after the loading state has finished and the text has changed? (Rendering issue)

I'm currently working on a Button component that features a spinner when loading=true to manage loading states. The text switches between "track" and "tracking" depending on the state. I've been using devTools to monitor the state changes, but I ...

Deciding on the optimal times to implement data structure methods within Vue.js applications

Currently studying the Vue.js v2 documentation and I'm noticing a difference in how data is structured. When looking at the official documentation, they demonstrate creating data like this: var data = { a: 1 } var vm = new Vue({ el: '#example&a ...

Is there a way to successfully submit multiple locations, each separated by commas, through the multipart form?

Here is my HTML form: <form method="POST" enctype="multipart/form-data" v-on:submit.prevent="handelSubmit($event);"> <div class="clear"> <div class="col-md-3"></div> <div class="col-md-6"> <div class="form ...

Laravel - Jetstream with InertiaJS: Implementing guards in the main header menu of AppLayout.vue

Is there a way to utilize guards for adding a specific admin menu entry using guards? I am aware that passing guard-"data" from controllers to view is possible as mentioned in the documentation: class UsersController extends Controller { public functio ...

Storing data from multiple children and triggering a parent component function in Vue: A guide

In my Messaging Tool component, I have several child components that make up the messaging tool interface. You can see the visual representation of these components here: https://i.stack.imgur.com/JoEko.png There are 3 channels within the tool, each openi ...

I am attempting to show a message using v-model, however, it seems to be malfunctioning

I am currently learning how to use Vue.js with Laravel. However, I am encountering an issue when trying to display a message in an input tag using v-model. <div class="card" id="myAppId"> <p>@{{ message }}</p> <input type="text" v-mod ...

What steps should I take to modify this Vue.js function?

<script lang="js"> import { ref } from 'vue'; export default { setup(){ const dateInput = ref(new Date()); function handleDateSelection(payload : Date): void { console.log(payload); } return ...

Managing Uploaded Files using Laravel and Vue.js

Currently, I am in the process of developing a Vue.js + Vuetify + Axios + Laravel framework for creating a dashboard. One of the functionalities I am working on is the user profile, which includes the ability for users to upload a profile picture and their ...

Invoking a child component's method using a parent event

Hello, I am a newcomer to VueJS and JavaScript and would greatly appreciate your assistance. I am facing an issue with my method "greet" not working, and I am uncertain as to why. When I click on the button "change to bar," I receive the following error: ...

Challenges arise when creating responsive map regions in Vue 3

I am currently working on a project in Vue 3 that involves an image map with click events on certain areas. The challenge I'm facing is that the images scale according to the browser size, but the coordinates of the image map are fixed pixel sizes. I& ...

Tips for connecting a Vuetify v-menu to its parent element within a list that can be scrolled

Within the project I am working on, there exists a scrollable inventory of items. Each item possesses a menu that is triggered open when the respective activator button is hovered over. By default, v-menu components are connected to the v-app element, ser ...

Adjust the x-axis on the Vue.js bar chart

I'm currently working on a Vue.js Laravel application where I am trying to incorporate a bar chart using the ApexCharts module. <apexchart ref="apexChart" :options="chartOptions" :series="chartData" type="bar" ...