Как создать глобальную переменную с помощью веб-пакета из модуля? - PullRequest
0 голосов
/ 15 февраля 2019

Я создаю простое приложение с ecma6 и пытаюсь создать глобальные переменные с помощью webpack.ProvidePlugin, но у меня возникают проблемы с доступом к переменным из моих модулей.

Мой файл веб-пакета:

new webpack.ProvidePlugin({
  $: 'jquery/dist/jquery.js',
  jQuery: 'jquery/dist/jquery.js',
  $chord: [path.resolve(__dirname, 'framework/chord.js'), 'Chord'],
  $route: [path.resolve(__dirname, 'framework/router/router.js'), 'Route']
})

Мой app.js:

console.log('app.js')
console.log($chord)
console.log($route)
$chord.init()

Мой chord.js:

export const Chord = {
  globalModules: {},

  initGlobalModules (modules) {
    this.globalModules = modules
  },

  render (CurrentView, CurrentModule) {
    //here I put the imported module in the window (in this example the home)
    $('#app').html(CurrentView);
    window.home = CurrentModule;
  },

  init () {
    // so far the variables work on the console
    console.log('chord.js')
    console.log($route)
    this.render($route.routes.root.view, $route.routes.root.module);
  }
}

Мой home.js

export default {
  msgHome () {
    console.log('Welcome to Home');
  },

  navigate () {
    // here the global variables not working in console
    console.log('home.js')
    console.log($chord)
    console.log($route)
  }
}

после запуска кода ивызовите метод window.home.navigate, который отображается на консоли как неопределенный.должен показывать глобальный модуль, определенный в веб-пакете.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...