Phoenix 通道 polyfill

Phoenix channels polyfills

当我浏览客户端通道代码(在 phoenix.js 文件中)时,我看到它使用 ES6。示例代码:

let chan = socket.chan("rooms:123", {token: roomToken})
//     chan.on("new_msg", msg => console.log("Got message", msg) )
//     $input.onEnter( e => {
//       chan.push("new_msg", {body: e.target.val})
//           .receive("ok", (message) => console.log("created message", message) )
//           .receive("error", (reasons) => console.log("create failed", reasons) )
//           .after(10000, () => console.log("Networking issue. Still waiting...") )

this.onError( reason => {
      this.socket.log("channel", `error ${this.topic}`, reason)
      this.state = CHAN_STATES.errored
      this.rejoinTimer.setTimeout()
    })

这意味着它不会 运行 在 IE 和 Safari 中原生(至少吃)。我不应该使用某种 polyfill 吗? 什么是最好的 approach/polyfill? 另外,我的印象是 polyfills 覆盖 classes/let/... 但不是箭头 functions/new 字符串插值。我应该自己更改吗?

由于 ES6 向该语言添加了新语法,因此无法填充箭头函数。

但是,在创建新应用程序时,Phoenix 会安装一个名为 Brunch which is used for combining assets. It includes a wrapper for Babel 的库,它将 ES6 转换为 JavaScript,从而在浏览器中 运行。

如果您查看 priv/static/app.js(编译输出)而不是 web/static/app.js(源代码),那么您会发现它没有新的 ES6 语法。

如果您使用某些功能,您可能会发现一件事,那么您可能需要包括 babel-polyfill.js,您可以在 https://babeljs.io/docs/advanced/caveats/

阅读有关内容

这是在 Phoenix 0.10.0 中引入的,您可以在公告中阅读更多相关信息 post http://www.phoenixframework.org/v0.14.0/blog/phoenix-0100-released-with-assets-handling-generat