Angular 为什么不是';“我的角”;《英雄之旅教程》;不在<;应用程序根目录>;?
无论出于何种原因,当我访问我的开发服务器时,我的Angular应用程序根本没有渲染: 教程和我的代码之间唯一的区别是主题Angular 为什么不是';“我的角”;《英雄之旅教程》;不在<;应用程序根目录>;?,angular,typescript,Angular,Typescript,无论出于何种原因,当我访问我的开发服务器时,我的Angular应用程序根本没有渲染: 教程和我的代码之间唯一的区别是主题 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Angular: Bears From Around The Multiverse</title> <base href="/"> &
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular: Bears From Around The Multiverse</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body>
</html>
根据错误消息,可能还会发生两个与此无关的错误:
DashboardComponent should create
Failed: Template parse errors:
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("<h3>Top Bears</h3>
<div class="grid grid-pad">
<a *ngFor="let bear of bears" class="col-1-4" [ERROR ->]routerLink="/detail/{{bear.id}}">
<div class="module bear">
<h4>{{bear.name}}</h4>
"): ng:///DynamicTestModule/DashboardComponent.html@2:48
仪表板组件应创建
失败:模板分析错误:
无法绑定到“routerLink”,因为它不是“a”的已知属性。(“顶级熊
目前我看到的是我上面提到的页面
编辑:下面有人指出,我可能应该考虑查看开发控制台。这让我看到在中引用bear.id时出现了一个打字错误
将bear:id更改为bear.id允许应用程序完全呈现。始终检查控制台。确保您的app.component.html是否有
将路由模块导入app.module.ts
import { RouterModule } from '@angular/router';
将RouterModule添加到导入[]
像这样:
imports: [RouterModule]
当您运行单元测试时,您应该记住angular在每个单元测试中使用单个模块。这意味着考虑您的app.component.spec:
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
您有一个模块,它只是将AppComponent作为声明,但对路由器等一无所知。它应该如下所示:
import { RouterTestingModule } from '@angular/router/testing';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [
AppComponent
],
}).compileComponents();
}));
这就是为什么您的单元测试失败,因为routerLink不是一个已知元素,但在本地主机上运行时,它不应该影响您的应用程序
编辑:
我查看了你的repo,在app.module中,你不必导入RouterModule,因为它已经通过app-routing.module导入。另外,尝试将CommonModule
添加到你的导入中,我知道它也不在angular教程中,但你需要该模块使用内置指令和管道,如*ngFor等
您在chrome中查看过调试控制台吗?可能有您要查找的错误(点击ctrl+shift+c)并切换到顶部的控制台。我通过一个简单的替换完成了此操作:
在您的bears.component.html
中,我只是将路由器链接替换为:
,它很有效欢迎来到Angular,感谢您花时间完成本教程。您可以下载本教程的源代码并比较差异。链接位于第一部分的末尾。我想您忘记了在app.module.ts中导入路由模块
,我会继续查看e源代码,但目前我无法发现差异。@Korfoo如果我错了,请纠正我,但我相信我在这里做到了:imports:[BrowserModule,FormsModule,AppRoutingModule]中的@Kaigue
您应该尝试添加RouterModule
谢谢您的建议,但我的app.component.html中有。我刚刚这么做了。我重新运行了单元测试,它仍然说“'router-outlet'不是已知元素:"你能共享你的routing.module.ts文件吗?我没有routing.module.ts文件,但我有一个app-routing.module.ts,我认为这是一样的。然后共享同一个文件app-routing.module.ts和app.module.ts。你在AppModule中添加了AppRoutingModule吗?这很令人兴奋!但它破坏了我的熊列表组件。l上没有链接它的工作是正确的。从React开始,我甚至没有想过要看控制台。我只研究了6个月的开发,但我真的应该考虑看看那里。BearsComponent{{{bear:id}}中有一个拼写错误,应该是{{bear.id}。谢谢!
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
import { RouterTestingModule } from '@angular/router/testing';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [
AppComponent
],
}).compileComponents();
}));