search

Home  >  Q&A  >  body text

前端 - 使用vue2服务端渲染的时候,如何在服务端请求数据,然后给vue渲染

使用vue2服务端渲染的web站点,它的大概流程:

  1. node服务器接受到客户端的请求

  2. 然后向其他服务器请求数据

  3. 把获取到的数据给vue渲染

  4. 把渲染后的东西返回给客户端

如有错误,欢迎指正

那么我现在疑惑的是,在2和3步骤,如何在服务端请求数据,然后给vue渲染,具体需要用到什么工具,如何实现?

提供数据的接口是常见的那种采用JSON格式的,问题相当于是,在node服务器中,如何像浏览器那样,向接口(url地址)发起请求(拉或推数据)。

我查看了vue2的官方文档并搜索了相关的资料,没有发现相关的描述。希望知道的大神给解答一下。

黄舟黄舟2783 days ago625

reply all(7)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 15:35:24

    Use the request package to easily send requests.

    reply
    0
  • 阿神

    阿神2017-04-17 15:35:24

    I have been studying server-side rendering of vue these days, and currently I have found that the following two packages can enable nodejs to request data from other servers. I'm using axios now because I saw that axios supports both nodejs and browsers. This is very useful for vue server-side rendering, I hope it can be a reference for everyone. Here is the address:

    request

    axios

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:35:24

    http://rc.vuejs.org/guide/ssr...

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:35:24

    vue-hackernews version 2.0 is included in the demo. The front-end and back-end isomorphic framework can be modified and modified to basically meet the needs of small projects

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:35:24

    I recommend using an express proxy package, so that when requesting in vue2, you don’t have to write the router controller repeatedly in the node. Just set the proxy rules.

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:35:24

    It can be seen from the official rendering plan that the backend just stuffs data and data status into the page. In terms of effect, the frontend is responsible for the rendering work, so other backends can also do it. For details, see the example and write the script tag to the page:)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:35:24

    I also want to know how to render data into the vue template. I have been studying it for a long time but I still don’t understand it~~! !

    reply
    0
  • Cancelreply