详解webpack+angular2开发环境搭建

 更新时间:2017年06月28日 11:21:05   作者:追逐前沿的攻城狮  
这篇文章主要介绍了详解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-metadata 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应用宿主页面;
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width" />
  <title>small胖的博客</title>
</head>
<body>
  <my-app></my-app>
  <script src="dist/bundle.js"></script>
</body>
</html>

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"]
  }
};

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解AngularJS之$window窗口对象

    详解AngularJS之$window窗口对象

    本篇文章主要介绍了AngularJS之$window窗口对象,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Angular.JS中指令ng-if的注意事项小结

    Angular.JS中指令ng-if的注意事项小结

    这篇文章主要给大家分享了关于Angular.JS中指令ng-if的一点注意事项,分享出来供大家参考学习,文中介绍的还是相对来说比较详细,对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。
    2017-06-06
  • angular动态删除ng-repaeat添加的dom节点的方法

    angular动态删除ng-repaeat添加的dom节点的方法

    本篇文章主要介绍了angular动态删除ng-repaeat添加的dom节点的方法,非常具有实用价值,需要的朋友可以参考下
    2017-07-07
  • 详解Angular.js的$q.defer()服务异步处理

    详解Angular.js的$q.defer()服务异步处理

    相信大家都知道jquery和angular都有defer服务,这篇文章暂以angular为例谈谈个人的理解,在文章的最后并附上jquery的阮一峰总结的defer。有需要的朋友们也可以参考借鉴,下面来一起学习学习吧。
    2016-11-11
  • AngularJS中的指令全面解析(必看)

    AngularJS中的指令全面解析(必看)

    下面小编就为大家带来一篇AngularJS中的指令全面解析(必看)。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • Angular 与 Component store实践示例

    Angular 与 Component store实践示例

    这篇文章主要为大家介绍了Angular 与 Component store实践示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 利用Angularjs和原生JS分别实现动态效果的输入框

    利用Angularjs和原生JS分别实现动态效果的输入框

    现在的很多网站都将输入框做成了动态的效果,这样对于用户体检来说非常好,这篇文章分别用Angularjs和原生JS两种方法来实现动态效果的输入框,具有一定的参考价值,有需要的小伙伴们可以来参考借鉴。
    2016-09-09
  • AngularJS Module方法详解

    AngularJS Module方法详解

    AngularJS中的Module类负责定义应用如何启动,它还可以通过声明的方式定义应用中的各个片段。我们来看看它是如何实现这些功能的
    2015-12-12
  • 详细谈谈AngularJS的子级作用域问题

    详细谈谈AngularJS的子级作用域问题

    大家在使用angularjs的时候,很容易忽略AngularJS自带指令的作用域问题,有一些指令会产生独立的自己作用域,造成子级无法与父级作用域双向绑定的问题。下面我们来看看这些问题,有需要的可以参考借鉴。
    2016-09-09
  • angular4 如何在全局设置路由跳转动画的方法

    angular4 如何在全局设置路由跳转动画的方法

    本篇文章主要介绍了angular4 如何在全局设置路由跳转动画的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论