使用materialize 1.0.0 gem,我不断收到“ Uncaught TypeError:sideNav”或“ dropdown is a function”,并恢复为实体化的0.100.2版本以使工作正常。我知道这是来自jquery的,无法实现应有的协作,因此,如果有人对此问题有任何见解,我将不胜感激,以便可以开始使用1.0.0。
这是一些代码。让我知道您是否需要更多。
application.js
//= require rails-ujs
//= require turbolinks
//= require activestorage
//= require jquery
//= require materialize
application / layouts / shared / _header.html.erb
<nav>
<div class="nav-wrapper">
<a href="#!" class="brand-logo">Logo</a>
<a href="#" data-target="mobile-demo" class="sidenav-trigger"><i class="material-icons">menu</i></a>
<ul class="right hide-on-med-and-down">
<li><a href="sass.html">Sass</a></li>
<li><a href="badges.html">Components</a></li>
<li><a href="collapsible.html">Javascript</a></li>
<li><a href="mobile.html">Mobile</a></li>
</ul>
</div>
</nav>
<ul class="sidenav" id="mobile-demo">
<li><a href="sass.html">Sass</a></li>
<li><a href="badges.html">Components</a></li>
<li><a href="collapsible.html">Javascript</a></li>
<li><a href="mobile.html">Mobile</a></li>
</ul>
<script>
$(document).ready(function(){
$('.sidenav').sidenav();
});
</script>
我还尝试过还原到旧版本的jquery-rails gem,但我应用中的其他gem至少依赖于4.2,并且似乎无法恢复到该版本。
答案 0 :(得分:1)
好吧,我做了一些进一步的研究,并在GitHub上发现了一篇帖子,该帖子帮助我意识到我错了我的问题,即jquery版本,我实际上需要在我的资产/ javascript文件夹中添加一个咖啡脚本。帮助事情发展。 https://github.com/mkhairi/materialize-sass/issues/176
这从来没有发生过,因为0.100.2从来不需要它。希望这对遇到相同问题的人有所帮助。
这是我添加的文件。
assets / javascripts / init.coffee
$(document).on 'turbolinks:before-visit turbolinks:before-cache', ->
elem = document.querySelector('#slide-out');
instance = M.Sidenav.getInstance(elem) if elem
if instance
instance.close() if instance.isOpen #close on click
instance.destroy()
$(document).on 'turbolinks:before-visit turbolinks:before-cache', ->
$('.toast').remove()
$(document).on 'turbolinks:load', ->
elem = document.querySelector('#slide-out');
instance = new M.Sidenav(elem, {}) if elem
$(document).on 'turbolinks:load', ->
Waves.displayEffect()
# Reset
M.Modal._count = 0;
M.ScrollSpy._count = 0;
$('.modal').modal();
$('.carousel').carousel();
$('.collapsible').not('.expandable').collapsible();
$('.collapsible.expandable').collapsible({accordion: false});
$('.tap-target').tapTarget();
$('.slider').slider();
$('.parallax').parallax();
$('.materialboxed').materialbox();
$('.scrollspy').scrollSpy();
$('.datepicker').datepicker();
$('.timepicker').timepicker();
$('.tooltipped').tooltip();
$('.dropdown-trigger').dropdown();
#form
M.updateTextFields()
$('select').not('.disabled').formSelect();
$('input.autocomplete').autocomplete data:
'Apple': null
'Microsoft': null
'Google': 'http://placehold.it/250x250'
$('input[data-length], textarea[data-length]').characterCounter();
$('.tabs').tabs();
# Swipeable Tabs Demo Init
if $('#tabs-swipe-demo').length
$('#tabs-swipe-demo').tabs 'swipeable': true
# Chips
# Handle removal of static chips.
$(document.body).on 'click', '.chip .close', ->
$chips = $(this).closest('.chips')
if $chips.length and $chips[0].M_Chips
return
$(this).closest('.chip').remove()
$('.chips').chips()
$('.chips-initial').chips
readOnly: true
data: [
{ tag: 'Apple' }
{ tag: 'Microsoft' }
{ tag: 'Google' }
]
$('.chips-placeholder').chips
placeholder: 'Enter a tag'
secondaryPlaceholder: '+Tag'
$('.chips-autocomplete').chips autocompleteOptions: data:
'Apple': null
'Microsoft': null
'Google': null
# Fab
$('.fixed-action-btn').floatingActionButton()
$('.fixed-action-btn.horizontal').floatingActionButton direction: 'left'
$('.fixed-action-btn.click-to-toggle').floatingActionButton
direction: 'left'
hoverEnabled: false
$('.fixed-action-btn.toolbar').floatingActionButton toolbarEnabled: true
console.log "load init on ready or turbolinks:load"
这很容易解决。 Sidenav和下拉菜单现在很棒!