首页 > 解决方案 > 无法在打字稿中导出类/任何内容 - 意外的令牌导出

问题描述

每次我在 TS 中使用带有意外令牌导出的单词导出时都会出现问题。

使用打字稿。文件具有 ts 扩展名。编辑:VSC。在我购买的 udemy 课程之后安装了 nodejs、angular、typescript 等(太糟糕了,他们没有在那里回答问题)。我是一个巨大的初学者,如果可能的话,我希望得到非常简单的答案(我只知道 Python 和一些 Java)。两台计算机上的问题,3 个不同的操作系统(包括带有 Ubuntu 的 VM)

在我开始之前,我想说我搜索了所有谷歌和类似的问题。阅读它们就像阅读中文一样,我什么也没得到,那些命令也没有帮助。我的问题是这样的:

    console.log("hello world");
let x = 20; 

我运行它的那一刻一切都很好(我通过 vsc 终端,命令 ts-node 运行它)。我在变量之前添加导出的那一刻,我得到了标题中写的错误。这不仅仅是变量的问题。起初我想导出和导入一个类(如在我购买的 udemy 课程的介绍中)。一切正常,直到出口。

我创建了 Angular ng 应用程序(就像 Djnago 中的项目一样),但从未更改文件中的任何内容 - 在介绍期间,我在名为 sandbox 的目录中创建 ts 文件并使用 ts-node 命令运行它们。一切正常,直到出口。它甚至没有导入问题,尝试导出某些内容足以给出该错误。就像下面这样:

console.log("hello world");
export let x = 20; 

(我记得我写的(文件在我的虚拟机中,无法复制它们)但语法是正确的。或者至少 - 与我的 Udemy 课程相同。其他人也有这个问题(没有人得到答复唉)。

这是我关于 SO 的第一篇文章。我只在必要时才使用它,而且我什至不需要创建一个帐户并询问任何事情。但这一次我完全迷失了。如果有人可以,请帮助我。我在教程中做所有事情,我知道为什么它不工作。Idk 关于不同版本的 JS、框架等。我只是简单地按照应该工作的说明进行操作,但它们没有。如有必要,我可以准确地写下我所做的一切、我是如何安装它的或提供任何其他信息。请帮忙。

顺便说一句:Angular 10.1.6,npm:6.14.4,tsc:4.0.3。windows 和 linux (ubuntu) 都给出相同的错误。编辑:VSC。昨天安装了 Angular、node.js 和所有东西(今天在 ubuntu 上,唯一的区别是 sudo 在 npm install 命令之前)所以它应该都是最新的(如果不是,我很惊讶)。

编辑:

谢谢你的答案。这是该类的导出。也不行:

export class Cust extends Persona {
constructor(name:string, age:number, private advisor:string)
{
  super(name, age);  
}

welcome(): string {

    return `Hello from ${this.name}, advised by ${this.advisor}}`;
}  

}

一切都像在 Udemy 课程中一样...... :(

编辑2:

包.json:

{
  "name": "test",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.1.5",
    "@angular/common": "~10.1.5",
    "@angular/compiler": "~10.1.5",
    "@angular/core": "~10.1.5",
    "@angular/forms": "~10.1.5",
    "@angular/platform-browser": "~10.1.5",
    "@angular/platform-browser-dynamic": "~10.1.5",
    "@angular/router": "~10.1.5",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1001.6",
    "@angular/cli": "~10.1.6",
    "@angular/compiler-cli": "~10.1.5",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
}

EDIT3:tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

PS.那些是我电脑里的文件,我不能从我的虚拟机复制粘贴文件。但它也不适用于计算机和VM。以下是我提供示例的设备的配置。

角度:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "test": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/test",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "test:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "test:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "test:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "test:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "test:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "test"
}

编辑:我从来没有想过在 TS 中运行简单的导出命令需要计算机科学博士学位......

标签: javascriptangulartypescript

解决方案


编辑:

这并没有解决问题,@Konrad J. 无法使用export const,或者export class我很茫然!

原始回复:

因为你是初学者,所以我不会深入细节,但你不能export let。但是你可以export const x = 20;

如果您想了解详细信息,请查看:


推荐阅读