Skip to content

Build a basic angular 2 application with VS Code

  • Gary Woodfine  Gary Woodfine
  • angular.js, JavaScript, Software Development, Web Development

A little side project I have been working on I wanted to build a very basic Angular2 Application, it doesn't do much other than display a heading and hyperlink to the source code. What I wanted to determine is what the detailed steps required create a basic Angular2 application architecture. I wanted to do this, basically to understand what the benefits are of using one of the many starter projects available.

The aim of this article is to discuss all the core components that make up a basic Angular2 application to give you an understanding of all the plumbing involved.

This tutorial was entirely developed making use of Visual Studio code,  but you can develop this using any text editor like Sublime, Atom or even just Gedit. We are going to focus on building an application making use of the Terminal and simple text editor.

Create Application folder

Open your TextEditor or IDE of choice and create a folder for application

Then initialise the application using npm init

Shell

Then answer a few basic question about the application you intend to build.

This will create your initial package.json file for your application which we will be using and adding too as we build the application further.

Install all angular2 Dependencies

There are two methods you can add dependencies to your project and there are also two types of dependencies.

Dependencies
Are required to run the application in a production environment.

DevDependencies
Are only required by during development or compilation of the application.

To add dependencies to your application you can either edit the package.json file by adding the dependencies you require as below and the type npm install

JS

The other method is to use the terminal and type npm install [packagename] --save for a dependency or npm install [package name] --save-dev for a development dependencies.

Then you can do npm install to install them all to your project.

Install your dependencies
Shell
Install your Development dependencies
Shell

Create Index.html

We need to create the entry point for our application, which is essentially a very simple HTML page. However this file will reference a number of scripts, CSS files and also have a container for a our angular module we will be creating shortly.

HTML

Create StyleSheet

We'll go ahead and create a really simple stylesheet for our application and add some styles

Shell

Add the following styles to it

CSS

Create app folder

A convention used in most Angular2 projects to is create an app folder that will be used to contain all your angular component files. Create this folder and change into it.

Shell

Create the application module

All Angular 2 applications require at least 1 module.  This will bethe root component of what will become a tree of nested components as the application evolves. You can create the file in the terminal window by using

Shell

This will create a TypeScript file that we will the following code too.

JS

We have created a very simple Angular2 component that does not do very much at the moment other than create our AppModule.

Create main.ts

We also need to create a main.ts file that is responsible for compiling the application with the JIT compiler and bootstraps the application to run in the browser.

Shell

Add the following code to it

JS

Create an Example Component

We will now also go ahead and create another Angular2 Module that our AppModule make use of the create our really simple application.

Shell

go ahead and add the following code to the file

JS

Update the AppModule

We'll now update our AppModule with some code in order to make it display our Example.Component we created.

JS

We now almost ready to run our application.  All we need to do now is create two new files in our root directory

Shell

And Add the following code to it

JS

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project. A project is compiled in one of the following ways:

Using tsconfig.json
  • By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain.
  • By invoking tsc with no input files and a --project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file, or a path to a valid .json file containing the configurations.

Read More about  TSConfig >>

Using systemjs to compile application

We will be using to SystemJS to compile and run our application.

Read More about SystemJS >>

Shell

Add starting point for our application

We now need to go add detail to package.json file which provides npm with instructions on how to start our application. Add the following the "scripts" section of your package.json file

Shell

Run the Application

We can now run the application to see it in all its glory

Shell

Your browser should launch and you should see the new page

4617作文网朱姓猪的年起名大全十格起名周易八字取名起什么名字好听孩子免费起名取名谈谈你对红楼梦的理解周易专业起名网起门头名字大全集算死命九品一局周公解梦迷信周易测字免费取网名八字算命的准确率晞字起名寓意好移门厂起名农场的起名字周易起名怎么算合适起名字的成语导弹命中概率计算周公解梦梦见搬新家马振彪周易学说梦见自己大全周易解梦周公解梦 翡翠玉镯起名11画有什么字知命八字算命起名周公解梦梦见捉到大鱼沈阳周易培训班精灵宝可梦精灵大全破解版水果注册商标名称起名大全免费算命里缺什么周易第二十五卦潮牌起名称淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男子给前妻转账 现任妻子起诉要回网友建议重庆地铁不准乘客携带菜筐月嫂回应掌掴婴儿是在赶虫子重庆警方辟谣“男子杀人焚尸”国产伟哥去年销售近13亿新的一天从800个哈欠开始男孩疑遭霸凌 家长讨说法被踢出群高中生被打伤下体休学 邯郸通报男子持台球杆殴打2名女店员被抓19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警两大学生合买彩票中奖一人不认账德国打算提及普京时仅用姓名山西省委原副书记商黎光被逮捕武汉大学樱花即将进入盛花期今日春分张家界的山上“长”满了韩国人?特朗普谈“凯特王妃P图照”王树国3次鞠躬告别西交大师生白宫:哈马斯三号人物被杀代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了房客欠租失踪 房东直发愁倪萍分享减重40斤方法“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火手机成瘾是影响睡眠质量重要因素考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼网友洛杉矶偶遇贾玲呼北高速交通事故已致14人死亡西双版纳热带植物园回应蜉蝣大爆发男孩8年未见母亲被告知被遗忘张立群任西安交通大学校长恒大被罚41.75亿到底怎么缴沈阳一轿车冲入人行道致3死2伤奥运男篮美国塞尔维亚同组周杰伦一审败诉网易国标起草人:淀粉肠是低配版火腿肠外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万杨倩无缘巴黎奥运男子被猫抓伤后确诊“猫抓病”春分“立蛋”成功率更高?记者:伊万改变了国足氛围奥巴马现身唐宁街 黑色着装引猜测

4617作文网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化