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

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

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

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

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

2857閲覧

Vueコンポーネント内のdataとpropsの値がnullになってしまう

Tikka123456

総合スコア34

Vue.js

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

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2020/12/23 14:01

Vueコンポーネント内で定義したdataとpropsがnullとなってしまう現象を解決したいです。

エラー
イメージ説明

app.js?id=5d551c7144e8f7c4a7e5:2112 Uncaught (in promise) ReferenceError: responce is not defined

responce is not definedと出ているのが、responceはちゃんと定義されているように見られます。

ソールコード
ArticleVote.vue

<template> <div> <button type="button" class="btn m-0 p-1 btn-peach-gradient" @click="clickVote" > </button> {{ countVotes }} // ここに数字が表示されない </div> </template> <script> export default { props:{ initialIsVotedBy: { type: Boolean, default: false, }, initialCountVotes: { type: Number, default: 0, }, authorized: { type: Boolean, default: false, }, endpoint: { type: String, }, initialLoginTime: { type: Boolean, default: false, }, }, data(){ return { isVotedBy: this.initialIsVotedBy, countVotes: this.initialCountVotes, loginTime: this.initialLoginTime, } }, methods: { clickVote() { this.isVotedBy ? this.unvote() :this.vote() this.loginTime ? this.unvote() :this.vote() }, async vote() { const response = await axios.put(this.endpoint) this.isVotedBy = true this.loginTime = false this.countVotes = response.data.countVotes console.log(this.countVotes) }, async unvote() { const response = await axios.delete(this.endpoint) this.isVotedBy = false this.loginTime = true this.countVotes = responce.data.countVotes }, }, } </script>

Article

<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use App\User; use Carbon\Carbon; class Article extends Model { public function user(): BelongsTo { return $this->belongsTo('App/User'); } public function votes(): BelongsToMany { return $this->belongsToMany('App\User', 'votes')->withTimestamps(); } public function getCountVotesAtribute(): int { return $this->votes->count(); } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

props に null が渡されても default が設定されないみたいです。
(undefined の場合のみdefaultが設定される?)

親のinitialCountVotesを見直すか、null の場合に 0 にします。

js

1 data(){ 2 return { 3 countVotes: this.initialCountVotes || 0, 4 } 5 },

あと、response ではないでしょうか。s が c になってます。

投稿2020/12/23 15:12

neko_daisuki

総合スコア2090

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

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

Tikka123456

2020/12/25 23:52

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問