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

哆哆女性网价格seo小型龙岗网站建设深圳养生加盟水饺馄饨店起名姓邱起名字男孩名字食品店起名名字大全集供应链物流公司起名网豆豆情侣网名装修口碑中央人民政府网站昆明seo搜索优化想用王姓起个小名seo类似新网站seo外包网站优化怎样做高姓男孩起名字好北京网站页面设计培训郑州网站优化外包关于信息的诗词质押贷款张家界市建设网站卫生纸品牌起名牛奶店起名大全集中国周易起名大师排行榜虚拟币交易网站的设计石家庄网站建设报价2020姓卢男孩起名周易是智慧改变自己的读后感海宁县为什么叫柘城淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化