简体   繁体   English

尝试将组件添加到模块文件时,Stackblitz 上的导入错误

[英]Import Error on Stackblitz when trying to add component to module file

I'm using Stackblitz to practice angularJS and it's coming up with an error when I attempt to import a component in the app.module.ts file.我正在使用 Stackblitz 练习 angularJS,当我尝试在 app.module.ts 文件中导入组件时出现错误。

Import error, can't find file:
./heroes/heroes.component

app.module.ts app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { HeroesComponent } from './heroes/heroes.component';

https://stackblitz.com/edit/angular-akxx4e?file=src%2Fapp%2Fapp.module.ts https://stackblitz.com/edit/angular-akxx4e?file=src%2Fapp%2Fapp.module.ts

Update below Changes, because it's in different folder更新下面的更改,因为它在不同的文件夹中

import { HeroesComponent } from '../heroes/heroes.component';

Method 1方法一

This issue occurs due to component creation inside wrong location.由于在错误位置内创建组件,会出现此问题。 So heroes component should be created inside app root.所以应该在app根目录中创建heroes component This is the recommended answer.这是推荐的答案。


Method 2方法二

Correcting component's relative path is also works.更正组件的相对路径也是可行的。 But it is not the correct answer.但这不是正确的答案。

import { HeroesComponent } from '../heroes/heroes.component';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试导入 html 文件时出现 Typescript 错误 - Typescript error when trying to import an html file Angular2,尝试添加新模块时出错 - Angular2, error when trying to add new module 尝试导入项目时出现错误“对每个 HTML 规范的模块脚本强制执行严格的 MIME 类型检查” - Getting error "Strict MIME type checking is enforced for module scripts per HTML spec" when trying to import project 为什么在尝试导入 CSS 文件时出现 SyntaxError:导入声明可能只出现在模块的顶层? - Why am I getting SyntaxError: import declarations may only appear at top level of a module, when trying to import a CSS file? 尝试导入时收到 Javascript 错误“未捕获的语法错误:无法在模块外使用导入语句”。 找到解决方案 - Receiving Javascript error "Uncaught SyntaxError: cannot use import statement outside a module" when trying to import. Solution found 尝试在jQuery中将类添加到父级时出错 - Error when trying to add class to parent in jQuery Static_pages中的Sass :: SyntaxError #home - 无效的CSS在“”之后:尝试将代码添加到CSS文件时出错 - Sass::SyntaxError in Static_pages#home - Invalid CSS After “”: error when trying to add code to a CSS file 尝试将外部脚本附加到Vue中的单个文件组件时出错 - Error trying to attach external script to single file component in Vue 尝试使用 three.js 时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when trying to use three.js 在Vue组件中导入javascript文件 - Import javascript file in Vue component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM