我使用Node + Riot.js + Grapnel路由库(它可以在客户机和服务器上工作)。我设法在客户机上设置路由,但我想不出如何使它在服务器上工作。
我的客户端路由器的当前功能很简单。我只是将opts.route发送到适当的组件,然后将请求的页面(也是组件)挂载到div。
<x-layout>
<div id="app-body"></div>
<script>
this.on('mount update', function() {
riot.mount('#app-body', 'x-page-' + opts.route);
});
</script>
</x-layout>但是使用riot.render(tag, {page: 'dashboard'}),它不会将<x-page-dashboard>挂载到#app-body。
当我移除this.on('mount update' ...包装时,它会给我一个错误
.../node_modules/riot/riot.js:1918
TypeError: (ctx || document).querySelectorAll is not a function`这很明显,因为Node不能执行DOM操作。
我还尝试动态加载组件,如下所示
// Riot doesn't compiles such expressions `<x-page-{opts.route}>`
var tag = riot.tag2('x-layout', '<div id="app-body"><x-page-{opts.route}></x-page-{opts.route}></div>');
riot.render(tag, {route: 'dashboard'}); // --> <x-layout><div id="app-body"><x-page-{opts.route}></x-page-{opts.route}></div></x-layout>
// Compiles but not mounts (empty tag)
var tag = riot.tag2('x-layout', '<div id="app-body" riot-tag="x-page-{opts.route}"></div>');
riot.render(tag, {route: 'dashboard'}); // --> <x-layout><div id="app-body" riot-tag="x-page-dashboard"></div></x-layout>
// It's only working when I hard coded the tag name
var tag = riot.tag2('x-layout', '<x-page-dashboard></x-page-dashboard>');
riot.render(tag, {route: 'dashboard'}); // <x-layout><x-page-dashboard>___ CHILDREN COMPONENTS... ____</x-page-dashboard></x-layout>有没有可能实现同构呈现+路由?我快到了,只是需要通过opts动态传递组件名称
发布于 2015-12-19 14:32:18
我终于解决了。解决方案是使用name="app_body"属性,而不是像我试图做的那样使用id="app-body"。
<x-layout>
<div name="app_body"></div>
<script>
this.mountPage = (page) => {
riot.mount(this.app_body, 'x-page-' + page);
}
if (opts.route)
this.mountPage(opts.route)
</script>
</x-layout>感谢GianlucaGuarini响应https://github.com/riot/riot/issues/1450#issuecomment-165984208
https://stackoverflow.com/questions/34313279
复制相似问题