【问题标题】:Returned data from post request not passed to vue template从 post 请求返回的数据未传递给 vue 模板
【发布时间】:2019-06-11 22:28:05
【问题描述】:

我有一个由发布请求(称为get_teams 的方法)触发的返回数据,它没有传递给我的 vue 模板。谁能告诉我我做错了什么?我没有将数据正确绑定到我的 vue 模板吗?

app.js

 var app = new Vue({
        el: '#app',
        components: {
            teamsTableTemplate
        },
        data: {
            teams: null,
            editable: true,
            show_teams: false
        },
        methods: {
          get_teams : function(){
            this.reset_show('get_teams')
            $.post(js_local.ajaxurl,{
                action:'get_advisor_teams'
            }).done(function(data){
                this.teams = data
                this.show_teams = true
                console.log(this.teams)
            }).fail(function(data){
                console.log('fail @ { action : get_advisory_teams }')
            })

          }
        }
 })

teams-table-template.js

const teamsTableTemplate = {
template:
`
<table class="table tablesorter">
    <thead class="text-primary">
        <tr></tr>
    </thead>
    <tbody class="">
        <tr v-for="team in teams">
            <td>
                <div class="form-check">
                    <label for="69cd1dbb353338" class="form-check-label">
                        <input id="69cd1dbb353338" type="checkbox" class="form-check-input"><span class="form-check-sign"></span>
                        <!---->
                    </label>
                </div>
            </td>
            <td>
                <p class="title">{{team.team_name}}</p>
                <p class="text-muted">{{team.problem_statement_text}}</p>
            </td>
            <td class="td-actions text-right">
                <button type="button" class="btn btn-link">
                    <!----><i class="tim-icons icon-pencil"></i></button>
            </td>
        </tr>
    </tbody>
</table>
`,
props: ['teams','edit'],
data() {
    return {
    }
}

}

HTML

<div id="app">
    <button @click="get_teams"></button>
    <teams-table-template :teams="teams" :edit="editable" v-if="show_teams" />
</div>

【问题讨论】:

  • 找不到您在组件中调用get_teams 方法的位置...请创建一个工作的codepen 或类似的。它将帮助我们帮助您。
  • 如果您对 vue 有任何想法,那么我必须告诉您,如果您想使用来自 api 的值,则模板的反应性取决于数据属性或计算属性,请改用 vuex 并存储您想在 store 中使用的值,或者如果您只想从模板调用 api 并存储响应,然后在模板的 data 属性中创建一个局部变量并使用它,它将是响应式的。

标签: javascript html vue.js vuejs2 vue-component


【解决方案1】:

我认为this 不是您在 .done() 回调中的组件实例,因为您使用的是简单函数。也许使用箭头功能。

尝试改变:

get_teams() {
        this.reset_show('get_teams')
        $.post(js_local.ajaxurl,{
            action:'get_advisor_teams'
        }).done((data) => {            // use arrow function
            this.teams = data
            this.show_teams = true
            console.log(this.teams)
        }).fail(function(data){
            console.log('fail @ { action : get_advisory_teams }')
        })

      }

// also make sure get_teams() method is invoking from somewhere else
created() {
  this.get_teams();
}

【讨论】:

  • 什么是 get_items()?
  • get_items() -> get_teams(),错字
  • 我的模板中的 v-for 没有输出正确的数据。例如,{{text}} 保持为 {{text}}
  • 你能在不期望输出的地方分享代码吗?
  • 它在teams-table-template.js中,例如对象是teams = [{"cohort":"Cohort 2019","project_id":"405","team_id":"1125","team_name":" Emily Doiafaf","problem_statement_text":"aslfnaksnfkjsbfkj","connector_id":"41"},{"cohort":"Cohort 2019","project_id":"413","team_id":"1133","team_name":"White FridayABS","problem_statement_text":"return return return","connector_id":"41"},{"cohort":"Cohort 2020","project_id":"422","team_id":"1142","team_name":"TEST 2020","problem_statement_text":"AHAHAHAHHAHA","connector_id":"41"}]
【解决方案2】:

像你这样使用回调会导致一些错误,所以我建议使用箭头函数()=&gt;{...} 而不是function(){...} 作为回调,因为你失去了this 的上下文:

get_teams : function(){
    this.reset_show('get_teams')
    $.post(js_local.ajaxurl,{
        action:'get_advisor_teams'
    }).done((data) => {
        this.teams = data
        this.show_teams = true
        console.log(this.teams)
    }).fail(function(data){
        console.log('fail @ { action : get_advisory_teams }')
    })

  }

或通过将this 分配给名为that 的全局变量并在回调上下文中使用它:

      get_teams : function(){
        this.reset_show('get_teams')
        let that=this; 
        $.post(js_local.ajaxurl,{
            action:'get_advisor_teams'
        }).done(function(data){
            that.teams = data
            that.show_teams = true
            console.log(that.teams)
        }).fail(function(data){
            console.log('fail @ { action : get_advisory_teams }')
        })

      }

【讨论】:

  • 我的模板中的 v-for 没有输出正确的数据。例如,{{text}} 保持为 {{text}}
  • 你的组件或vue实例没有正确注册
猜你喜欢
  • 1970-01-01
  • 2019-06-20
  • 2018-06-28
  • 1970-01-01
  • 2020-06-16
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多