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

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

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

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

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

Q&A

解決済

2回答

1855閲覧

henkou【Nuxt.js】子コンポーネントのメソッド内のforEachでpropsデータを使用する方法

tokuwgawa

総合スコア45

Vue.js

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

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

0グッド

0クリップ

投稿2018/12/13 07:45

編集2018/12/13 07:45

子コンポーネント内メソッドのforEachpropsないのデータを取得したいのですが、this.template.widthなどをログで吐かせてもundefindになってしまいます。
このような時にforEach内でpropsの変数を取得する方法を教えてください。

<template> </template> <script> export default { props: { template: { type: Object, default: () => ({ width: "", height: "", layers: [] }) } }, mounted: function() { }, methods: { getTemplates() { let templates = [ { id: 1, width: 300, height: 400, layers: [ {"type":"logo", "width":50, "height":50, "x_position": 10, "y_position": 10, "z_index": 1}, {"type":"image", "width":300, "height":200, "x_position": 100, "y_position": 80, "z_index": 0} ] }, ] let response = JSON.stringify(templates) return JSON.parse(response); }, replaceTemplates() { let oldTemplates = this.getTemplates() let templates = [] oldTemplates.forEach(function (templateSettings) {      //this.template.width と this.template.height が undefind になってしまう if(templateSettings.width == this.template.width && templateSettings.height == this.template.height) { templates.append(template) } }) }, } } </script>

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

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

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

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

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

guest

回答2

0

どんな風に確認しましたか?

mountedでreplaceTemplatesを呼び出してみましたが、default値を取得出来ているのが確認出来ますよー。

vue

1<template> 2 <div> 3 <p>child component</p> 4 </div> 5</template> 6 7<script> 8export default { 9 props: { 10 template: { 11 type: Object, 12 default: () => ({ 13 width: 500, 14 height: 300, 15 layers: [] 16 }) 17 } 18 }, 19 mounted() { 20 this.replaceTemplates() 21 }, 22 methods: { 23 getTemplates() { 24 const templates = [ 25 { 26 id: 1, 27 width: 300, 28 height: 400, 29 layers: [ 30 { 31 type: "logo", 32 width: 50, 33 height: 50, 34 x_position: 10, 35 y_position: 10, 36 z_index: 1 37 }, 38 { 39 type: "image", 40 width: 300, 41 height: 200, 42 x_position: 100, 43 y_position: 80, 44 z_index: 0 45 } 46 ] 47 } 48 ] 49 const response = JSON.stringify(templates) 50 return JSON.parse(response) 51 }, 52 53 replaceTemplates() { 54 const oldTemplates = this.getTemplates() 55 const templates = [] 56 57 oldTemplates.forEach(templateSettings => { 58 console.log(`width:${this.template.width}`) 59 console.log(`height:${this.template.height}`) 60 61 if (templateSettings.width == this.template.width && templateSettings.height == this.template.height) { 62 templates.append(template) 63 } 64 }) 65 } 66 } 67} 68</script> 69

投稿2018/12/13 08:25

shou6

総合スコア305

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

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

tokuwgawa

2018/12/13 08:52

ご回答ありがとうございます。 replaceTemplates() メソッドのforEatch内で console.log(this.template.width)を確認したのですが、表示されませんでした。。
guest

0

ベストアンサー

forEachの引数である関数の中で参照しているthisがコンポーネントになっていないことが原因です。
forEachの引数である関数のthisを任意の値に指定する場合は、forEachの第2引数に指定してください。

oldTemplates.forEach(function (templateSettings) { console.log(this) // -> `replaceTemplates()`の`this`

もしくは、アロー関数を使って関数のthisをレキシカルに束縛してください。

replaceTemplates() { // 省略 oldTemplates.forEach((templateSettings) => { console.log(this) // -> `replaceTemplates()`の`this` }) }

もしくは、クロージャを利用して、関数の外側の変数を参照してください。

replaceTemplates() { var vm = this // 省略 oldTemplates.forEach(function(templateSettings) { console.log(vm) // -> `replaceTemplates()`の`this` }) }

参考リンク

Googleで検索しても大量に情報があります。
後者2つは重要な概念ですので、一度調べることをおすすめします。

投稿2018/12/13 09:23

編集2018/12/13 09:23
NozomuIkuta

総合スコア1260

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問