如何访问 json 中所有 svelte 组件的数据文件 sapper/polka
How to access json data file for all svelte components in sapper/polka
我正在尝试用 Sapper(sveltejs) 重写我的网站 (Pug+Express)。我是 sveltejs 的初学者,如果我的问题可能真的很幼稚,请原谅。
我有一个 template.json 文件,其中包含我网站的所有静态数据。在 expressjs 版本中,我 const template = require('template.json')
并使用类似这样的 pug 模板呈现页面
router.get('/:pageName', function(req, res, next) {
res.render('pages/About', {template: template})
在 sveltejs/sapper 中实现此功能的等效版本是什么?
到目前为止,我在 app/server.js
文件中做了一个 import template from 'template.json'
。
然后呢?由于 sapper-template 使用的是 polka 而不是 express,我很困惑如何获得 right.Any 建议?
您将该数据放在使用它的页面(即 routes
中的组件)中:
<!-- routes/index.html -->
<h1>{{title}}</h1>
<script>
import data from './_template.json';
export default {
data() {
return {
title: data.title
};
}
};
</script>
请注意,我在 _template.json
中添加了一个下划线,以在路由器中隐藏该文件 — 您也可以将 JSON 文件放在 routes
目录之外。
您可以使用 Express 而不是 Polka;仅 npm install express
并将 app/server.js
中出现的每个 polka
替换为 express
.
我正在尝试用 Sapper(sveltejs) 重写我的网站 (Pug+Express)。我是 sveltejs 的初学者,如果我的问题可能真的很幼稚,请原谅。
我有一个 template.json 文件,其中包含我网站的所有静态数据。在 expressjs 版本中,我 const template = require('template.json')
并使用类似这样的 pug 模板呈现页面
router.get('/:pageName', function(req, res, next) {
res.render('pages/About', {template: template})
在 sveltejs/sapper 中实现此功能的等效版本是什么?
到目前为止,我在 app/server.js
文件中做了一个 import template from 'template.json'
。
然后呢?由于 sapper-template 使用的是 polka 而不是 express,我很困惑如何获得 right.Any 建议?
您将该数据放在使用它的页面(即 routes
中的组件)中:
<!-- routes/index.html -->
<h1>{{title}}</h1>
<script>
import data from './_template.json';
export default {
data() {
return {
title: data.title
};
}
};
</script>
请注意,我在 _template.json
中添加了一个下划线,以在路由器中隐藏该文件 — 您也可以将 JSON 文件放在 routes
目录之外。
您可以使用 Express 而不是 Polka;仅 npm install express
并将 app/server.js
中出现的每个 polka
替换为 express
.