简体   繁体   English

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

[英]Server side rendering + routing with Riot.js?

I'm using Node + Riot.js + Grapnel routing library (which can work on both client and server). 我正在使用Node + Riot.js + Grapnel路由库(可以在客户端和服务器上运行)。 I managed how to set up routing on the client but I can't figure the way to make it work on the server. 我设法在客户端上设置路由,但是我不知道如何使其在服务器上起作用。

The current functionality of my client router is simple. 我的客户端路由器的当前功能很简单。 I'm just sending opts.route to the proper component and then it mounts the requested page (which is also a component) to a div . 我只是将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>

But with riot.render(tag, {page: 'dashboard'}) it doesn't mounts the <x-page-dashboard> to #app-body . 但是,使用riot.render(tag, {page: 'dashboard'})不会将<x-page-dashboard>挂载到#app-body

When I remove this.on('mount update' ... wrapper it gives me an error 当我删除this.on('mount update' ...包装器时,它给我一个错误

.../node_modules/riot/riot.js:1918
TypeError: (ctx || document).querySelectorAll is not a function`

which is obvious, because Node can't perform DOM manipulations. 这很明显,因为Node无法执行DOM操作。

I also tried to dynamically load components, like this 我也试图动态加载组件,像这样

// 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>

Is there any possible way to implement isomorphic rendering + routing? 有没有可能实现同构渲染+路由的方法? I'm almost there, just need to pass somehow component name dynamically through opts 我快到了,只需要通过opts动态传递组件名称即可

I finally solved it. 我终于解决了。 The solution was to use name="app_body" attribute but not id="app-body" as I was trying to do. 解决方案是使用name="app_body"属性,而不要使用id="app-body" 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>

Thanks to GianlucaGuarini response https://github.com/riot/riot/issues/1450#issuecomment-165984208 感谢GianlucaGuarini的回复https://github.com/riot/riot/issues/1450#issuecomment-165984208

Working example https://github.com/GianlucaGuarini/riot-app-example 工作示例https://github.com/GianlucaGuarini/riot-app-example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM