服务器端渲染+路由与Riot.js?

时间:2015-12-16 13:25:42

标签: javascript node.js riot.js

我正在使用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动态传递组件名称

0 个答案:

没有答案