• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解webpack+angular2开发环境搭建

angularjs 搞代码 4年前 (2021-12-31) 25次浏览 已收录 0个评论

这篇文章主要介绍了详解webpack+angular2开发环境搭建,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

刚搭建完一个webpack+angular2环境,由于angular及webpack官网上没有一个折中的搭建方案,所以只能摸索着搭建,中间遇到一些坑,遂总结记录下来,以供交流。

搭建完后的项目初步环境如下:

 app ----app.component.ts ----app.module.ts ----main.ts index.html package.json tsconfig.json webpack.config.js

app.componnet.ts:组件文件。angular2应用是由组件构成,组件控制视图;

 import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <h1>{{title}}</h1><h2>My favorite hero is: {{myHero}}</h2> ` }) // 使用变量初始化方式 export class AppComponent { title = 'Tour of Heroes'; myHero = 'Windstorm'; }

app.module.ts:应用跟模块。angular是模块化,拥有自己的模块系统,被称为angular模块或NgModules(深入了解);//缺少下述模块引入,会输出”Uncaught reflect-m来源gao*daima.com搞@代#码网etadata shim is required when using class decorators”的错误

 import 'core-js/es6'; import 'core-js/es7/reflect'; import 'zone.js/dist/zone'; //引入NgModule装饰器 import { NgModule }   from '@angular/core'; //引入浏览器模块 import { BrowserModule } from '@angular/platform-browser'; //引入创建的component import { AppComponent } from './app.component'; @NgModule({ imports:   [ BrowserModule ], declarations: [ AppComponent ], bootstrap:  [ AppComponent ] }) export class AppModule { } 

 main.ts:用于引导跟模块启动应用;

 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule }       from './app.module'; //引导跟模块启动应用 platformBrowserDynamic().bootstrapModule(AppModule); index.html:angular应用宿主页面;   <title>small胖的博客</title> 

package.json:一个标准化的npm说明文件,其中包含诸如当前应用的依赖包、自定义的脚本命令等,在cmd终端可用npm init自动创建该文件;

注意,此处如果引入的angular模块版本是2.4.X,则会报错“Angular2 + Jspm.io : reflect-metadata shim is required when using class decorators”,产生此坑的具体原因尚不清楚,希望有朋友一起交流。

 { "name": "blogcode", "version": "1.0.0", "description": "", "main": "webpack.config.js", "dependencies": { "ts-loader": "2.0.0", "@angular/common": "2.1.2", "@angular/compiler": "2.1.2", "@angular/core": "2.1.2", "@angular/platform-browser": "2.1.2", "@angular/platform-browser-dynamic":"2.1.2", "rxjs": "5.0.0-beta.12", "zone.js": "0.6.26", "core-js": "^2.4.1" }, "devDependencies": { "webpack": "^2.2.1", "@types/core-js": "^0.9.35", "typescript": "^2.1.5", "webpack": "^2.2.0", "webpack-dev-server": "^2.3.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://git.coding.net/frankshin/xudengwei.git" }, "author": "", "license": "ISC" } 

tsconfig.json:用于定义typescript编译成ES5的各项参数;

 { "compilerOptions": { "module": "commonjs", "target": "es5", "moduleResolution": "node", "noImplicitAny": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "declaration": false }, "buildOnSave": false, "compileOnSave": false, "exclude": [ "node_modules" ] } 

webpack.config.js:一个标准化的commonjs文件,用于配置webpack编译打包的参数。

 module.exports = { entry: "./app/main.ts", output: { path: __dirname + '/dist', filename: "bundle.js" }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, }, ] }, resolve: { extensions: [".tsx", ".ts", ".js"] } }; 

以上就是详解webpack+angular2开发环境搭建的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解webpack+angular2开发环境搭建

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址