質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vue CLI

Vue CLIは、Vue.jsでアプリケーション開発を行うためのコマンドラインインタフェース(CLI)に基づいた開発ツールです。インタラクティブなプロジェクトの雛形や設定なしで使用できるプロトタイプの作成など、さまざまな機能が用意されています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

データバインディング

データソースと、アプリケーションやウェブページ(ウェブアプリケーション)のユーザインタフェースを静的または動的に結合する技術です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

1151閲覧

複数のデータを一つのセットとしてlocalStorageに保存したい

Eston

総合スコア67

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Vue CLI

Vue CLIは、Vue.jsでアプリケーション開発を行うためのコマンドラインインタフェース(CLI)に基づいた開発ツールです。インタラクティブなプロジェクトの雛形や設定なしで使用できるプロトタイプの作成など、さまざまな機能が用意されています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

データバインディング

データソースと、アプリケーションやウェブページ(ウェブアプリケーション)のユーザインタフェースを静的または動的に結合する技術です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

1クリップ

投稿2020/04/02 19:35

編集2020/04/03 10:45

いつもお世話になっております。
現在、ECサイトと作成する過程で、複数のデータを一つのセットにまとめてLocalStorageに保存する方法について模索しております。

開発環境
Mac, Vue, BoostrapVue
サーバーホストネームは https://euas.person.ee/ です

POSTとGETの使い方がまだよく分かっていないのですが、下記の3点を意識しています。
1)POST / user / carts / {cardId} / orders -指定されたcartIdに基づいてorderを作成します
2)GET / user / orders- orderの配列をreturnします。構造はShoppingCartと同じ
3)GET / user / orders / {orderId} -特定のorderをretunします。構造はShoppingCartと同じにする

下に2つの画像がありますが、"ShoppingCart"で表示されている3つのプロダクトとその他の情報(Price, qty,Amount,そしてTotalPrice)をまとめて、cartId:1に保存して、"OrderListing"(2枚めの写真)のCartIdとTotalPriceで表示したいと考えています。

Order Now!ボタンを押すと、ShoppingCartが空になる仕様です。

なので、ShoppingCartでデータを保存して、LocalStorageにcartIdごとにデータセットを保存するやり方について教えていただけると幸いです。

ShoppingCart↓
ShoppingCart Page
OrderListing↓

赤字は画像に書き込んだもので、実際に表示されているわけではありません.
OrderListing Page

ShoppingCartVue

1<template> 2<div class="shopping-cart-page"> 3 <h2>ShoppingCart</h2> 4 <table class="table"> 5 <tr> 6 <th>Product</th> 7 <th>Price</th> 8 <th>qty</th> 9 <th>Amount</th> 10 <th>Actions</th> 11 </tr> 12 13 <tr v-for="(item, index) in this.items" :key="item.productId + '_' + index"> 14 <td> 15 <img :src="item.optionImage" class="option-image" /> 16 </td> 17 <td>{{ item.price }}</td> 18 <td>{{ item.qty }}</td> 19 <td>{{ item.total }}</td> 20 <td> 21 <b-button variant="danger" @click="removeItem(index)">Remove</b-button> 22 </td> 23 </tr> 24 <tr class="total-row"> 25 <td>TOTAL:</td> 26 <td></td> 27 <td></td> 28 <td>{{ total }}</td> 29 <td></td> 30 </tr> 31 </table> 32 33 <b-button variant="success" size="lg" @click="orderNow" v-if="this.items.length">Order Now!</b-button> 34 35</div> 36</template> 37 38<script> 39import axios from "axios"; 40export default { 41 name: 'ShoppingCartPage', 42 computed: { 43 items: function() { 44 return this.$root.$data.cart.items || []; 45 }, 46 total: function() { 47 let sum = 0 48 for (const item of this.items) { 49 sum += item.total 50 } 51 return sum 52 } 53 }, 54 methods: { 55 removeItem: function(index) { 56 if (!this.$root.$data.cart.items) this.$root.$data.cart.items = [] 57 this.$root.$data.cart.items.splice(index, 1); 58 console.log(this.$root.$data.cart.items); 59 this.$root.$data.saveCart(); 60 }, 61 62 orderNow: function() { 63 let data = this.$root.$data 64 65 axios.post("https://euas.person.ee/user/carts/" + this.$root.$data.cart.id + "/orders", 66 this.$root.$data.cart).then(function() { 67 data.reinitCart(); 68 }) 69 } 70 } 71} 72</script> 73 74 75<style scoped> 76.option-image { 77 max-height: 50px; 78 max-width: 100px; 79} 80</style> 81

正直、何処を変えれば、cartIdが表示されるかわからなかったので、ShoppingCartをコピペして、一部を変化させただけになっています。↓

OrderListingVue

