简体   繁体   English

如何从 Node.js 获取 Vue 中的数据

[英]How can I get the data in Vue from Node.js

index.js索引.js

router.get('/', (req, res) => {
    var cart = req.cookies.cart;
    res.sendFile(path.join(__dirname,'../../src/components/cart.html'),cart);
})

cart.html购物车.html

<body>
    <div id="app" class="container mt-3">
        <h2> cart View </h2>
    </div>
</body>

<script>
    new Vue({
        el: '#app',
        data: {

        }
    })
</script>

I want to get cookies in vue..我想在 vue 中获取 cookie ..

How to get 'cart' data in Vue ?如何在 Vue 中获取“购物车”数据?

Is that right way make res.send('../cart.html',cart) ?这是正确的方法 make res.send('../cart.html',cart)吗?

You have to do request from vue to your server to fetch the data.您必须从 vue 向您的服务器发出请求以获取数据。 You can use axios for that.你可以使用axios

BUT in this case you are sending cookies to server and trying to return cookies from request, why?但是在这种情况下,您将 cookie 发送到服务器并尝试从请求中返回 cookie,为什么? You can take your cookies on front-end without any request, use js-cookie .您可以在没有任何请求的情况下在前端获取 cookie,使用js-cookie

Cookie.get('cart')

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

相关问题 如何将get函数中的数据从node.js中的同一个js文件中的另一个函数中获取? - How can I get data from a get function to another function in the same js file in node.js? 如何将这些数据从 mongodb 检索到 node.js? - How can I retrieve this data from mongodb into node.js? 如何从 node.js 中的表单获取输入数据? - How can i get input data from a form in node.js? 如何在字符串中通过data属性获取元素并在node.js应用程序中获取内部文本? - How can I Get element by data attribute in string and get the inner text in a node.js application? 如何从node.js中获取请求的响应中获取数据? - How to get data from response to get request in node.js? 我无法从服务器(node.js + express)获取数据,在客户端通过 fetch API - I can not get data from the server (node.js + express), on the client through the fetch API 我无法使用 socket.io 和 node.js 从数据库中获取数据 - i can't get data from database with socket.io and node.js 如何在Node.js中创建Get请求以从json获取特定对象? - How can I create a Get request in Node.js to get specific object from json? 如何使用Node.js从Node.js发送和获取JSON数据到HTML - How to send and get JSON data from Node.js into HTML using Node.js Node.js:如何获取Firebase数据库数据快照的参考 - Node.js: How can I get the reference for a Firebase Database Data Snapshot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM