【问题标题】:Angular Uncaught Error: Template parse errors: is not a known element角度未捕获错误:模板解析错误:不是已知元素
【发布时间】:2020-11-27 15:43:09
【问题描述】:

我正在尝试在反应应用程序中独立使用 babel 来转换 Angular TypeScript。转译确实“有效”,但是当我尝试导入组件并在另一个组件的模板中使用它的选择器时出现错误。

这是我得到的错误:

Uncaught Error: Template parse errors:
'search-bar' is not a known element:
1. If 'search-bar' is an Angular component, then verify that it is part of this module.
2. If 'search-bar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

SearchBar 组件(用户代码):

// ng is on the window object
const { Component, NgModule, Output, EventEmitter } = ng.core;
const { CommonModule } = ng.common;

@Component({
  selector: 'search-bar',
  template: `<div>Search Bar</div>`,
})
@NgModule({
  declarations: [SearchBar],
  exports: [SearchBar]
})
class SearchBar {}

export default SearchBar;

应用组件(用户代码):

const { Component, NgModule, VERSION } = ng.core;
const { CommonModule } = ng.common;

// Note: The online editor does not need a path to the component :)
import SearchBar from 'SearchBar.ts';

@NgModule({
  imports: [
    CommonModule,
    SearchBar,
  ],
  declarations: [AppComponent, SearchBar],
})
@Component({
  selector: 'my-app',
  template: `<search-bar></search-bar>`
})
export default class AppComponent {

}

请注意,我正在创建一个在线代码编辑器,所以是的,我需要单独使用 babel。这是我的 typescript 的 babel 配置:

(来自我的反应应用)

const TS_OPTIONS = {
  presets: [
    ['typescript', { allExtensions: true }],
    ['es2017', { 'modules': false }],
  ],
  plugins: [
    ["proposal-decorators", { "legacy": true }],
    ["proposal-class-properties", { "loose" : true }],
    'transform-modules-commonjs',
  ],
};

Babel 版本:

https://unpkg.com/@babel/standalone@7.12.9/babel.min.js

我的 transpile 函数(简化,也来自我的 react 应用程序):

export default function transpile(myCode) {
  const { code } = Babel.transform(myCode, TS_OPTIONS);

  return myCode;
}

我错过了什么?

【问题讨论】:

    标签: angular typescript babeljs


    【解决方案1】:

    在此过程中,我把事情搞得一团糟,CodeMonkey 正确地指出我的组件中不应该有 NgModule。我会把答案归功于他,但只是为了完整性,以防有人想看到一个可行的例子。请注意,我的在线代码编辑器有一些特定的语法(例如导入组件时不需要文件路径)。

    带模块的主要组件:

    // main.ts
    // contains my module
    const { Component, VERSION } = ng.core;
    const { BrowserModule } = ng.platformBrowser;
    const { NgModule } = ng.core;
    const { CommonModule } = ng.common;
    
    import AppComponent from 'App.ts';
    import SearchBarComponent from 'SearchBar.ts';
    
    @NgModule({
      imports: [
        BrowserModule,
        CommonModule,
      ],
      declarations: [AppComponent, SearchBarComponent],
      bootstrap: [AppComponent],
      providers: []
    })
    class AppModule {}
    
    const { platformBrowserDynamic } = ng.platformBrowserDynamic;
    
    platformBrowserDynamic()
      .bootstrapModule(AppModule)
      .catch(err => console.error(err));
    

    应用组件:

    // App.ts
    const { Component } = ng.core;
    
    @Component({
      selector: 'my-app',
      template: `
      <h1 [innerHTML]="title"></h1>
      <search-bar></search-bar>`
    })
    export default class AppComponent {
      title;
    
      constructor() {}
    
      ngOnInit() {
        this.title= "Hello World!";
      }
    }
    

    搜索栏组件:

    // SearchBar.ts
    const { Component } = ng.core;
    
    @Component({
      selector: 'search-bar',
      template: `<h1>Search Bar</h1>`
    })
    export default class SearchBarComponent {}
    
    

    【讨论】:

      【解决方案2】:

      我看到这段代码的第一个想法是我没有遇到一个既是模块又是组件的类,我的感觉是它们需要是两个独立的类。

      // ng is on the window object
      const { Component, NgModule, Output, EventEmitter } = ng.core;
      
      @Component({
        selector: 'search-bar',
        template: `<div>Search Bar</div>`,
      })
      export class SearchBar {}
      
      @NgModule({
        declarations: [SearchBar],
        exports: [SearchBar]
      })
      export class SearchModule {}
      

      你有在其他地方工作的双重用途课程的例子吗?

      【讨论】:

      • 感谢您的回复。在您的示例中,您建议我如何在 AppComponent 中使用 SearchBar 或 SearchModule?
      • 我认为你是对的,我不应该在 App 或 SearchBar 组件中使用 NgModule
      • @cbutler 很抱歉没能回复你,很高兴你把它整理好了 :)
      猜你喜欢
      • 2018-10-11
      • 2021-03-25
      • 2018-05-24
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2020-03-21
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多