简体   繁体   English

Angular 2:找不到模块:错误:无法解析

[英]Angular 2: Module not found: Error: Can't resolve

I have a simple app created with Angular-CLI, I wanted to refactor and move the app.component.ts code in separate component file and started to get a nasty error:我有一个使用 Angular-CLI 创建的简单应用程序,我想在单独的组件文件中重构和移动 app.component.ts 代码并开始出现令人讨厌的错误:

Module not found: Error: Can't resolve on './sb/sb.component.html' ( my SBComponent).找不到模块:错误:无法解析“./sb/sb.component.html”(我的 SBComponent)。 Under the在下面

This what i have:这是我所拥有的:

app.module:应用模块:

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

import { AppComponent } from './app.component';
import { SBComponent  } from './sb/sb.component'

@NgModule({
  declarations: [
    AppComponent, SBComponent  
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component:应用程序组件:

import { Component } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  {


}

new component:新组件:

import { Component, OnInit } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit, AfterContentInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@Component({

    selector: 'app-sb',
    templateUrl: './sb/sb.component.html'
})
export class SBComponent  {



}

Any help would be greatly appreciated!任何帮助将不胜感激!

If your component is inside the same directory, then you need to point your templateUrl to the same folder as well.如果您的组件在同一个目录中,那么您还需要将templateUrl指向同一个文件夹。 Here is how you can do it:您可以这样做:

@Component({
    selector: 'app-sb',
    templateUrl: './sb.component.html'
})

This problem is very well known, when we are using component relative paths for external html and css files.这个问题是众所周知的,当我们为外部 html 和 css 文件使用组件相对路径时。 The problem of absolute path and relative path can be resolved just by adding metadata ModuleId in component.只需在组件中添加元数据ModuleId即可解决绝对路径和相对路径的问题。

Now what does ModuleId do?现在 ModuleId 做了什么? ModuleId contains the absolute URL of the component class [when the module file is actually loaded] ModuleId 包含组件类的绝对 URL [实际加载模块文件时]

This ModuleId value is used by the Angular reflection processes and the metadata_resolver component to evaluate the fully-qualified component path before the component is constructed Angular 反射过程和 metadata_resolver 组件使用此ModuleId值在构建组件之前评估完全限定的组件路径

It's very easy to use.它非常容易使用。 Just add one more entry in @Component decorative.只需在@Component 装饰中再添加一项。 Full code example is below.完整的代码示例如下。

 @Component({
  moduleId: module.id,    // fully resolved filename; defined at module load time
  selector: 'app-sb',
  templateUrl: 'sb.component.html' //Observe this.
})
export class SBComponent  {

}

My problem was the same as questioned here, my solution was quite different我的问题与这里提出的问题相同,我的解决方案完全不同

code that was causing problem:导致问题的代码:

@Component({
    selector : 'app-header',
    templateUrl : './header.component.html',
    styleUrls : ['']
})

here by changing :在这里通过改变:

styleUrls : ['']

to this:对此:

styles : ['']

helped me remove module not found error, thought someone might get this error due to this silly mistake, that's why shared my solution帮我删除了未找到模块的错误,认为有人可能会因为这个愚蠢的错误而得到这个错误,这就是为什么分享我的解决方案

Try to give relative path instead of absolute one.尝试给出相对路径而不是绝对路径。

It will fix the Solution.它将修复解决方案。

Like : templateUrl = '../employee/employee.html' in your component.ts就像:你的component.ts templateUrl = '../employee/employee.html'

Ok, above solutions are excellent and must be used to see if it fixes your issue.好的,上述解决方案非常好,必须用于查看它是否解决了您的问题。 But the same error message can be displayed if you have set the Template property.但如果您设置了 Template 属性,则会显示相同的错误消息。 I had the issue when I created the component using ng gc and then it had templateUrl but I was writing some test html inside of templateUrl, thinking it is template.当我使用 ng gc 创建组件然后它有 templateUrl 时遇到了问题,但我在 templateUrl 中编写了一些测试 html,认为它是模板。 Hope this helps希望这可以帮助

Something a react developer showed me.反应开发人员向我展示的东西。 Go to your tsconfig.json and inside the outer curly braces {} where you have all the json options and codes, put this转到您的 tsconfig.json 并在外部大括号 {} 内,您拥有所有 json 选项和代码,将其放入

"include": [
    "src/app"
]
//if all your components files are in app, you can do this so as to make importation easy. You can just call the component. It should work for you.

from here you can easily use import without much slashes / and dots.从这里您可以轻松使用导入而无需太多斜线 / 和点。 like in my code,就像在我的代码中一样

import { Component, OnInit, Input, Injectable, NgModule } from '@angular/core';
import { products } from "product";
//instead of import { products } from ".../../product"; which will still import but might show error.
import { FormsModule } from '@angular/forms';




@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.scss'],
  providers:  [
    FormsModule
  ]
})



export class ProductListComponent implements OnInit {

 products = products;


onNotify() {
  window.alert("You will be notified when the product goes on discount")
}

share() {
  window.alert('The Product Has Been Added To Your Cart')
}


  constructor() {

   }

  ngOnInit(): void {
  }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM