jon*_*nes 10 bootstrapping typescript systemjs angular
我有以下文件和代码
index.html:
<!doctype html>
<html>
<head lang="en">
<title>Angular 2 Components</title>
</head>
<body>
<ngc-app></ngc-app>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2-polyfills.js"></script>
<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
System.import('lib/bootstrap.js');
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
bootstrap.js:
// Import Angular bootstrap function
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
// Import our main app component
import {App} from './app';
// We are bootstrapping Angular using our main application component
bootstrap(App);
Run Code Online (Sandbox Code Playgroud)
app.js:
// We need the Component annotation as well as the
// ViewEncapsulation enumeration
import {Component, ViewEncapsulation} from '@angular/core';
// Using the text loader we can import our template
import template from './app.html!text';
// This creates our main application component
@Component({
// Tells Angular to look for an element <ngc-app> to create this component
selector: 'ngc-app',
// Let's use the imported HTML template string
template,
// Tell Angular to ignore view encapsulation
encapsulation: ViewEncapsulation.None
})
export class App {}
Run Code Online (Sandbox Code Playgroud)
和我的app.html档案:
<div>Hello World!</div>
Run Code Online (Sandbox Code Playgroud)
而且package.json:
{
"name": "taskmanagement",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jspm": "^0.16.52"
},
"jspm": {
"dependencies": {
"@angular/common": "npm:@angular/common@^2.4.7",
"@angular/compiler": "npm:@angular/compiler@^2.4.7",
"@angular/core": "npm:@angular/core@^2.4.7",
"@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic@^2.4.7",
"rxjs": "npm:rxjs@^5.1.0",
"text": "github:systemjs/plugin-text@^0.0.9"
},
"devDependencies": {
"typescript": "npm:typescript@^2.0.7"
}
}
Run Code Online (Sandbox Code Playgroud)
}
我已经搜索过并发现每个主要组件都在内部NgModule,但根据我正在阅读的书,没有任何模块,并没有提到创建那个.那么这有什么问题,我应该创建模块文件吗?任何帮助和指导谢谢.
首先,您通过脚本标签加载的角度polyfill与通过jspm安装的版本之间存在版本不匹配.
其次,我认为你的问题的主要原因是你拥有的引导逻辑对于最近版本的角度2不再正确.
你有
// Import Angular bootstrap function
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
// Import our main app component
import {App} from './app';
// We are bootstrapping Angular using our main application component
bootstrap(App);
Run Code Online (Sandbox Code Playgroud)
你需要
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule, {
useDebug: true
});
Run Code Online (Sandbox Code Playgroud)
这意味着您需要在其中创建AppModule并导入并连接您的应用程序组件,然后将其传递给bootstrap.那将是这样的
app.module.ts
import {NgModule} from '@angular/core';
import {App} from './app';
@NgModule({
declarations: [App],
bootstrap: [App],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
当您共享完整的文件时。以下是你所犯的错误
问题在于引用的层次结构
<body>
<ngc-app></ngc-app>
<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
System.import('lib/bootstrap.js');
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2-polyfills.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
如果还需要其他事情,请告诉我。
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |