8

我无法构建我的 Angular 应用程序的产品版本。

并且在 IDE 控制台中只有这条消息:

无法分配给引用或变量中的错误!

所以我只能在添加这些选项时构建:

--aot=false --buildOptimizer=false

但是,即使使用这些选项应用程序在部署后在浏览器控制台中显示此消息也会失败:

错误类型错误:无法读取未定义的属性“init”

这可以通过添加一个选项来解决:

--optimization=false 

当我尝试构建时

npm run ng build -- --prod

我有一个包含一些信息的日志文件:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Backend\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\USER_NAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'ng',
1 verbose cli   'build',
1 verbose cli   '--',
1 verbose cli   '--prod' ]
2 info using npm@6.10.2
3 info using node@v10.15.3
4 verbose run-script [ 'preng', 'ng', 'postng' ]
5 info lifecycle dont-play-with-gp-web@0.0.2~preng: dont-play-with-gp-web@0.0.2
6 info lifecycle dont-play-with-gp-web@0.0.2~ng: dont-play-with-gp-web@0.0.2
7 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: unsafe-perm in lifecycle true
8 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: PATH: C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\DEV\WebStormProjects\dont-play-with-gp-web\node_modules\.bin;C:\DEV\WebStormProjects\dont-play-with-gp-web\node_modules\.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Backend\IntelliJ IDEA 2018.3.5\jre64;C:\Backend\PostgreSQL\10\bin;C:\Backend\nodejs;C:\Backend\WinSCP\;C:\Android\sdk\platform-tools;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Users\USER_NAME\AppData\Local\Microsoft\WindowsApps;
9 verbose lifecycle dont-play-with-gp-web@0.0.2~ng: CWD: C:\DEV\WebStormProjects\dont-play-with-gp-web
10 silly lifecycle dont-play-with-gp-web@0.0.2~ng: Args: [ '/d /s /c', 'ng "build" "--prod"' ]
11 silly lifecycle dont-play-with-gp-web@0.0.2~ng: Returned: code: 1  signal: null
12 info lifecycle dont-play-with-gp-web@0.0.2~ng: Failed to exec ng script
13 verbose stack Error: dont-play-with-gp-web@0.0.2 ng: `ng "build" "--prod"`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:326:16)
13 verbose stack     at EventEmitter.emit (events.js:189:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:189:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid dont-play-with-gp-web@0.0.2
15 verbose cwd C:\DEV\WebStormProjects\dont-play-with-gp-web
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\\Backend\\nodejs\\node.exe" "C:\\Users\\USER_NAME\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "ng" "build" "--" "--prod"
18 verbose node v10.15.3
19 verbose npm  v6.10.2
20 error code ELIFECYCLE
21 error errno 1
22 error dont-play-with-gp-web@0.0.2 ng: `ng "build" "--prod"`
22 error Exit status 1
23 error Failed at the dont-play-with-gp-web@0.0.2 ng script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

我试图更新所有内容,但没有帮助。

这是结果ng version

Angular CLI: 8.3.4
Node: 10.15.3
OS: win32 x64
Angular: 8.2.6
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.4
@angular-devkit/build-angular     0.803.4
@angular-devkit/build-optimizer   0.803.4
@angular-devkit/build-webpack     0.803.4
@angular-devkit/core              8.3.4
@angular-devkit/schematics        8.3.4
@angular/cdk                      8.2.0
@angular/cli                      8.3.4
@angular/flex-layout              8.0.0-beta.27
@angular/material                 8.2.0
@ngtools/webpack                  8.3.4
@schematics/angular               8.3.4
@schematics/update                0.803.4
rxjs                              6.5.3
typescript                        3.5.3
webpack                           4.39.2

我的package.json

{
  "name": "dont-play-with-gp-web",
  "version": "0.0.2",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^8.2.6",
    "@angular/cdk": "^8.2.0",
    "@angular/common": "~8.2.6",
    "@angular/compiler": "~8.2.6",
    "@angular/core": "~8.2.6",
    "@angular/flex-layout": "8.0.0-beta.27",
    "@angular/forms": "~8.2.6",
    "@angular/material": "^8.2.0",
    "@angular/platform-browser": "~8.2.6",
    "@angular/platform-browser-dynamic": "~8.2.6",
    "@angular/router": "~8.2.6",
    "angular-markdown-editor": "^2.0.2",
    "hammerjs": "^2.0.8",
    "jquery": "^3.4.1",
    "ngx-infinite-scroll": "^8.0.0",
    "ngx-markdown": "^8.1.0",
    "ngx-material-file-input": "^2.0.0",
    "rxjs": "~6.5.3",
    "tslib": "^1.9.0",
    "zone.js": "^0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.4",
    "@angular/cli": "~8.3.4",
    "@angular/compiler-cli": "~8.2.6",
    "@angular/language-service": "~8.2.6",
    "@types/jasmine": "~3.4.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.31",
    "@types/node": "~12.7.5",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~8.4.1",
    "tslint": "~5.20.0",
    "typescript": "^3.5.3"
  }
}

