window.autoInit in Rails using material-components-web and webpacker

可紊 提交于 2020-01-06 07:01:44

问题


I'm using Rails 5.1.4 with the code below. I have confirmed that the material.js file is being processed and the material-components-web module is downloaded and discovered.

Why is mdc undefined when calling window.mdc.autoInit()?

// app/javascript/packs/material.js
import 'material-components-web'

// app/views/layouts/application.html.eeb
<html>
  <head>
    <%= javascript_pack_tag 'material' %>
  </head>
  <body>
    <script>window.mdc.autoInit();</script>
  </body>
</html>

I have tried

  • importing @material/auto-init separately.
  • Putting the autoInit call inside an onload
  • Adding a console.dir(window)/console.dir(this) inside pack file. this is not window inside the the pack file and the exports aren't being attached to this

回答1:


To make material-components-web work with Rails I did

// app/javascript/packs/material.js
import * as mdc from 'material-components-web'
// for accessing it as a window object
window.mdc = mdc

And then I made sure window.mdc.autoInit() was called at the end of the body



来源:https://stackoverflow.com/questions/47528242/window-autoinit-in-rails-using-material-components-web-and-webpacker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!