1

当我浏览客户端通道代码(在 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 中本地运行(至少)。我不应该使用某种 polyfills 吗?什么是最好的方法/polyfill?另外,我的印象是 polyfills 涵盖类/let/...但不包括箭头函数/新字符串插值。我应该自己改变那些吗?

4

1 回答 1

3

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

但是,在创建新应用程序时,Phoenix 会安装一个名为Brunch的库,用于组合资产。它包括一个用于Babel的包装器,它将 ES6 转换为将在浏览器中运行的 JavaScript。

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

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

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

于 2015-07-21T10:47:11.333 回答