What causes the Vue.http configuration for vue-resource to be disregarded?

I am currently utilizing Vue.js 2.3.3, Vue Resource 1.3.3, and Vue Router 2.5.3 in my project while trying to configure Vue-Auth. Unfortunately, I keep encountering a console error message that reads

auth.js?b7de:487 Error (@websanova/vue-auth): vue-resource.1.x.js: Vue.http must be set.
. Even after setting Vue.http in the main.js file, vue-resource does not seem to recognize it for some unknown reason.

Main.js:

import Vue from 'vue'
import Actions from 'actions'
import App from './App'

Vue.use(Actions, {
  locales: ['en', 'zh', 'fr']
})

Vue.http.options.root = 'https://api.example.com'

new Vue({
  render: h => h(App),
  watch: {
    lang: function (val) {
      Vue.config.lang = val
    }
  }
}).$mount('#app')

Actions/index.js

import VueResource from 'vue-resource'
import Router from 'actions/router'
import I18n from 'actions/i18n'

export default {
  install (Vue, options) {
    Vue.use(Router)
    Vue.use(I18n, options.locales)
    Vue.use(require('@websanova/vue-auth'), {
      router: require('@websanova/vue-auth/drivers/router/vue-router.2.x'),
      auth: require('@websanova/vue-auth/drivers/auth/bearer'),
      http: require('@websanova/vue-auth/drivers/http/vue-resource.1.x')
    })
  }
}

Furthermore, adding Vue.use(VueResource) to actions/index.js directly below Vue.use(Router) results in a new error:

Error (@websanova/vue-auth): vue-router.2.x.js : Vue.router must be set.

Alternatively, if I move

Vue.http.options.root = 'https://api.example.com'
right below the import statements, a different error occurs:
Uncaught TypeError: Cannot read property 'options' of undefined

Answer №1

In order to resolve these errors, make sure you include the 'vue-resource' library in your main.js file:

import Vue from 'vue'
import VueResource from 'vue-resource';
import Actions from 'actions'
import App from './App'

Vue.use(Actions, {
  locales: ['en', 'zh', 'fr']
})

Vue.use(VueResource)
Vue.http.options.root = 'https://api.example.com'

new Vue({
  render: h => h(App),
  watch: {
    lang: function (val) {
      Vue.config.lang = val
    }
  }
}).$mount('#app')

Answer №2

If you're looking for a setup that utilizes axios instead of vue-resource, here is a configuration that has been effective for me:

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueRouter)
Vue.use(VueAxios, axios)

Vue.router = new VueRouter({
  // Define your routes here.
})

Vue.use(require('@websanova/vue-auth'), {
    auth: require('@websanova/vue-auth/drivers/auth/bearer.js'),
    http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
    router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
})

App.router = Vue.router

new Vue(App).$mount('#app')

If you need further assistance, I recommend checking out this informative tutorial: https://codeburst.io/api-authentication-in-laravel-vue-spa-using-jwt-auth-d8251b3632e0

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

Laravel mix is compiling the Vuetify styles in the sass file and generating an empty CSS file

I am having trouble compiling vuetify 2.0.0-beta.9 SASS using laravel mix. When I compile the styles.sass file, it generates an empty css file. How can I resolve this issue? Following the documentation (), I started by running: $ npm install sass sass-lo ...

Using the Facebook marketing API requires a valid Instagram account ID to be provided as a parameter

I've been exploring the capabilities of the Facebook Marketing API once again. After successfully creating Facebook ads using my Node.js app, I now have my sights set on Instagram. My current call to create an AdCreative looks like this: fb.api(&ap ...

How to display two elements side by side within a div using React

