【问题标题】:Express js render pug|ejs file throws Jquery errorExpress js 渲染 pug|ejs 文件抛出 Jquery 错误
【发布时间】:2017-06-16 19:06:22
【问题描述】:

我遇到了 ExpressJs 渲染和 Jquery 的问题。

我的 app.js(主文件)是

restapi=require('express')();
const pug = require('pug');
restapi.use(express.static(__dirname+"/public"));
restapi.set('views', __dirname+'/views');
restapi.set('view engine','pug');
restapi.get("/view/:datalog?",(req, res)=>{
  var datalog=req.params.datalog;
  res.render("index",{title:"Hey",rows:"hello"});

})

我的 index.pug 文件(我也一直在使用 EJS)是

 html
  head
   title= title
   script(src='/js/jquery-3.1.1.min.js')
   script.
     window.$ = window.jQuery = module.export;
     $().ready(function(){console.log("READY")});
     (alert("HELOOOOOOOOOOOOOOOOO"))
   body
     rows

它给我一个找不到 jquery 模块的错误 如果我删除window.$ = window.jQuery = module.export(我尝试过使用= require("jquery")而不是模块导出),简单的javascript会向我显示警报。

我什至尝试将 jquery 脚本链接到 cdn 存储库。它总是让我出错。

我什至尝试将所有文​​件移动到没有公共/静态分配的根文件夹。

似乎没有任何效果... 我错过了什么?!

【问题讨论】:

    标签: jquery express pug electron ejs


    【解决方案1】:

    你检查/js/jquery-3.1.1.min.js是你的jQuery文件的有效路径吗?

    然后只需使用下面的简洁语法:

    html
      head
       title= title
       script(src='/js/jquery-3.1.1.min.js')
       script.
         jQuery(function ($) {
           alert('ready');
         });
       body
         rows
    

    【讨论】:

    • 据我所知,似乎使用express和render,它不会使用诸如require等服务器端(节点js)功能。所以我把所有的逻辑都移到了主 js 文件和渲染文件中,只是为了视图。
    猜你喜欢
    • 2021-11-13
    • 2017-12-20
    • 2016-11-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2021-04-27
    • 2017-08-09
    相关资源
    最近更新 更多