简体   繁体   English

用ejs的客户端javascript文件表达

[英]Express with ejs's client side javascript file

recently I am trying to create a website using express and ejs and I found out that I can not just use one javascript file for all my ejs (view) files because when I target a DOM element to do something and if other view pages doesn't have that DOM element I'll get a error in my console.最近我正在尝试使用 express 和 ejs 创建一个网站,但我发现我不能只将一个 javascript 文件用于我的所有 ejs(视图)文件,因为当我以 DOM 元素为目标时,如果其他视图页面没有没有那个 DOM 元素我会在我的控制台中得到一个错误。

Example: (So if am visiting index page, I am fine but when I am on about page I got an error because that element with that class doesn't exist on the about page)示例:(因此,如果正在访问索引页面,我很好,但是当我在 about 页面上时出现错误,因为带有 class 的元素在 about 页面上不存在)

index.ejs索引.ejs

<div class="main-container">
    <h1 class="home-heading">Home page</h1>
    <p>some random text</p>
</div>

about.ejs关于.ejs

<div class="main-container">
    <h1 class="about-heading">About page</h1>
    <p>some random text</p>
</div>

script.js脚本.js

const homeHeading = document.querySelector('.home-heading');
homeHeading.style.backgroundColor = 'red'; 

So my question is what is a best way to link client side javascript file?所以我的问题是链接客户端 javascript 文件的最佳方法是什么? The only way I can think of is creating multiple js files and make it so each ejs page has its own js file, is this best practice?我能想到的唯一方法是创建多个 js 文件并使其每个 ejs 页面都有自己的 js 文件,这是最佳实践吗? or is there any other ways I can make it work using just one js file?或者有没有其他方法可以让我只使用一个js文件来让它工作?

Right now this is what I came up:现在这就是我想出的:

on view page(home), I'll create a variable with value of index and pass down to footer(partial)在视图页面(主页)上,我将创建一个值为 index 的变量并传递给页脚(部分)

index.ejs索引.ejs

<%- include('../partials/header'); -%>
<h1>Hello world</h1>
<a href="/about" >about</a>
<% const currentPage = "index"; %>
<%- include('../partials/footer',{currentPage: currentPage}); -%>

footer.ejs页脚.ejs

  </div>
  <%- include('../partials/scriptFiles',{currentPage: currentPage}); -%>
 </body>
</html>

and I'll pass the variable down again to scriptFiles.ejs(partial) to check which page I am on and output that js tag with correct file path我将再次将变量向下传递给 scriptFiles.ejs(partial) 以检查我在哪个页面上以及 output 那个带有正确文件路径的 js 标记

scriptFiles.ejs脚本文件.ejs

<% let currentScriptFile = ''; %>

   <% if(currentPage === 'index'){ %>
      <% currentScriptFile = '<script src="./script.js"></script>'; %>
   <% }else if(currentPage === 'about') { ; %> 
      <% currentScriptFile = '<script src="./about.js"></script>'; %>
   <% } %>

<%- currentScriptFile %> //Output script tag with correct file path

so when I am on home page it will use./script.js and when I am on about page it will use./about.js, to use express with ejs is this a correct way to do it?因此,当我在主页上时,它将使用./script.js,而当我在关于页面上时,它将使用./about.js,将express与ejs一起使用是正确的方法吗? if not what is the best way or correct way to do it.如果不是最好的方法或正确的方法是什么。

Thanks谢谢

So if am visiting index page, I am fine but when I am on about page I got an error because that element with that class doesn't exist on the about page因此,如果我正在访问索引页面,我很好,但是当我在关于页面上时,我收到一个错误,因为该类的元素在关于页面上不存在

Write the JavaScript robustly so it can cope with that state.健壮地编写 JavaScript,以便它可以应对这种状态。

const duck = document.querySelector(".duck");
if (duck) {
    // The element exists and you can do stuff with it.
}

I'm really struggling with these, you know, i use a lot axios, and wanted to do custom scripts for my ejs for the front-end, but i can't get them to work, and I don't think this is the best way to do it as suggested in the answer because I don't even have an answer once requested the ejs template in my case login, but, i guess that's it for now, locally.我真的在为这些而苦苦挣扎,你知道,我使用了很多 axios,并想为我的前端 ejs 做自定义脚本,但我无法让它们工作,我不认为这是最好的方法是按照答案中的建议进行操作,因为一旦在我的案例登录中请求了 ejs 模板,我什至没有答案,但是,我想现在就是这样,在本地。

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

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