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

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

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

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

Vuetify.js

Vuetify.jsは、マテリアルデザインを基本とするVue.jsのCSSフレームワークです。多くのマテリアルデザインのコンポーネントを提供しており、あらゆるアプリケーションに対応可能。vue-cli用テンプレートがあり、簡単にページを作成できます。

Q&A

解決済

1回答

3290閲覧

【Vue.js】componentで定義した関数が使えない(Property or method "say" is not defined on the instance…)

nbi

総合スコア8

Vue.js

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

Vuetify.js

Vuetify.jsは、マテリアルデザインを基本とするVue.jsのCSSフレームワークです。多くのマテリアルデザインのコンポーネントを提供しており、あらゆるアプリケーションに対応可能。vue-cli用テンプレートがあり、簡単にページを作成できます。

0グッド

1クリップ

投稿2018/05/23 23:11

前提・実現したいこと

component化されたHello.vueのmethodで定義した関数を、ボタンに設定した@click(v-onと同義)イベントで実行したいと思っています。

具体的には、「アラート」ボタンをクリックすると、say()関数によってアラートダイアログが表示されることを想定しています。

tree -I 'node_modules|config|build|static|yarn.lock'

├── README.md ├── index.html ├── package-lock.json ├── package.json └── src ├── App.vue ├── assets │   └── logo.png ├── components │   └── HelloWorld.vue ├── main.js └── router └── index.js

index.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet"> <link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"> </head> <body> <div id="app"> </div> </body> </html>

App.Vue

<template> <div id="app"> <img src="./assets/logo.png"> <router-view/> </div> </template> <script> export default { name: 'App' } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>

main.js

import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false new Vue({ el: '#app', router, components: { App }, template: '<App/>' })

HelloWorld.vue

<template> <div class="hello"> <v-btn @click="say('Hello!')">アラート</v-btn> </div> </template> <script> export default { name: 'HelloWorld', method: { say: function (message) { alert(message) } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>

index.js

import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import Vuetify from 'vuetify' Vue.use(Router) Vue.use(Vuetify) export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld } ] })

HelloWorld.vue

<template> <div class="hello"> <v-btn @click="say('Hello!')">アラート</v-btn> </div> </template> <script> export default { name: 'HelloWorld', method: { say: function (message) { alert(message) } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>

発生している問題

「アラート」ボタンをクリックすると以下のようなConsoleエラーが出てしまい、アラートダイアログが表示されません。

[Vue warn]: Property or method "say" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. found in ---> <HelloWorld> at src/components/HelloWorld.vue <App> at src/App.vue <Root>
vue.esm.js?efeb:591 [Vue warn]: Error in event handler for "click": "TypeError: _vm.say is not a function" found in ---> <VBtn> <HelloWorld> at src/components/HelloWorld.vue <App> at src/App.vue <Root>
vue.esm.js?efeb:1741 TypeError: _vm.say is not a function at click (eval at ./node_modules/vue-loader/lib/template-compiler/index.js?{"id":"data-v-469af010","hasScoped":true,"transformToRequire":{"video":["src","poster"],"source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0!./src/components/HelloWorld.vue (app.js:905), <anonymous>:14:19) at VueComponent.invoker (vue.esm.js?efeb:2027) at VueComponent.Vue.$emit (vue.esm.js?efeb:2538) at VueComponent.e.(:8080/anonymous function) [as $emit] (chrome-extension://nhdogjmejiglipccpnnnanhbledajbpd/build/backend.js:1:17624) at VueComponent.click (vuetify.js?dc48:7275) at invoker (vue.esm.js?efeb:2027) at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1826)

原因など検討つきましたらご教授いただけると幸いです。

補足情報

package.json

{ "name": "vue-sample", "version": "1.0.0", "description": "A Vue.js project", "author": "", "private": true, "scripts": { "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", "start": "npm run dev", "lint": "eslint --ext .js,.vue src", "build": "node build/build.js" }, "dependencies": { "vue": "^2.5.2", "vue-router": "^3.0.1" }, "devDependencies": { "autoprefixer": "^7.1.2", "babel-core": "^6.22.1", "babel-eslint": "^8.2.1", "babel-helper-vue-jsx-merge-props": "^2.0.3", "babel-loader": "^7.1.1", "babel-plugin-syntax-jsx": "^6.18.0", "babel-plugin-transform-runtime": "^6.22.0", "babel-plugin-transform-vue-jsx": "^3.5.0", "babel-preset-env": "^1.3.2", "babel-preset-stage-2": "^6.22.0", "chalk": "^2.0.1", "copy-webpack-plugin": "^4.0.1", "css-loader": "^0.28.0", "eslint": "^4.15.0", "eslint-config-standard": "^10.2.1", "eslint-friendly-formatter": "^3.0.0", "eslint-loader": "^1.7.1", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.2.0", "eslint-plugin-promise": "^3.4.0", "eslint-plugin-standard": "^3.0.1", "eslint-plugin-vue": "^4.0.0", "extract-text-webpack-plugin": "^3.0.0", "file-loader": "^1.1.4", "friendly-errors-webpack-plugin": "^1.6.1", "html-webpack-plugin": "^2.30.1", "node-notifier": "^5.1.2", "optimize-css-assets-webpack-plugin": "^3.2.0", "ora": "^1.2.0", "portfinder": "^1.0.13", "postcss-import": "^11.0.0", "postcss-loader": "^2.0.8", "postcss-url": "^7.2.1", "rimraf": "^2.6.0", "semver": "^5.3.0", "shelljs": "^0.7.6", "uglifyjs-webpack-plugin": "^1.1.1", "url-loader": "^0.5.8", "vue-loader": "^13.3.0", "vue-style-loader": "^3.0.1", "vue-template-compiler": "^2.5.2", "vuetify": "^1.0.18", "webpack": "^3.6.0", "webpack-bundle-analyzer": "^2.9.0", "webpack-dev-server": "^2.9.1", "webpack-merge": "^4.1.0" }, "engines": { "node": ">= 6.0.0", "npm": ">= 3.0.0" }, "browserslist": [ "> 1%", "last 2 versions", "not ie <= 8" ], "main": "index.js", "license": "MIT" }

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

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

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

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

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

guest

回答1

0

ベストアンサー

Vue properties は methodmethods と表記するので、その間違いではないでしょうか

投稿2018/05/24 02:12

naokie

総合スコア15

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

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

nbi

2018/05/24 11:05

上記の通りで、解決いたしました…! 凡ミス、恥ずかしい限りです。ご回答いただきありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問