gpt4 book ai didi

Angular 2.0.1 路由器 EmptyError : no elements in sequence

转载 作者:太空狗 更新时间:2023-10-29 17:30:17 26 4
gpt4 key购买 nike

我在使用 Angular 2 路由时遇到问题。我正在使用 Angular 2 和 Webpack。在 Angular 2 webpack starter 中,我注意到他们有 webpack 生成他们的 html 和他们的链接,但我希望我不必对我的 html 文件进行 webpack。

这是我的 app.routes.ts...

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from "./home/home.component";
import { ScheduleComponent } from "./schedule/schedule.component";
import { PageNotFoundComponent } from "./PageNotFoundComponent";

const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'schedule', component: ScheduleComponent },
{ path: '**', component: PageNotFoundComponent }
];

export const appRoutingProviders: any[] = [

];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

这是我的@NgModule 代码...

@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
routing ],
declarations: [
AppComponent,
AppBanner,
AppLogin,
HomeComponent,
ScheduleComponent,
PageNotFoundComponent
],
bootstrap: [ AppComponent ],
providers: [
ApplicationState,
appRoutingProviders
]
})
export class AppModule { }

这是计划组件...

import { Component } from '@angular/core';

@Component({
template: `
<div>Schedule</div>
`
})
export class ScheduleComponent {

}

这是我尝试添加路由的 HTML...

<a [routerLink]=" ['/schedule'] ">Schedule</a>

我知道这是很多代码,但这也是我的 webpack 构建。如您所见,webpack 任务不会触及我的 HTML 页面,因此此 webpack 代码可能无关紧要...

module.exports = {
entry: {
'polyfills': __dirname + '/src/polyfills.ts',
'vendor': __dirname + '/src/vendor.ts',
'app': __dirname + '/src/main.ts'
},

output: {
filename: '[name].bundle.js',
path: __dirname + '/src/bundle/',
publicPath: '/',
sourceMapFilename: __dirname + '/src/bundle/[name].bundle.map.js'
},

resolve: {
extensions: ['', '.ts', '.js'],
alias: { 'rxjs': 'rxjs-es' }
},

module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader']
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader")
},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file?name=/fonts/[name].[ext]" },
{ test: /\.(woff|woff2)$/, loader:"url?prefix=font/&limit=5000&name=/fonts/[name].[ext]" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream&name=/fonts/[name].[ext]" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml&name=/fonts/[name].[ext]" }
]
},

plugins: [
new ExtractTextPlugin('css/[name].css')
]
}

不幸的是,当我点击 Schedule 链接时,我得到了一个错误。这是来自区域的详细错误信息...

Unhandled Promise rejection: no elements in sequence ; Zone: angular ;
Task: Promise.then ; Value: Error: no elements in sequence(…)
EmptyError: no elements in sequence

有什么建议吗?这里有什么问题?

最佳答案

添加 pathMatch: 'full'

{ path: '', component: HomeComponent, pathMatch: 'full' },

否则两条路线

{ path: '', component: HomeComponent },
{ path: 'schedule', component: ScheduleComponent },

在路线 /schedule 上匹配,因为 '' 匹配,但不消耗 route 的任何内容,然后 'schedule' 匹配.

关于Angular 2.0.1 路由器 EmptyError : no elements in sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39734085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com