简体   繁体   English

在Rails中使用material-components-web和webpacker在window.autoInit中

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

I'm using Rails 5.1.4 with the code below. 我在下面的代码中使用Rails 5.1.4。 I have confirmed that the material.js file is being processed and the material-components-web module is downloaded and discovered. 我已经确认正在处理material.js文件,并且已经下载并发现了material-components-web模块。

Why is mdc undefined when calling window.mdc.autoInit() ? 为什么在调用window.mdc.autoInit()时未定义mdc

// 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. 分别导入@material/auto-init
  • Putting the autoInit call inside an onload autoInit调用放入onload中
  • Adding a console.dir(window) / console.dir(this) inside pack file. 在包文件中添加console.dir(window) / console.dir(this) this is not window inside the the pack file and the exports aren't being attached to this this不是打包文件中的window ,并且导出未附加this

To make material-components-web work with Rails I did 为了使material-components-web与Rails一起工作,我做了

// 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 然后确保在主体的结尾处调用了window.mdc.autoInit()

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

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