相对路径在 angular 中不起作用
Relative paths not work in angular
当我在“http://localhost/overview”时,模块是沿着这个路径加载的:
http://localhost/app/modules/overview/overview.module.js
但是当我转到 http://localhost/overview/users
页面然后按 F5(刷新页面)时,出现错误:
Error: Unexpected token <
Evaluating http://localhost/overview/app/modules/overview/overview.module
错误发生是因为URL不正确,他应该是http://localhost/app/modules/overview/overview.module
.
如何让它正常工作?
这是项目结构:
这是 systemjs tsconfig:
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true
}
}
这是 systemjs 配置:
(function (global) {
System.config({
baseURL: "/",
paths: {
'npm:': '/node_modules/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/material': 'npm:@angular/material/bundles/material.umd.js',
// CDK individual packages
'@angular/cdk/platform': 'npm:@angular/cdk/bundles/cdk-platform.umd.js',
'@angular/cdk/a11y': 'npm:@angular/cdk/bundles/cdk-a11y.umd.js',
'@angular/cdk/bidi': 'npm:@angular/cdk/bundles/cdk-bidi.umd.js',
'@angular/cdk/observers': 'npm:@angular/cdk/bundles/cdk-observers.umd.js',
'@angular/cdk/overlay': 'npm:@angular/cdk/bundles/cdk-overlay.umd.js',
'@angular/cdk/portal': 'npm:@angular/cdk/bundles/cdk-portal.umd.js',
'@angular/cdk/scrolling': 'npm:@angular/cdk/bundles/cdk-scrolling.umd.js',
'@angular/cdk/keycodes': 'npm:@angular/cdk/bundles/cdk-keycodes.umd.js',
'@angular/cdk/coercion': 'npm:@angular/cdk/bundles/cdk-coercion.umd.js',
'@angular/cdk/collections': 'npm:@angular/cdk/bundles/cdk-collections.umd.js',
'@angular/cdk/rxjs': 'npm:@angular/cdk/bundles/cdk-rxjs.umd.js',
'@angular/cdk/table': 'npm:@angular/cdk/bundles/cdk-table.umd.js',
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
'rxjs': 'npm:rxjs'
//Custom
},
packages: {
app: {
main: './bootstrap.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
更新
应用-routing.module:
import {RouterModule, Routes} from "@angular/router";
import {NgModule} from "@angular/core";
import {LoginFormComponent} from "./modules/auth/login-form/login-form.component";
const routes :Routes = [
{path: "auth", component: LoginFormComponent},
{path: "overview", loadChildren: "./app/modules/overview/overview.module#OverviewModule"}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule{
}
概述。routing.module:
import {RouterModule, Routes} from "@angular/router";
import {NgModule} from "@angular/core";
import {OverviewComponent} from "./overview/overview.component";
const routes :Routes = [
{path: "", component: OverviewComponent, children:[
{path: "users", loadChildren: "/app/modules/users/users.module#UsersModule"}
]}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class OverviewRoutingModule{
}
用户-routing.module:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {UsersListComponent} from "./users-list/users-list.component";
import {UserElementComponent} from "./user-element/user-element.component";
const routes: Routes = [
{path: "", component: UsersListComponent},
{path: ":id", component: UserElementComponent},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UsersRoutingModule { }
在我使用的组件 module.id
中,查找示例 overview.component.ts:
@Component({
selector: "overview",
moduleId: module.id,
templateUrl: "./overview.component.html",
providers:[
OverviewService
]
})
export class OverviewComponent implements OnInit {
constructor(private overviewService :OverviewService) {}
public ngOnInit() {
let data = this.overviewService.getOverview();
Log.info(data);
}
}
您在用户模块的路径中有前导 /。
改成这样:
{path: "users", loadChildren: "app/modules/users/users.module#UsersModule"}
在您的模块路由中,它不会从您的 AppRouting 延迟加载中获取 "overview"。你会认为它应该,但事实并非如此。
将您的路线更改为:
const routes :Routes = [
{path: "overview", component: OverviewComponent, pathMatch: 'full' },
{path: "overview/users", loadChildren: "app/modules/users/users.module#UsersModule"}
]}
];
和
const routes: Routes = [
{path: "overview/users", component: UsersListComponent},
{path: "overview/users/:id", component: UserElementComponent},
];
编辑:
https://plnkr.co/edit/pxQLNiFqltjxFtynGEgr?p=preview
做了一些改动。将所有延迟加载放入 AppRoutingModule 中,并添加了一个包装器 class,因此 Overview 部分可以有自己的 router-outlet。您可以看到在它们之间单击如何与概览和概览 2
一起使用
当我在“http://localhost/overview”时,模块是沿着这个路径加载的:
http://localhost/app/modules/overview/overview.module.js
但是当我转到 http://localhost/overview/users
页面然后按 F5(刷新页面)时,出现错误:
Error: Unexpected token <
Evaluating http://localhost/overview/app/modules/overview/overview.module
错误发生是因为URL不正确,他应该是http://localhost/app/modules/overview/overview.module
.
如何让它正常工作?
这是项目结构:
这是 systemjs tsconfig:
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true
}
}
这是 systemjs 配置:
(function (global) {
System.config({
baseURL: "/",
paths: {
'npm:': '/node_modules/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/material': 'npm:@angular/material/bundles/material.umd.js',
// CDK individual packages
'@angular/cdk/platform': 'npm:@angular/cdk/bundles/cdk-platform.umd.js',
'@angular/cdk/a11y': 'npm:@angular/cdk/bundles/cdk-a11y.umd.js',
'@angular/cdk/bidi': 'npm:@angular/cdk/bundles/cdk-bidi.umd.js',
'@angular/cdk/observers': 'npm:@angular/cdk/bundles/cdk-observers.umd.js',
'@angular/cdk/overlay': 'npm:@angular/cdk/bundles/cdk-overlay.umd.js',
'@angular/cdk/portal': 'npm:@angular/cdk/bundles/cdk-portal.umd.js',
'@angular/cdk/scrolling': 'npm:@angular/cdk/bundles/cdk-scrolling.umd.js',
'@angular/cdk/keycodes': 'npm:@angular/cdk/bundles/cdk-keycodes.umd.js',
'@angular/cdk/coercion': 'npm:@angular/cdk/bundles/cdk-coercion.umd.js',
'@angular/cdk/collections': 'npm:@angular/cdk/bundles/cdk-collections.umd.js',
'@angular/cdk/rxjs': 'npm:@angular/cdk/bundles/cdk-rxjs.umd.js',
'@angular/cdk/table': 'npm:@angular/cdk/bundles/cdk-table.umd.js',
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
'rxjs': 'npm:rxjs'
//Custom
},
packages: {
app: {
main: './bootstrap.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
更新
应用-routing.module:
import {RouterModule, Routes} from "@angular/router";
import {NgModule} from "@angular/core";
import {LoginFormComponent} from "./modules/auth/login-form/login-form.component";
const routes :Routes = [
{path: "auth", component: LoginFormComponent},
{path: "overview", loadChildren: "./app/modules/overview/overview.module#OverviewModule"}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule{
}
概述。routing.module:
import {RouterModule, Routes} from "@angular/router";
import {NgModule} from "@angular/core";
import {OverviewComponent} from "./overview/overview.component";
const routes :Routes = [
{path: "", component: OverviewComponent, children:[
{path: "users", loadChildren: "/app/modules/users/users.module#UsersModule"}
]}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class OverviewRoutingModule{
}
用户-routing.module:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {UsersListComponent} from "./users-list/users-list.component";
import {UserElementComponent} from "./user-element/user-element.component";
const routes: Routes = [
{path: "", component: UsersListComponent},
{path: ":id", component: UserElementComponent},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class UsersRoutingModule { }
在我使用的组件 module.id
中,查找示例 overview.component.ts:
@Component({
selector: "overview",
moduleId: module.id,
templateUrl: "./overview.component.html",
providers:[
OverviewService
]
})
export class OverviewComponent implements OnInit {
constructor(private overviewService :OverviewService) {}
public ngOnInit() {
let data = this.overviewService.getOverview();
Log.info(data);
}
}
您在用户模块的路径中有前导 /。
改成这样:
{path: "users", loadChildren: "app/modules/users/users.module#UsersModule"}
在您的模块路由中,它不会从您的 AppRouting 延迟加载中获取 "overview"。你会认为它应该,但事实并非如此。
将您的路线更改为:
const routes :Routes = [
{path: "overview", component: OverviewComponent, pathMatch: 'full' },
{path: "overview/users", loadChildren: "app/modules/users/users.module#UsersModule"}
]}
];
和
const routes: Routes = [
{path: "overview/users", component: UsersListComponent},
{path: "overview/users/:id", component: UserElementComponent},
];
编辑: https://plnkr.co/edit/pxQLNiFqltjxFtynGEgr?p=preview
做了一些改动。将所有延迟加载放入 AppRoutingModule 中,并添加了一个包装器 class,因此 Overview 部分可以有自己的 router-outlet。您可以看到在它们之间单击如何与概览和概览 2
一起使用