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;
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;
Thanks for your example. I tried it, but still getting the same error If i declare ```var backEndUrl = `localhost:8090/api/auth/event```` it works. I dont understand, why i cant ouput the url correctly. Do u have any other solutions in your mind?
What change did you make exactly? If you're still setting the value on
window
inbackend-config.js
but never importing that file, that code will never run. Try addingimport "../../public/config/backend-config.js"
beforeexport default { ...window._backendconfig }
But, honestly, you're better getting rid of setting it onwindow
entirely if you're using modules. Part of the point of modules is avoid polluting the global scope.I just changed backendURLconfig.js. Okay i`ll try it!
What is the best solution in your opinion?
Best solution is to simplify. Honestly, if you don't need that config anywhere else, just define it in
sendDataToBackEnd.js
. But, let's say that's not what you want. OK, you should still get rid of your IIFE(function () { })()
where you set it on the window. Again, unless you really need that for some code you can't control that doesn't use modules. So then, instead ofexport default { ...window._backendconfig }
, you can just export the config directly that you were setting in the other file, instead of just accessing it from window and re-exporting.