Rails 中的 window.autoInit 使用 material-components-web 和 webpacker [英] window.autoInit in Rails using material-components-web and webpacker
本文介绍了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 notwindow
inside the the pack file and the exports aren't being attached tothis
推荐答案
为了让 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屋!
查看全文