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

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

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

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

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

Q&A

解決済

1回答

1818閲覧

vueのelse部分のエラーについて

退会済みユーザー

退会済みユーザー

総合スコア0

Vue.js

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

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

0グッド

0クリップ

投稿2020/05/19 05:43

現在、vue.jsでtodoリストの作っているのですがif-elseを追加したところエディタでエラーが出てしまいました。
動作には特に影響ありませんが、このエラーを解消する方法を知りたいです。

↓v-elseのどちらかを消すとエラーは消えます。 <button class="todos__completed__btn" type="button" @click="changeCompleted(todo.id)" > <template v-if="todo.completed"> <span>完了</span> </template> <template v-else>   ←ここの部分を入れたらエラーが出ました。 <span>未完了</span> </template> </button> <template v-else> <p class="todos__empty">やることリストには何も登録されていません。</p>  ←ここでエラーが出ています。 </template>
↓コード全体 <template lang="html"> <div class="todo__wrapper"> <div class="todo__inner"> <header class="header"> <h1 class="header__title">やることリスト</h1> </header> <main class="main"> <form class="register" @submit.prevent="addTodo"> <div class="register__input"> <p class="register__input__title">やることのタイトル</p> <input v-model="targetTodo.title" type="text" name="title" placeholder="ここにTODOのタイトルを記入してください" required > </div> <div class="register__input"> <p class="register__input__title">やることの内容</p> <textarea v-model="targetTodo.detail" name="detail" rows="3" placeholder="ここにTODOの内容を記入してください。改行は半角スペースに変換されます。" required /> </div> <div class="register__submit"> <button class="register__submit__btn" type="submit" name="button"> 登録する </button> </div> </form> <div v-if="errorMessage" class="error"> <p class="error__text">{{ errorMessage }}</p> </div> <div class="todos"> <template v-if="todos.length"> <ul class="todos__list"> <li v-for="todo in todos" :key="todo.id" :class="todo.completed ? 'is-completed' : ''" > <div class="todos__inner"> <div class="todos__completed"> <button class="todos__completed__btn" type="button" @click="changeCompleted(todo)" > <template v-if="todo.completed"> <span>完了</span> </template> <template v-else> <span>未完了</span> </template> </button> </div> <div class="todos__desc"> <h2 class="todos__desc__title">{{ todo.title }}</h2> <p class="todos__desc__detail">{{ todo.detail }}</p> </div> <div class="todos__btn"> <button class="todos__btn__edit" type="button">編集</button> <button class="todos__btn__delete" type="button">削除</button> </div> </div> </li> </ul> </template> <template v-else> <p class="todos__empty">やることリストには何も登録されていません。</p> </template> </div> </main> <footer class="footer"> <p>全項目数: {{ todos.length }}</p> <p>完了済: {{ todos.filter(todo => todo.completed).length }}</p> <p>未完了: {{ todos.filter(todo => !todo.completed).length }}</p> </footer> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { todos: [], targetTodo: { id: null, title: '', detail: '', completed: false, }, errorMessage: '', }; }, created() { axios.get('http://localhost:3000/api/todos/').then(({ data }) => { this.todos = data.todos.reverse(); }).catch((err) => { if (err.response) { this.errorMessage = err.response.data.message; } else { this.errorMessage = 'ネットに接続がされていない、もしくはサーバーとの接続がされていません。ご確認ください。'; } }); }, methods: { addTodo() { const postTodo = Object.assign({}, { title: this.targetTodo.title, detail: this.targetTodo.detail, }); axios.post('http://localhost:3000/api/todos/', postTodo).then(({ data }) => { this.todos.unshift(data); this.targetTodo = Object.assign({}, this.targetTodo, { title: '', detail: '' }); this.errorMessage = ''; }).catch((err) => { if (err.response) { this.errorMessage = err.response.data.message; } else { this.errorMessage = 'ネットに接続がされていない、もしくはサーバーとの接続がされていません。ご確認ください。'; } }); }, changeCompleted(todo) { const targetTodo = Object.assign({}, todo); axios.patch(`http://localhost:3000/api/todos/${targetTodo.id}`, { completed: !targetTodo.completed, }).then(({ data }) => { this.todos = this.todos.map((todoItem) => { if (todoItem.id === targetTodo.id) return data; return todoItem; }); this.errorMessage = ''; }).catch((err) => { if (err.response) { this.errorMessage = err.response.data.message; } else { this.errorMessage = 'ネットに接続がされていない、もしくはサーバーとの接続がされていません。ご確認ください。'; } }); }, }, }; </script> <style lang="scss" scoped> .todo { &__wrapper { margin: 0 auto; padding: 20px 0; width: 700px; max-height: 100vh; } &__inner { position: relative; max-height: calc(100vh - 40px); border-radius: 10px; box-shadow: #aaa 0 0 20px 1px; } } .header { position: absolute; top: 0; left: 0; right: 0; padding: 20px 0; color: #fff; font-size: 16px; line-height: 1.2; text-align: center; border-radius: 10px 10px 0 0; background-color: #54b363; } .main { padding: 78px 0 56px; max-height: calc(100vh - 40px); overflow: scroll; border-radius: 10px; background-color: #fff; } .register { padding: 10px 20px; padding-bottom: 0; &__inner { width: 80%; } &__input { & + & { margin-top: 10px; } &__title { font-weight: bold; font-size: 14px; } input, textarea { padding: 10px; width: 100%; font-size: 14px; border: 1px solid #ddd; } } &__submit { margin-top: 10px; text-align: right; &__btn { padding: 10px 25px; color: #fff; font-size: 12px; border-radius: 7px; background-color: #54b363; } } } .error { padding: 0 20px; text-align: center; &__text { margin-top: 10px; padding: 5px; color: #f00; font-size: 14px; background-color: #efefef; } } .todos { margin-top: 20px; padding: 10px; &__empty { font-size: 14px; text-align: center; } &__list { & > li { padding: 15px 10px; border-top: 1px solid #ddd; transition: all .3s; &:first-child { border-top: none; } &.is-completed { color: #ccc; background-color: #f3f3f3; .todos__completed__btn { text-decoration: line-through; color: #ccc; border: 2px solid #eaeaea; background-color: #eaeaea; } .todos__desc__title, .todos__desc__detail { color: #ccc; } } } } &__inner { display: flex; align-items: flex-start; justify-content: space-between; } &__completed { width: 15%; min-width: 100px; &__btn { padding: 5px 10px; color: #ff1919; font-weight: bold; font-size: 12px; border-radius: 7px; border: 2px solid #ff1919; background-color: #fff; transition: all .3s; } } &__desc { width: 70%; min-width: 450px; &__title { color: #000; font-weight: bold; font-size: 16px; line-height: 1.2; transition: all .3s; input { padding: 5px 10px; width: 100%; color: #000; font-size: 16px; border: 1px solid #ddd; transition: all .3s; } } &__detail { margin-top: 5px; color: #777; font-size: 14px; line-height: 1.2; transition: all .3s; textarea { padding: 5px 10px; width: 100%; color: #000; font-size: 14px; border: 1px solid #ddd; transition: all .3s; } } } &__btn { width: 10%; min-width: 70px; text-align: center; &__edit, &__delete { padding: 5px 10px; border-radius: 7px; color: #fff; font-size: 12px; } &__edit { background-color: #07C4D7; } &__delete { margin-top: 5px; background-color: #ff1919; } } } .footer { display: flex; justify-content: space-around; position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; font-size: 14px; line-height: 1.2; color: #555; border-radius: 0 0 10px 10px; background-color: #ddd; } </style>

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

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

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

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

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

guest

回答1

0

ベストアンサー

条件付きレンダリング

v-else 要素は、v-if または v-else-if 要素の直後になければなりません。それ以外の場合は認識されません。

が原因ではないかと

html

1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="UTF-8"> 5 <script src="https://unpkg.com/vue"></script> 6</head> 7<body> 8<div id="app"> 9<button 10 class="todos__completed__btn" 11 type="button" 12 @click="changeCompleted(todo.id)" 13 > 14 <template v-if="todo.completed"> 15 <span>完了</span> 16 </template> 17<!-- 18 <template v-else> 19 <span>未完了</span> 20 </template> 21--> 22</button> 23<template v-else> 24 <p class="todos__empty">やることリストには何も登録されていません。</p> 25</template> 26</div> 27<script> 28 var app = new Vue({ 29 el: '#app', 30 data: { 31 display: true, 32 todo: { 33 completed: false 34 } 35 } 36 }) 37</script> 38</body> 39</html>

でもエラー出ます

投稿2020/05/20 13:39

rururu3

総合スコア5545

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

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

退会済みユーザー

退会済みユーザー

2020/05/21 01:01

分かりやすい解説ありがとうございます。 ベストアンサーにさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問