gpt4 book ai didi

javascript - Angular 2 : importing router bundle

转载 作者:行者123 更新时间:2023-12-02 15:08:54 25 4
gpt4 key购买 nike

我很困惑为什么我无法将路由器作为依赖项导入。在控制台中我收到错误:

system.js:4 GET http://127.0.0.1:8000/angular2/src/platform/dom/dom_adapter.js 404 (Not Found)

看看谷歌的例子,他们使用几乎相同的设置,所以我不太确定我哪里出了问题。如果我注释掉路由器的导入,一切都会按预期工作。

index.html:

<body>
<main>Loading...</main>

<script src="lib/traceur-runtime.js"></script>
<script src="lib/system.js"></script>
<script src="lib/Reflect.js"></script>

<script>
System.config({defaultJSExtensions: true});
</script>

<script src="lib/angular2.js"></script>
<script src="lib/router.js"></script>

<script>
System.import('index').catch(console.log.bind(console));
</script>
</body>

index.js:

import {Component, View, bootstrap} from 'angular2/angular2';
import {routerInjectables} from 'angular2/router';
import {stepOne} from 'step-one/step-one';

@Component({
selector: 'main'
})

/*@RouteConfig([
{path: '/', name: 'StepOne', component: stepOne, useAsDefault: true}
])*/

@View({
template: `
<h1>INIT</h1>
<router-outlet></router-outlet>
`
})

class Main {
constructor(){}
}

bootstrap(Main, [routerInjectables]);

最佳答案

看来您的代码中有很多错误,但这里很少......

使用下面给出的内容更正您的导入,因为 angular2 现在处于测试阶段......

import {bootstrap} from 'angular2/platform/browser';
import {Component, View} from 'angular2/core';

您还没有提供路由器指令,请从此处导入该指令并将其添加到指令列表中。

import {ROUTER_DIRECTIVES, RouteParams, ROUTER_PROVIDERS} from 'angular2/router';

在index.ts中使用它的更好方法

    @Component({
selector: 'main',
template: `
<h1>INIT</h1>
<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES],
})
@RouteConfig([
{path: '/', component: stepOne, name: 'StepOne', useAsDefault: true}
])

class Main {
constructor(){}
}

bootstrap(Main, [ROUTER_PROVIDERS]);

你的index.html应该是这样的。

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script>
System.config({
defaultJSExtensions: true,
map: {
rxjs: 'node_modules/rxjs'
},
packages: {
rxjs: {
defaultExtension: 'js'
}
}
});

</script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>

<script>
System.import('Main');

</script>

关于javascript - Angular 2 : importing router bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918604/

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