【问题标题】:Angular 12 upgrade error: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this fileAngular 12 升级错误:您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件
【发布时间】:2021-09-30 20:44:15
【问题描述】:

我已经从 Angular 11 升级到 12,我的许多 Angular 组件都低于错误。

./src/app/virtualspaces/home/home.component.html:1:0 - Error: Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

./src/app/virtualspaces/home/home.component

<div id="enterVirtualSpaces">
  <div class="bgEls">
    <div class="_bgEl1"></div>
    <div class="_bgEl2"></div>
  </div>
  <header>
    <div class="null1"><a class="logo" href="/enter"></a></div>
  </header>
  <div class="maincontent">
    <h2>Enter</h2>
    <a href="/enter/meetings">Meetings</a>
    <a href="/enter/conferences">Conferences</a>
    <a href="/enter/xpos">Expos</a>
  </div>
</div>

我知道 Angular 从 Angular 10 中停止使用 webpack,但我尝试尝试使用 npm i -D @expo/webpack-config 安装 webpack 并在根目录添加 webpack.config.js 文件并添加以下代码:

const createExpoWebpackConfigAsync = require('@expo/webpack-config');

module.exports = async function(env, argv) {
    const config = await createExpoWebpackConfigAsync({
        ...env,
        babel: {
            dangerouslyAddModulePathsToTranspile: ['@ui-kitten/components']
        }
    }, argv);
    return config;
};

没用。

我想了解问题的原因以及解决问题的可能方法,因为在我的 Angular 11 项目中运行良好的许多组件都显示错误并且不允许项目再次编译。

更新 1

使用 ng g c componentPath 生成一个新文件。新文件会立即添加到引发错误的文件中。

我在一些组件中加载了动态组件,这些组件甚至与文件抛出错误无关。这可能是一个可能的来源吗?

另外,我创建了一个新的 Angular 12 项目并逐渐更新文件以查看是否出现错误。

最初我不会收到新生成的组件的错误,直到我复制我的组件文件夹。除了动态导入的动态组件之外,每个组件都是使用 Angular cli 生成的。

这是我动态导入组件的打字稿文件之一

post-box3.component.ts


import {
  Component,
  OnInit,
  Input,
  Output,
  Renderer2,
  ViewChild,
  ElementRef,
  ViewContainerRef,
  ComponentFactoryResolver,
} from "@angular/core";

@Component({
  selector: "app-post-box3",
  templateUrl: "./post-box3.component.html",
  styleUrls: ["./post-box3.component.scss"],
})
export class PostBox3Component implements OnInit {
  @ViewChild("postContent", { read: ViewContainerRef }) postContentEl!: ViewContainerRef;
  constructor(
    private resolver: ComponentFactoryResolver,
    private vcRef: ViewContainerRef,
    private elmRef: ElementRef
  ) {
    this.dynLoadBIComp();
  }

  ngOnInit(): void {}

  async dynLoadBIComp({ 
    const compObj: any = await import(
      "../../../../templates/logo/1/logo1b/logo1b.component"
    );

    const compKeys: any = Object.keys(compObj);    
    this.postContentEl.createComponent(
      this.resolver.resolveComponentFactory(compObj[compKeys[0]])
    );;
  }
}


【问题讨论】:

  • 刚刚也遇到了这个问题——尝试将第 3 方包“供应商”到我现有的 Angular 项目中。你有没有发现问题所在?

标签: javascript angular typescript webpack angular12


【解决方案1】:

您的html 是如何加载到应用程序中的?我将假设它不是一个模板。当我们通过require 加载文件(在我们的例子中是图像)时,我们遇到了这个问题。例如,有问题的陈述是

photopath = somepath ? somepath : require('../assets/image-not-found.png');

这引发了与您的相同的异常 - 编译器预期的 JavaScript 代码和 &lt; 是意外的标记。我们用简单的'../assets/image-not-found.png' 替换了require,它解决了这个问题。那么,回到我的第一个问题 - 你如何加载你的 html?

【讨论】:

  • 我基本上创建了一个组件 'ng g c componentPath' 。如果我在另一个文件中但在同一目录中动态加载组件,是否会发生此错误?因为我确实在一些文件中动态加载了组件,这些文件甚至与错误中显示的文件无关。现在,如果我只生成任何组件,它们都会包含在错误中。在 Angular 11 中并非如此
  • 是的,这也是我们所拥有的......动态加载变得更加困难
  • 好的,我不应该再次动态加载组件吗?或者加载动态组件并且没有这些几乎无法追踪的错误的解决方法是什么?它抱怨项目中几乎所有的 html。还是我应该降级回 Angular 11?
  • 你能显示如何你正在加载吗?你说的是dynamically,但它可能意味着很多东西。我们将 HTML 作为模板加载,所以这从来都不是问题。或者,您可以将 HTML 加载到 TypeScript 变量中,并将其绑定到模板。最终,是的——我们无法找到从代码中动态加载文件的方法。有一些解释为什么它是设计的,为什么它更好,更安全 - 所以你最好继续使用该程序,否则你将被困在 Angular 11
  • 更新:我看到import() - 是的,不会工作。有一个使用 Angular 元素 angular.io/guide/elements 的替代方法,但对于简单的情况(例如,如果组件的名称在您的情况下是一个常量),您可能希望使其成为模板的一部分而不是 .ts 文件跨度>
猜你喜欢
  • 2021-04-19
  • 2021-08-11
  • 2020-01-15
  • 2021-11-21
  • 1970-01-01
  • 2021-02-26
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多