变量声明Javascript AngularJS中的意外令牌

时间:2017-06-20 16:13:32

标签: javascript angularjs gulp

我试图在Angular中创建一个服务,但是我的编译器(Gulp)似乎遇到了我在开头定义的变量的问题。确切的错误是Module build failed: SyntaxError: C:/*PATH*/src/app/components/demo.service.js: Unexpected token (8:8),它指向的代码是:var demos = [new Demo("Example Demo", "<b>Example Demo</b>")];,它专门指向&#34; d&#34;在变量名称中。对于上下文,这里是整个文件:

import { Demo } from './interfaces/demo';

export class DemosService {
    constructor() {
        'ngInject';
    }

    var demos = [new Demo("Example Demo", "<b>Example Demo</b>")];

    addDemo(name, html) {
        this.demos.push(new Demo(name, html));
    }

    removeDemo(name) {
        this.demos.splice(this.demos.indexOf(name), 1);
    }

    updateDemo(oldName, newName, html) {
        this.demos[this.demos.indexOf(oldName)].setName(newName);
        this.demos[this.demos.indexOf(oldName)].setHtml(html);
    }

    getDemoInfo(name) {
        return [this.demos[this.demos.indexOf(name)].getName(), this.demos[this.demos.indexOf(name)].getHtml()];
    }

    getDemos() {
        return this.demos;
    }
}

我确定这是一个非常愚蠢的问题,解决方案非常简单,但我似乎无法找到它。提前谢谢大家!

2 个答案:

答案 0 :(得分:0)

你必须将demos变量声明为类变量,所以如果你改变这样的代码,它应该没问题

mport { Demo } from './interfaces/demo';

export class DemosService {
constructor() {
    'ngInject';
}

private demos:any = [new Demo("Example Demo", "<b>Example Demo</b>")];

addDemo(name, html) {
    this.demos.push(new Demo(name, html));
}

removeDemo(name) {
    this.demos.splice(this.demos.indexOf(name), 1);
}

updateDemo(oldName, newName, html) {
    this.demos[this.demos.indexOf(oldName)].setName(newName);
    this.demos[this.demos.indexOf(oldName)].setHtml(html);
}

getDemoInfo(name) {
    return [this.demos[this.demos.indexOf(name)].getName(), this.demos[this.demos.indexOf(name)].getHtml()];
}

getDemos() {
    return this.demos;
}

}

答案 1 :(得分:0)

这是因为您无法按照ES6中声明的演示方式声明变量。

移动此行:

library(dplyr)
library(tidyr)

        # add grouping variables we'll need further down
df %>% mutate(group = 1:4) %>% 
        # reshape data to long format
        gather(question,count,-group,-totalN) %>%
        # add totals by question to df
        group_by(question) %>%
        mutate(answers = sum(totalN),
               yes = sum(count)) %>%
        # calculate z-scores by group against total
        group_by(group,question) %>%
        summarise(z_score = zScore(count, totalN, yes, answers)) %>%
        # spread to wide format
        spread(question, z_score)
## A tibble: 4 x 4
#  group       var1       var2      var3
#* <int>      <dbl>      <dbl>     <dbl>
#1     1  0.6162943 -2.1978303  1.979278
#2     2  0.6125615 -0.7505797  1.311001
#3     3 -3.9106430  2.6607258 -4.232391
#4     4  2.9995381  0.4712734  0.438899

进入你的构造函数:

 var demos = [new Demo("Example Demo", "<b>Example Demo</b>")];

然后,当您需要访问演示时,请确保调用this.demos。错误是因为在ES6中的类中使用作用域的方式。