我是 meteor 新手,但看起来这应该很简单。我想创建一个页面,将 get 变量拉下来并将其显示在客户端上
ex: www.example.com?yourname=bob
页面将显示
bob
我觉得这应该很容易,但到目前为止还没有做到。我在客户端加载时创建了一个请求信息的调用,但由于某种原因它在第一次加载时不起作用。在后续加载中,它确实如此。
<head>
<title>Page Chat</title>
</head>
<body>
<div id="wrapper">
{{> name}}
</div>
</body>
<template name="name">
{{name}}
<br />
<input type="button" value="Click" />
</template>
js代码
if (Meteor.isClient) {
Meteor.startup(function(){
Meteor.call("getData", function(error, result){
if(error){
Session.set("name", "bob");
}
else{
Session.set("name", result.name);
}
});
});
Template.name.name = function(){
return Session.get("name");
};
Template.name.events({
'click input' : function () {
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed the button");
}
});
}
if (Meteor.isServer) {
var connect = Npm.require('connect');
var app = __meteor_bootstrap__.app;
var post, get;
app
// parse the POST data
.use(connect.bodyParser())
// parse the GET data
.use(connect.query())
// intercept data and send continue
.use(function(req, res, next) {
post = req.body;
get = req.query;
return next();
});
Meteor.startup(function () {
});
Meteor.methods({
getData: function() {
return get;
},
postData: function(){
return post;
}
});
}
如果可能的话,我想共享初始页面加载时的数据,创建单独的页面加载来获取页面首次加载时已经存在的信息似乎是一种浪费。
最佳答案
使用类似meteor-router之类的东西可能会更容易。那么你可以这样做
服务器端js:
Meteor.Router.add('/something', function() {
return this.params.yourname;
});
因此,如果您访问 example.com/something?yourname=Bob
,您将返回 Bob
。
从查询字符串/输入参数直接向客户端显示某些内容时要小心,就好像您在将其用于 XSS 之前没有对其进行检查。
关于Meteor,如何在客户端显示 url 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18138600/