相关疑难解决方法(0)

ExpressJS将变量传递给JavaScript

我完全迷失了; 我正在使用NodeJS来获取JSON,我需要将变量传递给我的页面并让JavaScript使用数据.

app.get('/test', function(req, res) {
    res.render('testPage', {
        myVar: 'My Data'
    });
Run Code Online (Sandbox Code Playgroud)

这是我的Express代码(非常简单,用于测试目的); 现在使用JADE我想收集这些我知道要在页面上呈现的数据

p= myVar
Run Code Online (Sandbox Code Playgroud)

但是我需要能够在JavaScript中收集这些数据(如果可能的话,在.js文件中),但现在只是在Alert框中显示变量我试过了

alert(#{myVar})
Run Code Online (Sandbox Code Playgroud)

还有许多其他人如果有人可以帮助我们非常感激

javascript node.js express

14
推荐指数
2
解决办法
9726
查看次数

JADE + EXPRESS:在内联JS代码(客户端)中迭代对象?

我想基于它的api实现谷歌地图.我想基于坐标添加路径.因此,我从我的模型中获取坐标,并希望迭代对象以使用此点填充地图.在我的玉模板中,我包含这样的api js代码:

script(type='text/javascript')
    function initialize() {
      var myLatLng = new google.maps.LatLng(0, -180);
      var myOptions = {
        zoom: 3,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };

      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      var flightPlanCoordinates = [

       - if (typeof(pins) != null)
           - var.pins.forEach(function(pin) {
                new google.maps.LatLng(pin.latitude, pin.longitude),
           - })
           new google.maps.LatLng(0,0)
      ];
      var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
      });

      flightPath.setMap(map);
    }

div#map_canvas(style='height: 500px; background-color: #990000;')
Run Code Online (Sandbox Code Playgroud)

问题是:jade渲染这个片段

var flightPlanCoordinates = [

       - if (typeof(pins) != null)
           - …
Run Code Online (Sandbox Code Playgroud)

javascript templates node.js express pug

10
推荐指数
2
解决办法
1万
查看次数

从 Vue 脚本中获取 Pug/Jade mixin 参数

是否可以将 mixin 参数绑定到 vue 实例?

我有这个 mixin 来渲染表格:

mixin table(header,data,type)
- var type = type || ""
table.table(class=type)
    thead
        tr
            each head, i in header
                th #{head}
    tbody
        each row,j in data
            tr
                each col,k in row
                    td #{col}
Run Code Online (Sandbox Code Playgroud)

可以像这样正常使用:

+table-striped(["#","First Name","Last Name","Username"],[["1","Mark","Otto","@mdo"],["2","Jacob","Thornton","@fat"],["3","Larry","the Bird","@twitter"]])
Run Code Online (Sandbox Code Playgroud)

我的问题是是否可以从 vue 实例中获取 mixin 的“header”和“data”参数。

vue.js pug

5
推荐指数
1
解决办法
1866
查看次数

如何将变量传递给 Pug 的 `script.` 块?

我的index.pug文件中有此代码

doctype html
html
  head
    title= title
  body
    script(src=`${source}`)
    script.
      for (var event of events){
        VClient.Event.subscribe(event, createDiv);
      } 
Run Code Online (Sandbox Code Playgroud)

这是我将变量从 Express 传递给 pug 的方式。

var express = require('express');
var app = express();
app.set('view engine', 'pug')

app.get('/', function(req, res){
    var id = req.query.id || 23717;
    var source = `https://some.source.url/${id}.js`;
    res.render('index',
    {title: 'Preview Embed', source: source, events:["AD_START","AD_COMPLETE"]});
});
app.listen(5000);
Run Code Online (Sandbox Code Playgroud)

两者titlesource使其进入 pug 文件。但不是events

Uncaught ReferenceError: events is not defined
Run Code Online (Sandbox Code Playgroud)

如何在script.块内正确传递变量?

javascript node.js express pug

4
推荐指数
1
解决办法
2936
查看次数

标签 统计

express ×3

javascript ×3

node.js ×3

pug ×3

templates ×1

vue.js ×1