Warm tip: This article is reproduced from serverfault.com, please click

BackEndUrl is undefined

发布于 2020-11-28 00:51:16

I created a plugin, which should post some data to my backend. I tried to set up some backend url config. I checked the URl within my plugin with "console.log(...)" as u can see withn my code (in sendDataToBackEnd.js). But i getting following output "undefined".This is the error: "Error message: Cannot read property 'backEndUrl' of null"

Project structure:

project1
   public
     backend-config.js
     faviocon.ico
     index.html
   src
     App.vue
     main.js
     config
       backEndUrlConfig.js
     plugin
       sendDataToBackEnd.js

Therefore I created backend-config.js within in Folder "public"

(function (window) {

    window._backendconfig = {
        urlBackend: `http://localhost:8090/api/auth/event`,
    }

}(this));

My config.js looks like this:

export default { ...window._backendconfig }

And my PLugin "sendDataToBackEnd.js" looks like this:

import url from '../../config/backendURLconfig';

var backEndUrl = url.urlBackend;
console.log(backEndUrl)

const sendDatatoBackEnd = {}

sendDataToBackEnd.install = function (Vue){  
    {Method to send Data to my Backend}
}

export default sendDatatoBackEnd;
Questioner
Nilo Meier
Viewed
0
Matthew Brooks 2020-11-28 09:45:49

You're mixing using global scope JS (setting a property on window) in your config file with module style JS in your sendDataToBackEnd.js file (importing and exporting from modules).

You either need to export something from config (best option if you're using modules), or just access it from the window.

backendURLconfig.js

const config = {
  urlBackend: `http://localhost:8090/api/auth/event`,
}

export default config

sendDataToBackEnd.js

import config from '../config/backendURLconfig';

var backEndUrl = config.urlBackend;

console.log(backEndUrl)

const sendDatatoBackEnd = {}

vuePlugin.install = function (Vue){  
    {Method to send Data to my Backend}
}

export default sendDatatoBackEnd;