【发布时间】:2017-05-13 18:33:17
【问题描述】:
我正在尝试将我的代码转换为更多插件类型的代码,因此所有内容都将分开,以防我将来更改类名。
出于某种原因,在我的代码中,我得到了Cannot read property 'dropdown' of undefined。
我的猜测是,函数Navigation.bindEvents()在我设置配置之前运行,所以它找不到它......但我不知道如何解决它。
这是我的 Navigation.js 文件:
let Navigation = {
config: {},
init(config) {
this.config = config;
this.bindEvents();
},
bindEvents() {
$(this.config.trigger).on('click', this.toggleNavigation);
$(document).on('click', this.hideAllDropdowns);
},
toggleNavigation(event) {
// Store the current visible state
var visible = $(this).siblings(this.config.trigger).hasClass('visible');
// Hide all the drop downs
this.hideAllDropdowns();
// If the stored state is visible, hide it... Vice-versa.
$(this).siblings(this.config.content).toggleClass('visible', !visible);
event.preventDefault();
event.stopPropagation();
},
hideAllDropdowns() {
$(this.config.dropdown + ' ' + this.config.content).removeClass('visible');
}
}
export default Navigation;
这是我运行所有 init 函数的 app.js 文件。
window.$ = window.jQuery = require('jquery');
import Navigation from './layout/navigation.js';
Navigation.init({
dropdown: '.dropdown',
trigger: '.dropdown-trigger',
content: '.dropdown-content'
});
【问题讨论】:
标签: javascript jquery ecmascript-6