那么,在这种情况下我能做些什么呢?如何找到错误的原因以及如何修复它?

如果它可以帮助 - 这是我的源代码:

https://bitbucket.org/mohaxspb/gp-web/commits/tag/ATTEMPT_TO_FIX_PROD_BUILD_WITH_DEPS_UPDATE


同样的问题ru.stackoverflowhttps ://ru.stackoverflow.com/questions/1025395


解决:

最后我有错误原因(所以用户https://stackoverflow.com/users/10123947/shadow找到它):尝试更改模板中变量的值,在自制指令的帮助下在模板中定义。

这里的问题是错误消息。提到的用户仅通过调试编译器(据我了解)收到详细信息。有一些细节(俄语):https ://ru.stackoverflow.com/a/1025417/17609

更新:

我将问题和 PR 添加到 Angular 存储库以修复它。

  1. 错误消息中没有详细信息:问题公关
  2. 为模板变量赋值会导致产品构建崩溃:问题公关
4

4 回答 4

25

碰巧来自 AST 解析器的大多数异常都令人头疼。只是因为它们没有携带任何有用的东西,除了一条消息:

无法分配给引用或变量中的错误!

哦真的吗?

例如,链接器 ( ViewBuilder) 总是写它不能构建模板表达式的地方。

谷歌搜索可能是解决方案之一。但是整个圈子都将关闭,因为大多数答案都会提供检查ngModel模板中指令的用法。


第二个选项有点硬编码。值得进入编译器本身并进行调试。Angularnode_modules/@angular/compiler/bundles/compiler.umd.js在编译期间使用。

让我们打开这个文件并尝试搜索文本Cannot assign to a reference or variable!。我们将找到_AstToIrVisitor类和visitPropertyWrite方法。有这么一行代码

// Otherwise it's an error.
throw new Error('Cannot assign to a reference or variable!');

抛出异常,根本没有任何信息

让我们console.log(ast)在异常之前添加,函数ast的参数在哪里visitPropertyWrite。解析器会将类的一个实例记录PropertyWrite到控制台:

PropertyWrite {
  span: ParseSpan { start: 0, end: 73 },
  receiver: ImplicitReceiver { span: ParseSpan { start: 0, end: 0 } },
  name: 'translation',
  value: MethodCall {
    span: ParseSpan { start: 14, end: 73 },
    receiver: ImplicitReceiver { span: [ParseSpan] },
    name: 'getTranslationForLanguageFromArticle',
    args: [ [PropertyRead], [PropertyRead] ]
  }
}

我们得出的结论是,我们必须了解translation属性和getTranslationForLanguageFromArticle方法调用之间的关系。当我试图从 Bitbucket 克隆您的示例时,我查找了“getTranslationForLanguageFromArticle”用法,并在以下行中找到了这一行feed.component.html

(change)="translation = getTranslationForLanguageFromArticle($event.value, article)"

这是解析器无法理解的,因为translation它不是类属性。它是在指令中隐式访问的局部ng-template变量。*ngVar

我会changeTranslation在组件内部创建一些方法并在article.translations[indexOfCorrectArticleTranslation(article)]那里进行操作。

至少你知道该走哪条路:)

于 2019-09-17T19:48:59.680 回答
3

如果我可以添加...当我将模板控件命名为与我的类的绑定属性相同时,我得到了这个错误。像这样:

<input type="password" class="form-control" name="passwordConfirmation" #passwordConfirmation="ngModel" [(ngModel)]="passwordConfirmation"/>

“passwordConfirmation”是模板中控件的名称和组件类的属性/变量的名称。这似乎让编译器感到困惑,因为它试图绑定到模板变量而不是类属性。

于 2020-03-05T13:39:58.547 回答
2

看起来像是给出问题的变量的名称之一,很难假设是哪个变量给出了问题,并且为这里的任何人进行调试会耗费大量时间。我建议您检查 all ngIf, ngModel, ngFor, 等...很可能您正在尝试在项目中的某个位置使用相同的变量名称。

于 2019-09-17T17:24:42.997 回答
0

在我的情况下[(ngValue)]是使用而不是[ngValue]

于 2020-08-05T06:50:54.877 回答