1<template> 2<div class="OrderListing"> 3 <h2>Your Orders</h2> 4 <table class="table"> 5 <tr> 6 <th>CartId</th> 7 <th>TotalPrice</th> 8 <th>Action</th> 9 </tr> 10 11 <tr v-for="(item, index) in this.items" :key="item.productId + '_' + index"> 12 13 <td>ここにcartIdを表示</td> 14 <td>ここにcartIdごとのtotalPriceを表示したい</td> 15 </tr> 16 </table> 17 18 19 20</div> 21</template> 22 23<script> 24 25export default { 26 name: 'OrderListing', 27 computed: { 28 items: function() { 29 return this.$root.$data.cart.items || []; 30 }, 31 total: function() { 32 let sum = 0 33 for (const item of this.items) { 34 sum += item.total 35 } 36 return sum 37 } 38 }, 39 methods: { 40 } 41} 42</script> 43 44 45<style scoped> 46.option-image { 47 max-height: 50px; 48 max-width: 100px; 49} 50</style>

main.js↓では、POSTとGETを設定していくと思うのですが、どうすれば、OrderごとにcartIdに保存できるのかわかりません。

main

1import Vue from 'vue' 2import App from './App.vue' 3import BootstrapVue from 'bootstrap-vue' 4import 'bootstrap/dist/css/bootstrap.css' 5import 'bootstrap-vue/dist/bootstrap-vue.css' 6import VueRouter from 'vue-router' 7import MainPage from './components/MainPage.vue' 8import ProductPage from './components/ProductPage.vue' 9import Category from './components/Category.vue' 10 11import axios from "axios"; 12import ShoppingCartPage from './components/ShoppingCartPage.vue' 13import OrderListing from './components/OrderListing.vue' 14 15 16Vue.config.productionTip = false 17Vue.use(BootstrapVue) 18Vue.use(VueRouter) 19 20const router = new VueRouter({ 21 routes: [{ 22 path: '/', 23 component: MainPage 24 }, 25 26 { 27 path: '/categories/:categoryAlias', 28 component: Category 29 }, 30 { 31 path: '/products/:productId', 32 component: ProductPage 33 }, 34 { 35 path: '/cart', 36 component: ShoppingCartPage 37 }, 38 { 39 path: '/order', 40 component: OrderListing 41 } 42 ], 43 mode: 'history' 44}); 45 46axios.defaults.headers.common['Authorization'] = 'Bearer pasuwaado135@gmail.com'; 47 48if (localStorage.cartId) { 49 axios.get("https://euas.person.ee/user/carts" + localStorage.cartId).then(response => { 50 new Vue({ 51 render: h => h(App), 52 router: router, 53 data: { 54 cart: response.data, 55 saveCart() { 56 axios.put("https://euas.person.ee/user/carts/" + this.cart.id, this.cart) 57 }, 58 reinitCart() { 59 axios.post("https://euas.person.ee/user/carts").then(response => { 60 localStorage.cartId = response.data.id 61 this.cart = response.data; 62 }); 63 } 64 } 65 }).$mount('#app') 66 }); 67} else { 68 axios.post("https://euas.person.ee/user/carts").then(response => { 69 new Vue({ 70 render: h => h(App), 71 router: router, 72 data: { 73 cart: response.data, 74 saveCart() { 75 axios.put("https://euas.person.ee/user/carts/" + this.cart.id, this.cart) 76 }, 77 reinitCart() { 78 axios.post("https://euas.person.ee/user/carts").then(response => { 79 localStorage.cartId = response.data.id 80 this.cart = response.data; 81 }); 82 } 83 } 84 }).$mount('#app') 85 }); 86}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

minrara

2020/04/03 04:28

現在OrderListingの画面でCartIdとTotalPriceを表示するところまでできて、追加実装としてPrice, qty,Amountも画面に表示させたい。という認識で大丈夫ですか?
Eston

2020/04/03 10:44

ご連絡、ありがとうございます。 いえ、CartIDとTotalPriceが表示されないということで悩んでおります。 赤字は画像に書き込んだもので、実際に表示されているわけではありません.
Eston

2020/04/03 10:48

すでに、ShoppingCartVueのOrder Now!ボタンでPOSTは出来ているはずなので、保存されているデータをどう取り出して表示するのか?(GET / user / ordersを使って) と言った方が、問題として正しい説明かも知れません.
guest

回答1

0

自己解決

Totalは表示できませんでしたが、それなりのものは出来ました

イメージ説明

OrderListingVue

1<template> 2<div class="OrderListing"> 3 <h2>My Orders</h2> 4 <table class="table"> 5 <tr> 6 <th>OrderId</th> 7 <th>OrderDescription</th> 8 <th>Action</th> 9 </tr> 10 11 <tr v-for="(cart, order) in this.orders" :key="order.id"> 12 <td>{{order}}</td> 13 <td>{{cart}}</td> 14 <td> 15 <b-button variant="dark" :to=" '/orders/' + order">Detail</b-button> 16 </td> 17 </tr> 18 19 </table> 20 21 22 23</div> 24</template> 25 26<script> 27import axios from "axios"; 28export default { 29 name: 'OrderListing', 30 props: { 31 order: Object 32 }, 33 data: function() { 34 return { 35 orders: [] 36 } 37 }, 38 mounted() { 39 axios.get("https://euas.person.ee/user/orders") 40 .then(response => { 41 this.orders = response.data; 42 }); 43 } 44} 45</script> 46 47 48<style scoped> 49.option-image { 50 max-height: 50px; 51 max-width: 100px; 52} 53</style> 54

投稿2020/04/04 20:33

編集2020/04/04 20:34
Eston

総合スコア67

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問