简体   繁体   English

如何将变量从js传递给pug

[英]How to pass variable from js to pug

My problem is that I cannot seem to modify the variable that the pug file uses to loop.我的问题是我似乎无法修改 pug 文件用于循环的变量。

section.p-0.md-p-5.muli
    .flex.flex-wrap
      script.
        var users2 = '#{users}';
        console.log(users2);
        /*
        const searchBar = document.getElementById('searchBar');
        searchBar.addEventListener('keyup', (e) => {
          const search = e.target.value;
          const filteredFiles = users2[0].filter(user => {
            return user.toString[0][0].includes(search);
          })
          console.log(filteredFiles);
        });
        */
      each user in users2
        .w-100pc.md-w-33pc.p-10
          a.block.no-underline.p-5.br-8.hover-bg-indigo-lightest-10.hover-scale-up-1.ease-300.filePage(href=`/${user.slug}`)
            img.w-100pc(src=`${user.photoUrl[0]}` alt='')
            p.fw-600.white.fs-m3.mt-3
              | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed…
            .indigo.fs-s3.italic.after-arrow-right.my-4 3 days ago by Jeff

In the 3 line I have a script tag that sets a passed in array of objects.在第 3 行中,我有一个脚本标记,用于设置传入的对象数组。 Than I want to modify those objects and pass the new filtered array to loop on line 16. However, I do not know how to pass the variable from line 4 to line 16. When I run the code I get the error "Cannot read property 'length' of undefined".比我想修改这些对象并将新的过滤数组传递给第 16 行的循环。但是,我不知道如何将变量从第 4 行传递到第 16 行。当我运行代码时,我收到错误“无法读取属性未定义的“长度”。 Any ideas on how to fix this?有想法该怎么解决这个吗? Thanks.谢谢。

At all times, you must be very clear about what is on the server and what is on the client .在任何时候,您都必须非常清楚服务器上的内容和客户端上的内容。

The server (ie Pug) computes and generates the Html output.服务器(即 Pug)计算并生成 Html output。 At this server stage, there is no browser in existence.在这个服务器阶段,不存在浏览器。 Any variables used are variables at the server.使用的任何变量都是服务器上的变量。

When a user launches a browser to access your page, the above generated Html output is received by the browser and any script commands in it then run in the browser.当用户启动浏览器访问您的页面时,上面生成的 Html output 会被浏览器接收,然后其中的任何脚本命令都会在浏览器中运行。 There is no existence of Pug at this time.此时不存在帕格。

users2 , contained in a <script> block, is a client side variable. users2包含在<script>块中,是客户端变量。 It does not exist when Pug is running. Pug 运行时不存在

users is a server side variable, and is thus available to the Pug code. users是服务器端变量,因此可用于 Pug 代码。

The error message code can be fixed with a simple change: each user in users .可以通过简单的更改来修复错误消息代码: users 中的each user in users

//-Your `users` object is a SERVER variable.

script.
  var users2 = '#{users}';
  console.log(users2);
  //-The above, other than the substitution of #{users}, DO NOT RUN at the server

//-The below runs ONLY at the server, thus object `users` is available
section.p-0.md-p-5.muli
  .flex.flex-wrap
    each user in users
      .w-100pc.md-w-33pc.p-10
        a.block.no-underline.p-5.br-8.hover-bg-indigo-lightest-10.hover-scale-up-1.ease-300.filePage(href=`/${user.slug}`)
        img.w-100pc(src=`${user.photoUrl[0]}` alt='')
        p.fw-600.white.fs-m3.mt-3
          | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed…
        .indigo.fs-s3.italic.after-arrow-right.my-4 3 days ago by Jeff

Fortunately or unfortunately, you didn't use the exact same name for the client variable.幸运或不幸的是,您没有为客户端变量使用完全相同的名称。 It may confuse you thoroughly, or it may enlighten you completely.它可能会让你彻底迷惑,也可能完全启发你。 For example, you could have written the following and it will still work fine:例如,您可以编写以下内容,它仍然可以正常工作:

script.
  let users = '#{users}';
  console.log(users);

section
  each user in users
    img(src=user.photoUrl[0])

OK, now to your requirement to modify users .好的,现在您需要修改users You can't if you are thinking of letting the client modify it and then returning to Pug to use the updated value.如果您想让客户端修改它,然后返回 Pug 以使用更新的值,则不能。 After the browser receives the Html output, it disconnects from the server.浏览器收到 Html output 后,与服务器断开连接 Similarly, when the Pug is generating the Html output, the browser does not exist.同样,当Pug在生成Html output时,浏览器不存在。 There is zero interaction between the Pug engine and the browser's Javascript engine! Pug引擎和浏览器的Javascript引擎之间是零交互的!

The commented out code in your client code does not reveal how you want to modify users .您的客户端代码中注释掉的代码不会显示您希望如何修改users You should post another question to describe the interaction you want between browser and server.您应该发布另一个问题来描述您想要的浏览器和服务器之间的交互。 The answer will probably require Ajax calls or a form submission.答案可能需要 Ajax 调用或表单提交。

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

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