javascript - vue组件在加载页面时不显示。如何让它显示?

标签 javascript vuejs2 vue-component

<project-comp></project-comp>加载页面时组件不显示。我设置了projects使用 created 的属性 Hook 。但它不起作用。

这是我的代码:

Vue.component('project-comp',{
        template: `
        <div class='box'>            
            <div v-for='one in this.$parent.projects'>
               <h2> @{{one.place}}</h2>
               <h2> @{{one.time}}</h2>
            </div>
        </div>
        `,
});

var app = new Vue({
        el: '#root',

        props:['projects'],

        created: function () {
            $.post(
                'url',
                {'city':'beijing'},
                function(data){
                    this.projects = data;
                },
                'json'
            );
        },


});

最佳答案

似乎这个范围不正确,你应该有它:

var app = new Vue({
        el: '#root',    
        props:['projects'],
        created: function () {
            var self = this
            $.post(
                'url',
                {'city':'beijing'},
                function(data){
                    self.projects = data;
                },
                'json'
            );
        },
});

对于 ES-6,它可以是:

var app = new Vue({
        el: '#root',    
        props:['projects'],
        created: function () {
            $.post(
                'url',
                {'city':'beijing'},
                (data) => {
                    this.projects = data;
                },
                'json'
            );
        },
});

检查 this进行解释。

关于javascript - vue组件在加载页面时不显示。如何让它显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42597979/

相关文章:

javascript - 无法从 chrome.runtime.sendMessage 访问响应变量。 (关闭?)

html - 如何设置Vue Core UI选择值

json - Vue 无法读取 JSON 响应?

javascript - Vue.js 直接在模板中的 v-for 中引用存储的不好做法?

module - 无模板的组件

Javascript继承类构造函数问题

javascript - 如果某个功能不起作用,则在一段时间后停止该功能

javascript - 如何避免 TypeScript 中的虚假 'unused parameter' 警告

vuejs2 - Vuejs 从路由器 View 到外部组件

vue.js - Nuxt 和 vue 中的 Data() VS asyncData()