几天后我才开始学习Angular2。我一步一步跟踪文档以及一些YouTube教程。redirectTo未定义index.js 20:angular2路由
目前我在配置路由时遇到了问题。我正在使用Visual Studio代码进行开发。以下是文件内容。
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { HeroFormComponent } from './heroComponent/hero-form.component';
import {Routes, RouterModule} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';
import {ContactusComponent} from './contactUs/contactus.component';
import {AboutusComponent} from './AboutUs/aboutus.component';
export const router:Routes [
{ path: '', redirectTo: '/', pathMatch: 'full'},
{ path:'contact', component:'ContactusComponent'},
{ path:'about', component:'AboutusComponent'}
];
@NgModule({
imports: [ BrowserModule , FormsModule,RouterModule.forRoot(router)],
declarations: [ AppComponent , HeroFormComponent, ContactusComponent,AboutusComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
在上述文件Visual Studio代码是否显示在红色[,:等,误差是"[ts] is expected"
的index.html
<html>
<head>
<base href="/">
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<base href="/">
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
这是我的一些其他疑问
(1)我看到一些Youtube和其他教程包含<script src="route.dev.js">
,但为什么角度文档没有包括它。而且在node_modules里面的文件夹结构中没有文件夹名称angular2,它是@ angular.Like这里有很多其他的库,比如RxJx,它们已经包含在教程中,但是角文件没有。我因此而感到困惑。 是否因为angular2的发布版本不同而不同。 (2)何时为routerLink包含方括号(数组),因为在文档示例中它们没有包含它。
如果您需要其他文件内容(如tsconfig.json或systemjs.config.js),请告诉我。
主要问题是我的应用程序本身没有在控制台上加载抛出错误“redirectTo is not defined index.js 20”。
我忘了包含任何文件吗?
任何帮助表示赞赏!
谢谢!
非常感谢。你可以请评论我提出的其他疑问 – shreyansh
@shreyansh更新了我的答案 – PierreDuc
非常感谢你:-) – shreyansh