我有一个在Elm中运行的应用程序,我需要在应用程序在 "IE兼容 "模式下加载前向用户显示一些提示msg,因为我不打算支持,特定的IE兼容模式只支持。我需要在应用程序在 "IE兼容 "模式下加载前向用户显示一些提示msg,因为我不打算支持,特定于IE兼容模式。
使用JavaScript,我可以做到
navigator.userAgent.indexOf('compatible') > -1 ? true : false
但是,在ELm中是否有办法做到这一点,就像第一件事一样。Main.init
?
我曾试过使用Elm port,但应用程序在到达端口之前就失败了,这就是为什么在Elm代码运行之前就必须检查浏览器的兼容性。
谢谢你
一种方法可以是使用 旗帜. 标志是在你第一次从JavaScript初始化应用时,可以传入Elm的值。
var app = Elm.Main.init({
node: document.getElementById('myapp'),
flags: {
compatibilityMode: navigator.userAgent.indexOf('compatible') > -1
}
});
而在Elm这边则被传递给了... init
职能。
type alias Model = { compatibilityMode : Bool, ... }
type alias Flags = { compatibilityMode : Bool }
init : Flags -> ( Model, Cmd Msg )
init flags =
( { compatibilityMode = flags.compatibilityMode, ... }
, Cmd.none
)
...
main : Program Int Model Msg
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}