简体   繁体   English

从“凤凰”功能导入{Socket,Presence}不起作用。 凤凰/药剂

[英]import {Socket, Presence} from “phoenix” function not working. Phoenix/Elixir

I was building a simple chat app with phoenix and elixir. 我当时正在用phoenix和elixir构建一个简单的聊天应用程序。 One of the steps was to import Socket and Presence into the Javascript but I'm pretty sure that step is not working for reasons I have no idea about. 其中一个步骤是将Socket and Presence导入Javascript,但是我很确定该步骤由于我不知道的原因而无法正常工作。 I put this line at the top of my app.js : 我把这一行放在我的app.js的顶部:

import {Socket, Presence} from "phoenix"

When I debugged this line with debugger, I typed Presence in the console and it gave me a VM1416:1 Uncaught ReferenceError: Presence is not defined. 当我使用调试器调试此行时,我在控制台中键入Presence ,它给了我VM1416:1 Uncaught ReferenceError: Presence is not defined. Is there some setup that I could have missed? 有没有我可能会错过的设置? maybe with brunch or something? 也许是早午餐之类的?

The compiled code will be wrapped in an anonymous function, so the scope of Presence will not leak outside of the anonymous function. 编译后的代码将包装在匿名函数中,因此Presence的范围不会泄漏到匿名函数之外。

If you want Presence to be available globally, you can do something like: 如果您希望Presence在全球范围内可用,则可以执行以下操作:

import {Socket, Presence} from "phoenix"
window.Presence = Presence;

Then it will work in your console. 然后它将在您的控制台中工作。

You can read more about JavaScript scope in this answer https://stackoverflow.com/a/500459/219743 您可以在此答案中阅读有关JavaScript范围的更多信息https://stackoverflow.com/a/500459/219743

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM