私はコードをよりプラグインのタイプのコードに変換しようとしていますので、今後クラス名を変更する場合はすべてが分離されます。未定義のプロパティ 'ドロップダウン'を読み取ることができません
私のコードで何らかの理由でCannot read property 'dropdown' of undefined
と表示されます。
私の推測では、機能を設定する前にNavigation.bindEvents()
が実行されているので、見つけられません...しかし、それを解決する方法はわかりません。ここ
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です:
は、ここに私のNavigation.jsファイルです。
window.$ = window.jQuery = require('jquery');
import Navigation from './layout/navigation.js';
Navigation.init({
dropdown: '.dropdown',
trigger: '.dropdown-trigger',
content: '.dropdown-content'
});
...明らかに、その値は、あなたのメソッドが呼び出された場所に依存していることを意味し、動的である私は、それらの両方のためにそれを追加しました、これは、私がクリックしたところで、「未定義の適切なドロップダウンを読むことができない」という前の状況と比べて何もしません。 – Akar
まだエラーがありますか?そうでない場合は、あなたのクリックハンターに別の問題があると思います。 $(this.config.dropdown + 'this.config.content)の代わりに$(this.config.content)を使うべきでしょう –
私はそれを修正しました。これは 'this'の使用に関連する問題でした。クリックハンドラの中で 'this'はオブジェクトへの参照をしなくなりました。この場合は' Navigation'です。すべての 'this'ステートメントを' Navigation'に変更しました。私はこの答えをアップヴォーヴします。それは私にヒントを与えたからです。ありがとう。 – Akar