当前位置: 首页 > 知识库问答 >
问题:

Angular2未加载路由文件

祝高阳
2023-03-14

我正在尝试将我的angular2应用程序从已弃用的路由器切换到推荐的路由器[https://angular.io/docs/ts/latest/guide/router.html][1]。我正在使用angular2 cli帮助构建我的项目。我已经在没有cli的种子项目中成功地完成了这一点,但是在使用cli执行此操作时,我一直收到此错误:

加载错误http://localhost:4200/app.routes作为来自的“/app.routes”http://localhost:4200/main.js“区域:;任务:Promise.then;值:Error:patchProperty/desc.set/wrapFn@http://localhost:4200/vendor/zone.js/dist/zone。js:769:27 Zonehttp://localhost:4200/vendor/zone.js/dist/zone.js:356:24Zonehttp://localhost:4200/vendor/zone.js/dist/zone.js:256:29佐涅塔斯克/this.invoke@http://localhost:4200/vendor/zone.js/dist/zone。js:423:29加载错误http://localhost:4200/app.routes作为来自的“/app.routes”http://localhost:4200/main.js

这是我的main.ts文件。

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';
import { APP_ROUTER_PROVIDERS } from './app.routes';

if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent, [APP_ROUTER_PROVIDERS])
  .catch(err => console.error(err));

这是我app.routes.ts档案

import { provideRouter, RouterConfig } from '@angular/router';
import {Main} from "./app/splash_app/main.component";
import {Whatsup} from "./app/splash_app/whatsup.component";
import {LocalBus} from "./app/splash_app/localbus.component";
import {Login} from "./app/splash_app/login.component";

export const routes: RouterConfig = [
  { path: 'main', component: Main },
  { path: 'search', component: Whatsup },
  { path: 'local-business', component: LocalBus },
  {path: 'guest-login', component: Login}
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

这是系统配置文件

// SystemJS configuration file, see links for more information
// https://github.com/systemjs/systemjs
// https://github.com/systemjs/systemjs/blob/master/docs/config-api.md

/***********************************************************************************************
 * User Configuration.
 **********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
};

/** User packages configuration. */
const packages: any = {

};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',


  // Thirdparty barrels.
  'rxjs',

  // App specific barrels.
  'app',
  'app/shared',
  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
  cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js'
  },
  packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

main.ts文件和app.routes.ts文件都在src文件夹中。

共有1个答案

乜建柏
2023-03-14

app.routes.ts文件移动到您的app目录中,然后将main.ts中的导入更改为指向新路径......

import { APP_ROUTER_PROVIDERS } from './app/app.routes';
 类似资料:
  • 当打包构建应用时,Javascript 包会变得非常大,影响页面加载速度。如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了。 结合 Vue 的异步组件和 Webpack 的代码分割功能,轻松实现路由组件的懒加载。如: const Foo = () => import('./Foo.vue') 当你觉得你的页面热更新速度慢的时候,才需要往下看

  • 所以我有一些路线,比如<code>/category/tech</code>和<code</categority/tech/new<code>和<code>/category/tech/old</code>等等 他们都使用 或者我必须分别定义它们,就像

  • Angular2有一个routerLinkActive指令,如果路由是活动的,它会向元素添加类。我想在路由不活动时添加一个类。有办法做到这一点吗?

  • 不确定我是否做对了一切。但问题是:当我从延迟加载的模块导航到组件的某些子路由时,它根本不加载。它从延迟加载模块重新加载主组件。 app-routing.component.ts planet-detector-routeting.module.ts 所以在上面的例子中,当你输入“http://localhost:4200/planet-detector/first”时,它会加载DetectorCo

  • 我有AngularJs ui路由器在我的应用程序。我需要加载js文件基于我的状态。 当主控状态加载时,headerController、homeController和footerController具有不同的js文件。我们需要加载控制器js文件。是否可以通过UI路由器?

  • 我有一个带有路由器插座的根应用程序组件,并且路由是从家庭模块路由加载的,该路由在其子路由中使用延迟加载和 loadchildren。家庭组件中有一个路由器插座,在家庭的所有子模块中也有延迟加载的路由器插座。路由工作正常,但子路由也加载到根路由器出口中。例如:- 组件“testCreateComponent”正在加载localhost:4200 / test/create和localhost:420