Angular 2-基于文件的应用程序的路由问题

麦克雷德

我一直在根据多个主题和教程修改这个主题,但似乎都没有一个解决这个特定问题的方法。我有一个小的应用程序,它是从文件系统而不是通过HTTP加载的。无论我如何调整设置,index.html#/character还是index.html#character将始终路由到AppComponent而不是CharacterComponent如果我将引导程序app.module.ts更改AppComponentCharacterComponent,它将显示字符。我有一种感觉,我只是缺少一个可以正确在组件之间进行路由的路由配置。

版本号

  "devDependencies": {
    "awesome-typescript-loader": "^3.0.0-beta.3",
    "babel-core": "^6.18.2",
    "concurrently": "^3.1.0",
    "es6-shim": "^0.35.1",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.10",
    "webpack": "^2.1.0-beta.25"
  },
  "dependencies": {
    "@angular/common": "2.2.1",
    "@angular/compiler": "2.2.1",
    "@angular/core": "2.2.1",
    "@angular/http": "2.2.1",
    "@angular/platform-browser": "2.2.1",
    "@angular/platform-browser-dynamic": "2.2.1",
    "@angular/router": "3.2.1",
    "angular-in-memory-web-api": "~0.1.15",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-rc.4",
    "zone.js": "^0.6.26",
    "ui-router-ng2": "^1.0.0-beta.3"
  }

Webpack

 entry: {
    'angular2': [
      'zone.js',
      'rxjs',
      'reflect-metadata',
      '@angular/core'
    ],
    'app': './app/app'
  },

index.html

<html>
  <head>
    <base href="">
    <meta charset="UTF-8">
    <title>test app</title>
    <link rel="stylesheet" type="text/css" href="css/app.css">

  </head>
  <body>
    <div class="container">
      <app></app>
    </div>
    <script src="../build/common.js"></script>
    <script src="../build/angular2.js"></script>
    <script src="../build/app.js"></script>
  </body>
</html>

app.component.ts

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

@Component({
  selector: 'app',
  templateUrl: 'templates/selectFile.html'
})

export class AppComponent {
}

templates / selectFiles.html

<div id="selectFile">
  <a href="" id="fileselect"></a>
</div>
<router-outlet></router-outlet>

character.component.ts

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

@Component({
  selector: 'app',
  templateUrl: 'templates/character.html'
})

export class CharacterComponent {
}

templates / character.html

<div id="selectFile">
  character <br />
</div>
<router-outlet></router-outlet>

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';

import { AppComponent }   from './app.component';
import { CharacterComponent }   from './character.component';

const routes: Routes = [
  { path: 'character', component: CharacterComponent },
  { path: '', component: AppComponent }
]

@NgModule({
  imports:      [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  declarations: [
    AppComponent,
    CharacterComponent
  ],
  providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
  bootstrap: [ AppComponent ]
})

export class AppModule { }
凯文·基普(Kevin Kipp)

引导您的应用程序组件,该组件将始终在页面上。路由器将把适当的组件放入<router-outlet></router-outlet>标签中(实际上是在同一级别,但这并不重要)。

问题是,您想让空路''走到哪里?您可能HomePageComponent加载了一个,或者您可能想CharacterComponent使用该redirectTo选项重定向到您

{
  path: '',
  redirectTo: '/character',
  pathMatch: 'full'
}

在这里阅读更多

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 1和Angular 2混合应用程序路由问题(不显示Angular组件)

Angular 2应用程序和Angular 2路由器的异步性问题

S3上部署的Angular 2+应用程序出现路由问题

在Angular2中禁用应用程序加载/路由

Angular 2路由应用程序路由模块声明错误

在angular 2应用程序中读取JSON文件

模块中子应用程序之间的Angular2路由

使用带有路由的ngUpgrade引导Angular 2 rc.6混合应用程序

ASP.Net Core + Angular 2应用程序/家庭路由

angular2 应用程序中所有路由的默认方法是什么?

具有 angular (2/4) 的单页应用程序中的命名路由器

如何使用核心angular2路由器停止在angular 2应用程序中添加URL

如何基于TypeScript将Electron ipcRenderer集成到Angular 2应用程序中?

是否可以在Angular2中创建基于客户端工作区的应用程序?

Dockerizing Angular 2应用程序失败

调试Angular2应用程序

Angular 2应用程序的范围

使用Angular 2的Cordova应用程序

Ping Angular 2 应用程序

如何运行angular 2应用程序?

从Angular 2向Spring支持的Java应用程序发送POST请求时出现CSRF问题

Angular 2 AoT编译会导致应用程序问题并在JiT中正常运行

github页面上Angular2应用程序中的路径问题

Angular 应用程序的 WSO2 Api Manger 3.0.0 Token API CORS 问题

自动构建 angular cli 应用程序缓存文件问题

如何以 angular 2+ 从一个应用程序路由到另一个应用程序

以 `file://` 形式运行基于 angular 7 路由的应用程序(无服务器)

如何在 angular2 应用程序中使用第二个路由器插座?

如何在angular2路由中正确设置应用程序上下文路径?