gpt4 book ai didi

javascript - $ 未定义 (Node+Express+Pug+JQuery)

转载 作者:行者123 更新时间:2023-11-30 14:23:26 25 4
gpt4 key购买 nike

肯定是一个新手问题,但我很感激任何帮助,因为我完全被困住了(即,我尝试了我能找到的一切但没有成功)。当我尝试使用 Jquery 时,出现“ReferenceError: $ is not defined”。我的项目结构是:


bin/www
public
javascripts
stylesheets
routes
index.js
views
layout.pug
...
app.js

layout.pug,我尝试加载 CDN jquery 的地方是:

    doctype html
html(lang='en')
head
meta(charset='utf-8')
title= title
//- bootstrap CSS
link(rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'
integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous')
link(rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css')

//- my Styling CSS
link(rel='stylesheet', href='/stylesheets/style.css')

body

block content
script(src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js")
script(src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous")
script(src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'
integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous')

script(src='/javascripts/jquery-3.3.1.js')
script(src='routes/index.js')

我的 index.js 文件如下:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});

$(function () {
alert('JavaScript Loaded!');
});

module.exports = router;

最后,在我的 app.js 文件中有一些相关的行:

const indexRouter = require('./routes/index');
const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// Set Static Paths
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);

谁能帮帮我?

最佳答案

我认为您不知道 server-side-renderingclient-side-rendering

Expressjs 是服务器端渲染。所以你不要在 index.js 中调用 jQuery

如果你调用 jQuery,你就这样做了。 var $ = require('jQuery');

引用:https://www.npmjs.com/package/jQuery#nodejs

关于javascript - $ 未定义 (Node+Express+Pug+JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com