简体   繁体   English

如何将变量从 EJS 页面传递到另一个 EJS 页面

[英]how can i pass a variable from an EJS page to another EJS page

Hello你好
how can i pass a variable from an EJS page to another EJS page.如何将变量从 EJS 页面传递到另一个 EJS 页面。
For example, I have this page例如,我有这个页面

    // insertion.ejs //




<!DOCTYPE html>
            <html lang=”en”>
            <head></head>
            <header>
                <% include ../views/header %>
            </header>
            <body>
            <script>
            var a="hello";
            </script>
            </body>
        </html>

And this page还有这个页面

 // get.ejs //



        <!DOCTYPE html>
            <html lang=”en”>
            <head></head>
            <header>
                <% include ../views/header %>
            </header>
            <body>
            <script>
            var b 
            </script>
            </body>
         </html>

So how can I pass the content of the variable a to the variable b那么如何将变量 a 的内容传递给变量 b

If you mean to pass the value when a user clicks a link and goes from one page to the other, then you can use a query parameter and have a link like:如果您打算在用户单击链接并从一个页面转到另一个页面时传递该值,那么您可以使用查询参数并具有如下链接:

<a href="page2?name=value">link</a>

where name is the name of the query parameter and value is its value, that could be populated by the EJS template with an appropriate data.其中name是查询参数的名称, value是它的值,它可以由 EJS 模板用适当的数据填充。

Now you could get that parameter in your route handler and pass it to the template with other variables or you could access it by the client-side JavaScript on the page.现在,您可以在路由处理程序中获取该参数并将其与其他变量一起传递给模板,或者您可以通过页面上的客户端 JavaScript 访问它。

If you want to pass the variable in a sense that you want to have that variable available to a user then you need to use some session storage, a cookie or local storage.如果你想在某种意义上传递变量,你想让该变量可供用户使用,那么你需要使用一些会话存储、cookie 或本地存储。

If you want to pass the variable from one page to the other all users just to reduce duplication then you should use include to include that in all pages that need it or to pass it by the handlers.如果您想将变量从一个页面传递给其他所有用户只是为了减少重复,那么您应该使用include将其包含在所有需要它的页面中或由处理程序传递它。

This is not very clear from your question what is your intention but this is more or less what you can do in all scenarios.从您的问题中这不是很清楚您的意图是什么,但这或多或少是您在所有情况下都可以做的。

You can use LocalStorage concept.您可以使用 LocalStorage 概念。 Here is the explanation how to use local storage.以下是如何使用本地存储的说明。 https://www.w3schools.com/html/html5_webstorage.asp https://www.w3schools.com/html/html5_webstorage.asp

Well you can model binding in one page, but not two separate page.好吧,您可以在一页中对绑定进行建模,但不能在两个单独的页面中进行建模。 For this purpose you have cook up your own recipe, you have two novel ways of doing this, first use socket.io , with this method let each page publish and subscribe to events, whenever one page emits a event, the other automatically receives it and vice versa.为此,您编写了自己的食谱,您有两种新颖的方法可以做到这一点,首先使用 socket.io ,使用此方法让每个页面发布和订阅事件,每当一个页面发出事件时,另一个页面会自动接收它反之亦然。 The alternative approach is to utilize node local storage and cache system modules.另一种方法是利用节点本地存储和缓存系统模块。 For local storage you can memcache, node-cache just do a npm search.对于本地存储,您可以使用 memcache,node-cache 只需执行 npm 搜索。 For a fast and direct caching use redis,redis in-memory data structure store, used as a database, cache and message broker.对于快速和直接缓存使用 redis,redis 内存数据结构存储,用作数据库、缓存和消息代理。 It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries.它支持诸如字符串、散列、列表、集合、带有范围查询的排序集合等数据结构。

Happy Coding :):快乐编码:):

  1. Firstly, create an HTML tag in which you want to pass a value example:首先,创建一个 HTML 标记,您要在其中传递一个值示例:
     <p> <%= startingContent %>  </p> 

(Here startingContent is a key created in page where you want your data). (这里的startingContent是在您想要数据的页面中创建的键)。

  1. in your xx.js file from where you want to export your data use Key: value pair)在您要从中导出数据的 xx.js 文件中使用 Key: value 对)
  app.get("/", function(req, res) {
  res.render("home", {startingContent: homeStartingContent});

startingContent: is the Key you identified in page where you want your data. startingContent:是您在您想要数据的页面中确定的密钥。

homeStartingContent: is the variable of which you wanted the value to be displayed. homeStartingContent:是您希望显示其值的变量。

Happy Coding!快乐编码!

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

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