最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js access parent component - Stack Overflow

programmeradmin2浏览0评论

I've got a child-component that has this method:

getSubscriptions () {
    MessageService.subscriptions()
            this.$parent.loading = true;
            .then((data) => {
                if (data.data.subscriptions == null) {
                    this.noSubscriptions = true;
                } else {
                    this.subscriptions = data.data.subscriptions;
                }

                this.$parent.loading = false;
            }).bind(this);
}

So I want to show a load circle in my parent component I access it like this:

this.$parent.loading

(My parent component has a data attribute called loading)

But when I try to compile that with gulp ^ I receive:

[14:52:56] Starting 'browserify'...
  46 |                             }
  47 | 
> 48 | t                           this.$parent.loading = false;
     |                             ^
  49 |                         }).bind(this);
  50 |             },
  51 | 
{ SyntaxError: unknown: Unexpected token (48:28)
  46 |                             }
  47 | 
> 48 | t                           this.$parent.loading = false;
     |                             ^
  49 |                         }).bind(this);
  50 |             },

What could be wrong here?

(I'm using Vue.js 1.0)

I've got a child-component that has this method:

getSubscriptions () {
    MessageService.subscriptions()
            this.$parent.loading = true;
            .then((data) => {
                if (data.data.subscriptions == null) {
                    this.noSubscriptions = true;
                } else {
                    this.subscriptions = data.data.subscriptions;
                }

                this.$parent.loading = false;
            }).bind(this);
}

So I want to show a load circle in my parent component I access it like this:

this.$parent.loading

(My parent component has a data attribute called loading)

But when I try to compile that with gulp ^ I receive:

[14:52:56] Starting 'browserify'...
  46 |                             }
  47 | 
> 48 | t                           this.$parent.loading = false;
     |                             ^
  49 |                         }).bind(this);
  50 |             },
  51 | 
{ SyntaxError: unknown: Unexpected token (48:28)
  46 |                             }
  47 | 
> 48 | t                           this.$parent.loading = false;
     |                             ^
  49 |                         }).bind(this);
  50 |             },

What could be wrong here?

(I'm using Vue.js 1.0)

Share Improve this question asked Oct 13, 2016 at 13:06 JamieJamie 10.9k35 gold badges109 silver badges198 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 18

You're putting a new statement in the middle of a promise setup. You need to rearrange the code:

this.$parent.loading = true;
MessageService.subscriptions()
        .then((data) => {

However, I wouldn't directly access the parent like this. It creates a tight coupling between parent and child -- this component will only work if the parent exposes a loading flag.

Instead, look into custom events.

发布评论

评论列表(0)

  1. 暂无评论