I'm not quite sure what is going on with my code. I think that Webpack for some reason is setting the value of this
to undefined
I have no idea why.
The webpack file builds properly - but if I console.log(this) in my app.js I get undefined - Is there something I am missing?
this
should refer to the window and then the Controller where I log it in setVars ?
Here is my webpack file
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require('path'
const entry = [path.resolve(__dirname, 'public/js/app.js'), path.resolve(__dirname, 'public/styles/scss/main.scss')];
module.exports = {
entry: entry,
output: {
filename: 'scripts/bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
}
},
{
test: /\.css$/,
use: [
"style-loader", "css-loader"
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", "sass-loader?"
]
}]},
plugins: [
new MiniCssExtractPlugin({
filename: "styles/[name].css",
chunkFilename: "[id].css"
}),
// new HtmlWebpackPlugin()
]
}
And Babel RC
{
"presets": ["@babel/preset-env"]
}
import axios from 'axios';
const Controller = {
init: async () => {
const res = await axios.get('/api/nav.json');
const data = res.data.items;
Controller.createNavigationMenu(data);
Controller.getVars()
},
getVars: () => {
const navigationContainer = document.querySelectorAll('.navigation__primary-menu');
console.log(navigationContainer, this)
},
createNavigationMenu: (data) => {
console.log(data)
}
}
Controller.init()
console.log(this)