简体   繁体   English

如何在Barba中使用module.exports

[英]How to use module.exports with Barba

I'm trying to split my barba views into different modules with module.exports. 我正在尝试使用module.exports将barba视图拆分为不同的模块。

This is the code i have for the view. 这是我查看的代码。

var Barba = require('barba.js');

var HomeView = Barba.BaseView.extend({
  namespace: 'homepage',

  onEnter: function() {

    console.log('intro start home');
      // The new Container is ready and attached to the DOM.
  },
  onEnterCompleted: function() {
      // The Transition has just finished.
      console.log('intro finished home');
  },
  onLeave: function() {
      // A new Transition toward a new page has just started.
      console.log('outro start home');
  },
  onLeaveCompleted: function() {
      // The Container has just been removed from the DOM.
      console.log('outro finished home');

  }
});

HomeView.init();

module.exports = HomeView;

And this is how i am creating the module 这就是我创建模块的方式

var homeView = new HomeView();

The error i keep getting is 'i is not a constructor'. 我不断收到的错误是“我不是构造函数”。 I do this the same way with backbone views and this works. 我对主干视图执行相同的操作,并且可以正常工作。

I believe you should not do var homeView = new HomeView(); 我相信你不应该做var homeView = new HomeView(); , instead import the module and do HomeView.init(); ,而是导入模块并执行HomeView.init(); . These barba.js view's doesn't look like constructor functions that you manually initialize, they seem to find elements with matching namespace like 这些barba.js视图看起来不像您手动初始化的构造函数,它们似乎在查找具有匹配名称空间的元素,例如

<div class="barba-container" data-namespace="homepage">

and create instances internally. 并在内部创建实例。 Follow the documentation, it doesn't tell you to do var homeView = new HomeView(); 请遵循文档,它不会告诉您执行var homeView = new HomeView();

我仅通过导入模块(而不创建新实例)进行拆分,并且可以正常工作:

import  HomeView from './your/path/HomeView'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM