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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1063閲覧

【Vue.js】フォームをコンポーネント化したら表示されなくなった

katahik

総合スコア79

Vue.js

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/02 21:59

編集2021/08/02 22:06

概要

現在、作成したnew.vueとedit.vueでフォームの部分が似ているので、共通化して、コンポーネント化したいと考えています。
そこで、新たに、form.vueを作成して、それを呼び出す形でページを表示したいと考えています。

しかし、コンポーネント化した際に、ページが表示されなくなってしまいました。
(ログにもそれらしき記述はなく、Consoleにもエラー表示がない)

データの受け渡しがうまくいっていないものと思いますが、どこを修正したらよいかわかりかねます。

修正方法について、ご教示いただければ幸いです。

コンポーネント化する前のもとのコード

New.vue
<template> <main> <form> <section> <div> <div> <fieldset> <div class="form-row"> <div class="form-group col-3"> <label>タイトル</label> <input v-model="latest_information.title" type="text"> </div> <div class="form-group col-3"> <label>詳細</label> <input v-model="latest_information.detail" type="text"> </div> </div> </fieldset> </div> </div> </section> <div class="btn-container d-flex justify-content-center"> <button class="button-square btn-send" type="button" @click="createLatestInformation">保存する</button> </div> </form> </main> </template> <script> export default { data() { return { latest_information: { title: '', detail: '', }, } }, methods: { createLatestInformation() { this.$loading.load(this.$auth.api.post('admin/latest_informations/', { latest_information: this.latest_information }) .then(response => { this.$router.push({name: 'AdminLatestInformationIndex'}) }))} }, } </script>

コンポーネント化をしたあとのコード(うまく挙動していない)

New.vue
<template> <form :latest_information="latest_information" @click="createLatestInformation"></form> </template> <script> import Form from './Form.vue'; export default { components:{ Form }, data() { return { latest_information: { title: '', detail: '', }, } }, methods: { createLatestInformation() { this.$loading.load(this.$auth.api.post('admin/latest_informations/', { latest_information: this.latest_information }) .then(response => { this.$router.push({name: 'AdminLatestInformationIndex'}) }))} }, } </script>
Form.vue
<template> <main> <form> <section> <div> <div> <fieldset> <div class="form-row"> <div class="form-group col-3"> <label>タイトル</label> <input v-model="latest_information.title" type="text"> </div> <div class="form-group col-3"> <label>詳細</label> <input v-model="latest_information.detail" type="text"> </div> </div> </fieldset> </div> </div> </section> <div class="btn-container d-flex justify-content-center"> <button class="button-square btn-send" type="button" @click="$emit('click')">保存する</button> </div> </form> </main> </template> <script> export default { props: { latest_information: { title: '', detail: '', }, } } </script> <style> </style>

環境

rails 6系
vue@2.6.10

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

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

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

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

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

guest

回答1

0

ベストアンサー

formは予約語なのでテンプレート名に使ってはいけません(formタグとの区別ができないので)。なので、tformなどという名前に変えて対処しましょう。

vue:New.vue

1<template> 2 <tform :latest_information="latest_information" @click="createLatestInformation"></tform> 3</template> 4 5<script> 6import Form from './Form.vue'; 7 8export default { 9 components:{ 10 'tform':Form 11 },

投稿2021/08/03 01:08

編集2021/08/03 01:10
FKM

総合スコア3644

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

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

katahik

2021/08/03 04:38

ご提示いただいたとおり予約語を使ってしまっていたため、修正したところ、無事正常な挙動を確認しました。本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問