Vue.js访问父组件

Jam*_*mie 5 javascript vue.js

我有一个具有这种方法的子组件:

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);
}
Run Code Online (Sandbox Code Playgroud)

所以我想在我的父组件中显示一个加载圈我像这样访问它:

this.$parent.loading
Run Code Online (Sandbox Code Playgroud)

(我的父组件有一个名为的数据属性loading)

但是当我尝试使用gulp编译时,我会收到:

[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 |             },
Run Code Online (Sandbox Code Playgroud)

这可能有什么问题?

(我正在使用Vue.js 1.0)

Pat*_*ele 10

你在一个承诺设置的中间放了一个新的声明.您需要重新排列代码:

this.$parent.loading = true;
MessageService.subscriptions()
        .then((data) => {
Run Code Online (Sandbox Code Playgroud)

但是,我不会像这样直接访问父母.它在父级和子级之间创建紧密耦合 - 此组件仅在父级公开loading标志时才起作用.

相反,查看自定义事件.