Rails 中的 window.autoInit 使用 material-components-web 和 webpacker [英] window.autoInit in Rails using material-components-web and webpacker

查看:27
本文介绍了Rails 中的 window.autoInit 使用 material-components-web 和 webpacker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Rails 5.1.4 和下面的代码.我已经确认正在处理 material.js 文件,并且下载并发现了 material-components-web 模块.

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.

为什么调用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>

我试过了

  • 单独导入@material/auto-init.
  • autoInit 调用放入 onload
  • 在包文件中添加console.dir(window)/console.dir(this).this 不是包文件中的 window 并且导出没有附加到 this
  • 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

推荐答案

为了让 material-components-web 与 Rails 一起工作,我做到了

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

然后我确保 window.mdc.autoInit() 在正文的末尾被调用

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

这篇关于Rails 中的 window.autoInit 使用 material-components-web 和 webpacker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