I have an array that looks like this: const arr = [1,2,3,4,5,6,7,8,9,10] I am looking to display the elements in pairs per line within two-dimensional divs. Here is what I have in mind: This represents the main React element: render() { return <di ...

Running code sequentially in a Node.js controller

Recently delved into the world of Node.js, and I'm currently tackling the challenge of running this code synchronously in my controller. Quest.js async function create(req, res, next) { const formValue = req.body['form']; ...

Adding a property conditionally in jsx: A guide

I have a simple example of Material UI RadioGroup code that I want to display conditionally in either a row or column format. By default, it is displayed in column format. Below is the code snippet: <RadioGroup aria-label="gender" name=&q ...

Facing delays in receiving responses for inner loop in node.js

Having an issue with retrieving data from the inner loop, as there is a delay in getting it. In my code, I am fetching the user list along with their ancestors from the SQL table. The ancestors are needed to verify their root values in the hierarchy/tree ...

Incorporating TypeScript basics into the if statement post compiling

As I delve into the Angular2 Quickstart, I stumbled upon a peculiar issue within app.component.js after compiling app.component.ts using tsc (version 1.8.2): if (d = decorators[i]) I am unable to pinpoint where I may have gone wrong in configuring the qu ...

Determining whether the user has a token stored in the localStorage is a crucial step in my

I have an app that calls a Login API and returns a token. I store the token in localStorage, but I'm unsure how to validate if the user has a token to log in. What steps can I take to solve this? Below is my login page where I store the token in loca ...

The HTML to PDF file converter API performs well in a local environment but encounters issues when deployed on node.Js, Express.Js, html-pdf, and Azure Web Services platform

I've developed an API that converts HTML to PDF, and it works flawlessly in my local environment but encounters issues when deployed on Azure app web services. During the process of generating the PDF, the request gets stuck and eventually times out, ...

Table Header Stays Put Without Shrinking or Expanding with Window Adjustment

I have a sticky table header that stays at the top when scrolling down on my web page. To achieve this effect, I followed the solution provided on css-tricks.com/persistent-headers/. However, I encountered an issue where the sticky table header does not ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: Here is a snapshot of my database: Below is my event controller class: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Event ...

Navigating an array to link values to anchor tags

I'm struggling with an array that contains image file names ["1352.jpg", "1353.jpg", "1354"]. My goal is to loop through this array and generate anchor links for each item separated by commas. I've attempted the following code snippet, but it&apo ...

The problem with asynchronous requests in Ajax

In the code below, I have noticed that when an alert() box is used and then clicked, the content loads on the page. However, without the alert() box, the content does not load. I've tried researching 'Ajax' requests extensively but still can ...

Tips for efficiently utilizing both client-server side rendering and static generation rendering in web development

I am looking to implement static generation for top products using getStaticProps. Currently, there are sections in my rendering that do not require static generation, such as comments and related products. Here is the full code snippet: export default fu ...

Tips on creating a button that, upon clicking, triggers the download of an Excel file using PHPSpreadsheet

I am trying to figure out how to create a button that, when clicked, will download an Excel file named b1b5.xls with some specified values added. Here is the code I have so far: <html> <head> </head> <body> <center> < ...

Issues with AngularJS Directives not functioning properly when elements are added dynamically through an AJAX request

I am facing a scenario where I have a page featuring a modal window that is created using the AngularUI Bootstrap modal directive. The DOM structure for this modal is being dynamically loaded from the server, and it is triggered to open when a specific but ...

Slideshow of table rows in HTML

On a webpage, I am populating an HTML table with a random number of rows ranging from 1 to 100. Regardless of the total number of rows, the requirement is to display only 10 rows at a time on the screen and then shift to the next set of 10 rows every 5 sec ...

Is it recommended to utilize a "default" key within an object?

Creating a JavaScript application and looking to establish a config object. Here's what I have so far: var config = { localization: { locales: ['en', ..., 'de'], defaultLocale: 'en' } } Consideri ...

Is there a way I can retrieve a cookie stored in an Express middleware even if the req.cookies object is returning empty?

I am currently working on validating a cookie using the cookie-parser module to verify if the user is authenticated to access restricted routes within my application. My technology stack includes NodeJS and Express for the server, and Sveltekit for the fro ...

Steps to successfully implement onClick functionality in html within C# server side code

I'm having trouble with my onClick function. When I click, nothing happens and there are no errors to help me diagnose the issue. var data = new FormData(); data.append("cart_list", new_cart); $.ajax({ url: "@Url.Action ...