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

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

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

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

Laravel

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

Q&A

1回答

723閲覧

laravel vue.js コンポーネントで設定したクリックイベントが実行されない

buenabisuta

総合スコア0

Vue.js

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

Laravel

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

0グッド

0クリップ

投稿2020/04/22 17:49

前提・実現したいこと

laravelでVue.jsのクリックイベントを実施してその処理した値を画面に動的に表示をしたい。

発生している問題・エラーメッセージ

初期で設定している値(number)は表示されるのだが、
クリックイベントを実施しても値が変化しません。
いろいろサイトのソースを使用して試してみたのですが、動きません。
何が悪いのかが全然わからなかったので、ご質問致しました。。

設定しているテンプレートの画面が表示はされているので、ファイルを読み込んでいるとは思います。
そこから画面上で値を更新するイベントが実施されていません。

該当のソースコード

ソースコード

SampleComponent.vue

<template> <div> <p> Count : <span :class="numberClasses">{{ number }}</span> </p> <button @click="countDown">−1</button> <button @click="countUp">+1</button> </div> </template> <script> export default { name: "SampleComponent", data () { return { number: 0 } }, computed: { numberClasses: function () { return { 'positive': (this.number > 0), 'negative': (this.number < 0) } } }, methods: { countDown: function () { this.number-- }, countUp: function () { this.number++ } } } </script> <style scoped> p { margin: 10px; } .positive { color: blue; } .negative { color: red; } </style>

index.blade.php

@extends('layouts.app') <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="csrf-token" content="{{ csrf_token() }}"> <script src="https://unpkg.com/vue"></script> <title>Title</title> </head> @section('content') <body> <div class="container"> <div id="app"> <sample-component></sample-component> </div> </div> <script src="{{ mix('js/app.js')}}"></script> </body> @endsection
/** * First we will load all of this project's JavaScript dependencies which * includes Vue and other libraries. It is a great starting point when * building robust, powerful web applications using Vue and Laravel. */ require('./bootstrap'); window.Vue = require('vue'); /** * The following block of code may be used to automatically register your * Vue components. It will recursively scan this directory for the Vue * components and automatically register them with their "basename". * * Eg. ./components/ExampleComponent.vue -> <example-component></example-component> */ // const files = require.context('./', true, /.vue$/i) // files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default)) Vue.component('poke-component',require('./components/PokeComponent.vue').default); Vue.component('sample-component', require('./components/ExampleComponent.vue')); /** * Next, we will create a fresh Vue application instance and attach it to * the page. Then, you may begin adding components to this application * or customize the JavaScript scaffolding to fit your unique needs. */ const app = new Vue({ el: '#app', });

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

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

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

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

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

meshi_s

2020/04/23 08:59

※vueに詳しくないのですが… 各メソッド「countDown」「countUp」は通っていることを確認されていますか? また、「Vue.component('sample-component', require('./components/ExampleComponent.vue')); 」とありますが、「ExampleComponent.vue」の読み込みで大丈夫ですか?
buenabisuta

2020/04/23 13:06

ご返信ありがとうございます。 おそらく「countDown」「countUp」の処理は通っていないと思います。(コンソール処理で確認をしてみましたが、何も表示されておりませんでした。) すみません。ExampleComponent.vueで大丈夫です。 上のサンプルコード名ではSampleComponent.vueとなっていますが、誤りです。。。。
hayato7

2020/04/23 15:11

コンソールにエラーは出たりしていないのですか?
guest

回答1

0

Vue

1methods: { 2 countDown () { 3 this.number-- 4 }, 5 countUp () { 6 this.number++ 7 } 8}

としてはどうですか?

投稿2020/05/01 15:44

mktia

総合スコア110

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問