简体   繁体   English

Vue组件没有显示没有错误

[英]Vue component not showing no errors

I am trying to do a very simple vue example and it won't display.我正在尝试做一个非常简单的 vue 示例,它不会显示。 I've done similar things before, but this won't work.我以前做过类似的事情,但这不起作用。

It is an extremely simple task list.这是一个非常简单的任务列表。 It is an input with a submit button that adds an item to a list.它是一个带有提交按钮的输入,用于将项目添加到列表中。 For some reason the component does not render at all.由于某种原因,组件根本不渲染。 I am very lost am supposed to give a presentation on vue.我很迷茫,应该在 vue 上做一个演示。 I was hoping to use this as an example.我希望以此为例。

I'm really not sure what else to say about this, but stack overflow won't let me submit this without typing more information about the issue.我真的不确定对此还有什么要说的,但是堆栈溢出不会让我在不输入有关该问题的更多信息的情况下提交此内容。

<div id="app">
    <task-list></task-list>
</div>


    Vue.component('task-list-item', {
        props: ["task"],
        template: '#task-list-item-template'
    })

    Vue.component('task-list', {
        data: function () {
            return {
                taskList: [],
                newTask: ''
            }
        },
        methods: {
            addTask: function () {
                var self = this;
                if (self.newTask !== ""
                    && self.newTask !== null
                    && typeof self.newTask !== "undefined") {
                    this.taskList.push(self.newTask);
                    this.newTask = "";
                }
            }
        },
        template: '#task-list-template'
    })



    new Vue({
        el: '#app',
        data: function () {
            return {
            }
        }
    })



<script id="task-list-template" type="text/x-template">
    <input v-model="newTask" />
    <button v-on:click="addTask()">Add Task</button>
    <ul>
        <task-list-item v-for="taskItem in taskList"
                        v-bind:task="taskItem">
        </task-list-item>
    </ul>
</script>

<script id="task-list-item-template" type="text/x-template">
    <li>{{task}}</li>
</script>

I am getting no error messages of any kind.我没有收到任何类型的错误消息。

I think the problem is there should be only 1 child under <script id="task-list-template" type="text/x-template"></script> .我认为问题是<script id="task-list-template" type="text/x-template"></script>下应该只有 1 个孩子。 In task-list-template , you have multiple children.task-list-template中,您有多个孩子。 Try to wrap them in 1 div尝试将它们包装在 1 个 div 中

<script id="task-list-template" type="text/x-template">
    <div>
    <input v-model="newTask" />
    <button v-on:click="addTask()">Add Task</button>
    <ul>
        <task-list-item v-for="taskItem in taskList"
                        v-bind:task="taskItem">
        </task-list-item>
    </ul>
  </div>
</script>

Demo on codepencodepen上演示

According to A Single Root Element根据单根元素

Every component must have a single root element每个组件都必须有一个根元素

To fix you can do some thing like:要修复,您可以执行以下操作:

<script id="task-list-template" type="text/x-template">
    <div>
        <input v-model="newTask" />
        <button v-on:click="addTask()">Add Task</button>
        <ul>
            <task-list-item v-for="taskItem in taskList" v-bind:task="taskItem">
            </task-list-item>
        </ul>
    </div>
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM