learn-webapp-guideline icon indicating copy to clipboard operation
learn-webapp-guideline copied to clipboard

Express : res.render(...,{...})时导入的数据怎么在一般js中使用。

Open zjutszl opened this issue 7 years ago • 3 comments

view/post.ejs

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title>cnode·Supported by Vue</title>
</head>

<body>
<% var postId = _id %>

<h1>_id : <%= _id %></h1>
<p>postId : <%= postId %></p>
<div id="app">
<h1>{{ detailData.content }}</h1>
<p>{{ post_id }}</p>

</div>
</body>

<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
var postId = '12345654321';
var app = new Vue({
    el: "#app",
    data: {
      detailData : {
      	content:'zjutszl'
      },
      post_id:postId
    }
})
</script>

</html>

routes/post.js

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

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.render('post_detail', {_id:req.query.id});
});


module.exports = router;

页面显示

alt

添加一行定义 [var postId = '12345654321']

alt

修改后页面显示

alt


为了阅读方便,先放了代码,图片在后面。

zjutszl avatar Oct 25 '17 08:10 zjutszl

那么问题来了,怎么在Vue里用到routes/post.js传入的数据:_id 呢?

zjutszl avatar Oct 25 '17 08:10 zjutszl

alt alt

zjutszl avatar Oct 25 '17 08:10 zjutszl

参考 18-edit-post

xugy0926 avatar Oct 25 '17 08:10 xugy0926