gpt4 book ai didi

javascript - 带有 babel ES2015 的 Angular 2 未加载且没有错误

转载 作者:行者123 更新时间:2023-11-30 16:00:23 26 4
gpt4 key购买 nike

我正在尝试将 angular 2 与 babel、grunt、browserify 和 ES2015 源一起使用。我正在尝试一个非常基本的示例,即简单地将组件及其模板加载到我的文档中。

构建运行良好,创建包时没有错误,但加载时没有任何反应,我在控制台中也没有错误,因此很难调试。

我花了很多时间试图弄清楚,并根据我可以在网上找到的示例,我的配置似乎没问题,但如果它不能正常工作,那我就出错了。我根本看不到它。

Package.json

{
"name": "angular2-es2015-babel",
"version": "0.0.1",
"description": "angular2-es2015-babel",
"devDependencies": {

"http-server": "^0.9.0",
"shelljs": "^0.6.0",

"babel-preset-es2015": "6.9.0",
"babel-preset-angular2": "0.0.2",
"babel-plugin-transform-es2015-modules-umd": "6.8.0",

"grunt": "~1.0.1",
"grunt-babel": "6.0.0",
"grunt-browserify": "4.0.1"
},
"dependencies": {

"@angular/common": "2.0.0-rc.2",
"@angular/compiler": "2.0.0-rc.2",
"@angular/core": "2.0.0-rc.2",
"@angular/http": "2.0.0-rc.2",
"@angular/platform-browser": "2.0.0-rc.2",
"@angular/platform-browser-dynamic": "2.0.0-rc.2",
"@angular/router": "2.0.0-rc.2",
"@angular/router-deprecated": "2.0.0-rc.2",
"@angular/upgrade": "2.0.0-rc.2",

"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12"
},
"scripts": {

"prestart": "npm install && grunt",
"start": "http-server -a localhost -p 8000 -c-1 ./",
"preupdate-webdriver": "npm install",
"update-webdriver": "webdriver-manager update",
"preprotractor": "npm run update-webdriver",
"protractor": "protractor test/protractor.conf.js"
}
}

Gruntfile.js

module.exports = function(grunt) {

var tasks = ["babel", "browserify"];

grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
babel: {
options: {
presets: ["es2015", "angular2"],
plugins: ["babel-plugin-transform-es2015-modules-umd"]
},
demo: {
files: {
"app.babel.js": "app.js"
}
}
},
browserify: {
demo: {
files: {
"bundle.js": [
"node_modules/core-js/client/shim.js",
"node_modules/zone.js/dist/zone.js",
"node_modules/reflect-metadata/Reflect.js",
"node_modules/@angular/*/bundles/*.umd.js",
"app.babel.js"
]
}
}
}
});

grunt.loadNpmTasks("grunt-browserify");
grunt.loadNpmTasks("grunt-babel");

grunt.registerTask("default", tasks);
};

app.js

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

@Component({
selector: "my-app",
template: "<p>It works!</p>"
})

export class AppComponent {

}

index.html

<!DOCTYPE html>
<html>
<body>
<my-app>Loading...</my-app>
<script src="bundle.js"></script>
</body>
</html>

运行这个简单的演示我缺少什么?

最佳答案

要使 Angular 2 应用程序正常工作,您需要引导 Angular 应用程序组件。

代码

import { Component } from "@angular/core";
import { bootstrap } from '@angular/platform-browser-dynamic'; //added import

@Component({
selector: "my-app",
template: "<p>It works!</p>"
})

export class AppComponent {

}

bootstrap(AppComponent); //bootstrap application here

关于javascript - 带有 babel ES2015 的 Angular 2 未加载且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37865122/

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