【问题标题】:RouteConfig does not seem to work in my angular 2 projectRouteConfig 在我的 Angular 2 项目中似乎不起作用
【发布时间】:2016-05-11 03:40:42
【问题描述】:

我正在尝试在我的 Angular2 应用程序中使用 RouteConfig。现在,我似乎无法找出我做错了什么。

我的 App 组件正在使用 router-outlet 东西,但它不起作用。

我创建了一个 main.ts 文件来引导我的应用程序:

Main.ts

import {provide, enableProdMode, Injectable, Component} from 'angular2/core';
import {bootstrap, ELEMENT_PROBE_PROVIDERS} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig, LocationStrategy, HashLocationStrategy} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';

const ENV_PROVIDERS = [];

if ('production' === process.env.ENV) {
  enableProdMode();
} else {
  ENV_PROVIDERS.push(ELEMENT_PROBE_PROVIDERS);
}

import {App} from './app/app';

/*
 * Bootstrap our Angular app with a top level component `App` and inject
 * our Services and Providers into Angular's dependency injection
 */
document.addEventListener('DOMContentLoaded', function main() {
  bootstrap(App, [ENV_PROVIDERS, HTTP_PROVIDERS, ROUTER_PROVIDERS,  RouteConfig, provide(LocationStrategy, { useClass: HashLocationStrategy }) ])
  .catch(err => console.error(err));

});

应用组件

// Bootstrapping imports
import { bootstrap } from 'angular2/platform/browser';
import { HTTP_PROVIDERS } from 'angular2/http';
import { ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig } from  'angular2/router';
import { Injectable, provide, Component, ViewEncapsulation } from 'angular2/core';

// Import components
import {AmendmentComponent} from '../Components/amendmentComponent';

@Component({
    selector: 'app',
    template: `
<div>
    <div class="header wrapper">
        <header class="row">
            <div class="column small-12">
                <a href="/">
                    <img src="/assets/img/logo.svg" title="" />
                </a>
                <a class="navigation-toggle icon-menu hide-for-large-up" (click)="toggleMenuState()" href="javascript:void(0);"></a>
            </div>
        </header>
    </div>

    <router-outlet></router-outlet>

    <div class="footer wrapper">
        <footer class="row">
            <div class="column small-12">
                <div class="container">
                    <div class="icon icon-ship hide-for-medium-down"></div>
                </div>
            </div>
        </footer>
    </div>
</div>
`,
encapsulation: ViewEncapsulation.None,
directives: [ ROUTER_DIRECTIVES ],
styles: [`
    app {
        display: block;
    }
`]
})

@RouteConfig([
    { path: '/', redirectTo: ["Amendment"] },
    { path: '/amendment/...', name: 'Amendment', component: AmendmentComponent, useAsDefault: true },
])

export class App {
    angularclassLogo = 'assets/img/favicon.ico';
    name = 'AmendmentFront';
    url = '';

    constructor() {

    }
}

当用户输入地址:localhost:3000 或 localhost:3000/amendment 时,它应该加载 AmendmentComponent 组件,但没有任何反应,我可以看到页眉和页脚,但中间没有路由器-出口。

我做错了什么?

在与用户讨论后,我收到以下错误:

Router 实例化过程中出错! (RouterOutlet -> 路由器)。 “/amendment”不允许使用子路由。在父路由路径上使用“...”。

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    Angular 在最后一个 alpha 或第一个 beta 版本中对无效 HTML 变得更加严格。 &lt;img ... /&gt; 在 HTML5 中无效,即使浏览器尝试按照开发人员的意图解释它。它可能会导致意想不到的结果,因此 Angular 不再试图过于宽容。

    还将useAsDefault: true 添加到一条路线。

    【讨论】:

    • 已尝试从侧面删除完整的图像,不要改变任何东西.. 路由器插座似乎仍然没有加载其他组件。
    • 只允许一条路线。如果我删除 routeconfig 中的第二行,它不会给我一个错误,但如果用户输入 localhost:3000/amendment 我仍然需要加载它?
    • 我完全不确定.. 我删除了一些代码并替换了它,这就成功了。这是我现在使用的代码 @RouteConfig([ { path: '/', redirectTo: ["Amendment"] }, { path: '/amendment/...', name: 'Amendment', component: AmendmentComponent, useAsDefault : true } ]) 和以前一样。所以我不明白:S
    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2016-09-23
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多