【问题标题】:Why does this vue component not render为什么这个vue组件不渲染
【发布时间】:2021-01-24 22:38:44
【问题描述】:

我已经用头撞墙了 3 个小时了。这个最简单的可能组件不会渲染:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
    app
    <container></container>
</div>

<script type="text/x-template" id="container-template">
<div class="container">
    container
    {{this.foo}}
</div>
</script>

<script>
const app = new Vue({
    el: "#app",
    data: {foo:"bar"},
    created: function(){
        console.log("app created")
    },
});

Vue.component('container', {
    template: '#container-template',
    data: function(){
        return {foo:"bar"}
    },
    created: function(){
        console.log("container created")
    },
});
</script>

使用Unknown custom element: &lt;container&gt; 运行 sn-p 错误。但是将 x-template 放入 #app 也无济于事。

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    您在创建“应用程序”时正在使用容器组件,但容器组件尚未注册。只需切换语句,它就会起作用。

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    
    <div id="app">
        app
        <container></container>
    </div>
    
    <script type="text/x-template" id="container-template">
    <div class="container">
        container
        {{this.foo}}
    </div>
    </script>
    
    <script>
    Vue.component('container', {
        template: '#container-template',
        data: function(){
            return {foo:"bar"}
        },
        created: function(){
            console.log("container created")
        },
    });
    
    const app = new Vue({
        el: "#app",
        data: {foo:"bar"},
        created: function(){
            console.log("app created")
        },
    });
    
    </script>

    【讨论】:

    • 如果允许附加问题:在 Web 服务器中运行时,如何在本地获取 vue 调试输出?也很乐意提出新问题
    • 不确定您对vue debug output locally when running in web server 的含义。您的 Vue 应用程序正在浏览器中运行(甚至像 Nuxt.js 这样的 SSR)。因此,如果您想查看调试输出,请打开浏览器控制台/Vue 开发工具。
    猜你喜欢
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2020-10-15
    • 2020-09-15
    • 2016-06-21
    • 2021-10-21
    相关资源
    最近更新 更多