Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
变量声明Javascript AngularJS中出现意外标记_Javascript_Angularjs_Gulp - Fatal编程技术网

变量声明Javascript AngularJS中出现意外标记

变量声明Javascript AngularJS中出现意外标记,javascript,angularjs,gulp,Javascript,Angularjs,Gulp,我试图在Angular中创建一个服务,但我的编译器(Gulp)似乎与我在开始时定义的变量有问题。确切的错误是模块构建失败:SyntaxError:C:/*PATH*/src/app/components/demo.service.js:意外令牌(8:8),它指向的代码是:var demos=[新演示(“示例演示”,“示例演示”)],它特别指向变量名中的“d”。对于上下文,以下是整个文件: import { Demo } from './interfaces/demo'; export clas

我试图在Angular中创建一个服务,但我的编译器(Gulp)似乎与我在开始时定义的变量有问题。确切的错误是
模块构建失败:SyntaxError:C:/*PATH*/src/app/components/demo.service.js:意外令牌(8:8)
,它指向的代码是:
var demos=[新演示(“示例演示”,“示例演示”)],它特别指向变量名中的“d”。对于上下文,以下是整个文件:

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;
    }
}
从“/interfaces/Demo”导入{Demo};
导出类拆卸服务{
构造函数(){
"ngInject",;
}
var demos=[新演示(“示例演示”、“示例演示”)];
addDemo(名称,html){
this.demos.push(新的Demo(name,html));
}
removeDemo(姓名){
this.demos.splice(this.demos.indexOf(name),1);
}
updateDemo(旧名称、新名称、html){
this.demos[this.demos.indexOf(oldName)].setName(newName);
this.demos[this.demos.indexOf(oldName)].setHtml(html);
}
getDemoInfo(名称){
返回[this.demos[this.demos.indexOf(name)].getName(),this.demos[this.demos.indexOf(name)].getHtml();
}
getDemos(){
返回这个.demos;
}
}

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

您必须将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;
}
mport{Demo}来自“/interfaces/Demo”;
导出类拆卸服务{
构造函数(){
"ngInject",;
}
私有演示:any=[新演示(“示例演示”、“示例演示”)];
addDemo(名称,html){
this.demos.push(新的Demo(name,html));
}
removeDemo(姓名){
this.demos.splice(this.demos.indexOf(name),1);
}
updateDemo(旧名称、新名称、html){
this.demos[this.demos.indexOf(oldName)].setName(newName);
this.demos[this.demos.indexOf(oldName)].setHtml(html);
}
getDemoInfo(名称){
返回[this.demos[this.demos.indexOf(name)].getName(),this.demos[this.demos.indexOf(name)].getHtml();
}
getDemos(){
返回这个.demos;
}

}这是因为您不能像在ES6中声明演示那样声明变量

移动此行:

 var demos = [new Demo("Example Demo", "<b>Example Demo</b>")];
var demos=[新演示(“示例演示”、“示例演示”)];
像这样进入构造函数:

 constructor() {
        'ngInject';
      this.demos = [new Demo("Example Demo", "<b>Example Demo</b>")];
    }
constructor(){
"ngInject",;
this.demos=[新演示(“示例演示”、“示例演示”)];
}

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

这样做可以去掉构造函数之外的任何“var demos”行,对吗?我现在有另一个问题:告诉控制台从另一个类记录getDemos方法返回“undefined”。如果您愿意,我可以提供Demo类,您可以去掉构造函数之外的任何var Demo,因为变量只能在构造函数或函数中的类中定义。对于你的其他问题,你能提供一个代码的样本,让我可以看到它;求你了,谢谢,不过我最后还是修好了。谢谢你的帮助@Wubbalubbaudub很高兴你能工作,没问题。