我有几个关于使用这个框架的问题。
首先,我将提供我的代码片段和屏幕截图:
[slug].html
-----------
<:Head>
<title>{{info.title}}</title>
</:Head>
<Layout page="{{slug}}">
<h1>{{info.title}}</h1>
<div class="content">
<TopBar :organization_name />
<br>
<h3>Attributes</h3>
{{#each Object.keys(info.attributes) as attribute}}
<p>{{info.attributes[attribute].description}} <input type="text" on:keyup="updateComponent(this.value)" value="Org Name" /></p>
{{/each}}
</div>
</Layout>
<script>
import Layout from '../_components/components/Layout.html';
import TopBar from '../../_components/header/TopBar.html';
let COMPONENTS = require('../_config/components.json');
export default {
components: {
Layout, TopBar
},
methods: {
updateComponent(value) {
this.set({organization_name: value});
}
},
data() {
return {
organization_name: 'Org Name'
}
},
preload({ params, query }) {
params['info'] = COMPONENTS.components[params.slug];
return params;
}
};
</script>
现在我有一些问题:
我注意到我不能通过对象进行循环遍历。我必须遍历其键。如果我可以像这样做那就太好了:
{{#each info.attributes as attribute }}
{{attribute.description}}
{{/each}}
在使用 Sapper 之前, 我会使用 Angular-translate 模块基于给定的 JSON 文件对字符串进行翻译,是否有 Sapper/Svelte 的相应模块,或者我需要自己解决?
我不习惯使用导入。我更习惯于在 Angular 中使用依赖注入,它看起来更加简洁(没有路径)。我是否可以创建一个可在文件中随处使用的
COMPONENTS
常量,还是每次需要访问其数据时都需要导入 JSON 文件?作为 #3 的后续,我想知道是否有更好的方法来包含文件,而不必依赖于使用
../..
导航到我的文件夹结构中?如果我更改其中一个文件的路径,我的终端将发出错误,这很好,但我想知道是否有更好的方法来导入我的文件。我知道有更好的方法来实现我在示例中所实现的功能。基本上,您可以看到一个属性旁边的输入框,如果我在那里进行更改,我会调用一个
updateComponent
函数,然后在当前范围内执行this.set()
来覆盖绑定。这个方法可行,但我想知道是否有一种避免使用这个函数的方法。我认为可能可以绑定输入值并自动更新我的<TopBar>
组件绑定......也许?preload
方法提供了对params
的访问。我想知道是否有一种方法可以在不使用 preload 函数的情况下访问params.slug
。
最棒的方法是让一些专家以最佳方式重写我已经完成的部分,可能解答了一些我的问题。
params
的错误。 - roshnet$page.params
访问,或者从preload函数中获取它们,请执行import { stores } from '@sapper/app'; const { page } = stores();
。 - Rich Harris