所以我很难在app.js中编写Javascript实际工作。这是我到目前为止所做的:
我做的第一件事就是将代码编写到app.js文件中,希望它可以正常工作。
App.js
import "deps/phoenix_html/web/static/js/phoenix_html"
const selectSort = document.getElementById("sort");
console.log("Herp");
const sortEntities = () => {
const sortvalue = selectSort.value;
const entities = document.querySelectorAll(".entitiy");
const sortedEntities = entities.sort((a, b) =>
a.querySelector(`.${sortvalue}`).textContent > a.querySelector(`.${sortvalue}`).textContent);
document.querySelector(".entitiy-list").innerHTML = sortedEntities;
};
selectSort.addEventListener("onChange", sortEntities);
现在我尝试通过控制台访问selectSort变量,但我得到了未定义。我发现app.js从未加载过,因为我运行windows并且可以通过在autoRequire中添加另一行来修复它。
早午餐-config.js
<---SNIPPET-->
modules: {
autoRequire: {
"js/app.js": ["web/static/js/app"],
"js\\app.js": ["web/static/js/app"] <-- The line added
}
},
<---SNIPPET-->
既然app.js文件最终正确加载,我的phoenix_html导入错误。
错误
Uncaught Error: Cannot find module "deps/phoenix_html/web/static/js/phoenix_html" from "web/static/js/app"
即使我删除了导入,当我在控制台中输入变量时,变量仍然无法正确加载,并且代码永远不会执行,而执行console.log消息时。我错过了什么?如果我尝试在控制台窗口中编写代码,它可以正常工作。
项目本身很老,可能来自11月,所以我可能不会运行最新的“凤凰模板”。不确定是否有一些与此相关的已知问题,我试着环顾四周。我甚至有“编程凤凰”,但没有太多关于让Javascript在那里运行。
答案 0 :(得分:0)
我的项目使用了Phoenix 1.0,它已知Windows的问题并使Javascript正确执行。我升级到1.1.2现在一切正常。