vue-resource得到数据后,如何修改组件中data的值?

<div id="app">
    <div id="content">
        <my-nav></my-nav>
    </div>
</div>

//js
var myNav = Vue.extend({
    template: '<div>{{msg}}</div>',
    data: function () {
        return {
            msg:'123',
        }
    },
    mounted:function(){
        this.getNavname();
    },
    methods:{
        getNavname:function(){
        this.$http.get("server/MyServer.ashx")
                .then(
                    function(response){
                    //获取数据后尝试修改组件中msg的值
                        this.$set(this.msg,'hello')
                    //报错:vue.js:1156 Uncaught (in promise) TypeError: Cannot create property 'hello' on string '123'(…)
                        console.log(response)
                    },
                    function(response){
                        console.log(response)
                    });
        }
    }
});
new Vue({
    el:'#app',
    components:{
        'my-nav':myNav
    }
})

新手刚入手,求各位大虾指导,告诉我怎么做可以更好。

组件加载正常,也渲染出了123,但是为什么渲染不出修改后的值,获取数据是成功的。

阅读 15.6k
3 个回答

为什么不用 this.msg = 'hello' ?

this指向问题吧

getNavname:function(){
const that = this
 //其他代码 中的this对应用that 
新手上路,请多包涵

this.$set(this.msg,'hello')
改为
this.$set('msg','hello')

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题