It is detailed instruction on how to setup Angular 2 and
Typescript in existing Sitecore project. For this purposes I’ve created a
simple Sitecore solution, you can download it from here https://github.com/kosty78/SimpleSc
and setup Angular 2 by following my instructions. The solution has TDS and Web
projects with simple layout and test control.
Also I’ve checked in solution with configured Angular 2, so
you can download it, restore all packages and run https://github.com/kosty78/SimpleScWithAngular2
My Environment:
- VS 2015 Update 3
- Sitecore 8.1
- NodeJS 6.3 https://nodejs.org/en/
First
of all, please install the ‘Package Installer’ it will help us to manage JS
packages:
·
Right
click on the Project(SimpleSc.Web) and chose ‘Quick Install package’
Enter ‘angular2’ and click ‘Install’
Use the Quick Installer again and install
- rxjs
- es6-shim
- es6-promise
- gulp
- systemjs
Make all folders visible
and you will see new folder in the project – ‘node_modules’
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "system",
"moduleResolution": "node",
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"target": "es5",
"outDir": "static/scripts/js"
},
"compileOnSave": true,
"watch": true,
"exclude": [
"node_modules",
"wwwroot"
]
}
Into boot.ts add:
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './component'
bootstrap(AppComponent);
Into Component.ts add:
///
import {Component} from 'angular2/core';
@Component({
selector: 'angular2',
template: `{{title}}
`
})
export class AppComponent {
title: string;
constructor() {
this.title = 'Angular 2';
}
}
Now we need to copy necessary js files to our folder, add Gulp configuration file
And replace the text in
the file:
var gulp = require('gulp');
gulp.task('copy:libs', function () {
return gulp.src([
'node_modules/es6-shim/es6-shim.min.js',
'node_modules/systemjs/dist/system-polyfills.js',
'node_modules/systemjs/dist/system.src.js'
]).pipe(gulp.dest('static/scripts/vendor/ang'))
});
gulp.task('copy:rx', function () {
return gulp.src([
'node_modules/rxjs/**/*.js'
]).pipe(gulp.dest('static/scripts/vendor/ang/rxjs'))
})
gulp.task('copy:angular', function () {
return gulp.src([
'node_modules/angular2/**/*.js',
]).pipe(gulp.dest('static/scripts/vendor/ang/angular2'))
});
gulp.task('copy:symbol-observable', function () {
return gulp.src([
'node_modules/symbol-observable/**/*.js',
]).pipe(gulp.dest('static/scripts/vendor/ang/symbol-observable'))
});
gulp.task("copy", ["copy:libs", 'copy:rx', 'copy:angular', 'copy:symbol-observable']);
Go to the task runner and start the copy task
Now we need to configure
system.js, create new javascript file setup.systemjs.js in static/script/js/
and add the text below into the file:
System.config({
defaultJSExtension: true,
map: {
app: 'static/scripts/js',
rxjs: 'static/scripts/vendor/ang/rxjs',
angular2: 'static/scripts/vendor/ang/angular2',
'symbol-observable': 'static/scripts/vendor/ang/symbol-observable'
},
packages: {
app: {
defaultExtension: 'js',
format: 'register'
},
rxjs: {
defaultExtension: 'js',
format: 'cjs'
}
,
angular2: {
defaultExtension: 'js',
format: 'cjs'
},
'symbol-observable':
{
defaultExtension: 'js',
main: 'index.js'
}
}
});
System.import('app/boot').then(null, console.error.bind(console));
Now we need to add the references in the Layout
file : SimpleLayout.cshtml
<script src="static/scripts/vendor/ang/es6-shim.min.js" type="text"></script>
<script src="static/scripts/vendor/ang/system-polyfills.js"></script>
<script src="static/scripts/vendor/ang/angular2/bundles/angular2-polyfills.js"></script>
<script src="static/scripts/vendor/ang/system.src.js"></script>
<script src="static/scripts/vendor/ang/rxjs/Rx.js"></script>
<script src="static/scripts/vendor/ang/angular2/bundles/angular2.dev.js"></script>
<script src="static/scripts/js/setup.systemjs.js"></script>
Add post build event to
copy necessary angular2 files into sitecore web directory
xcopy
"$(ProjectDir)Static\scripts\vendor\ang"
"c:\Sitecore\Sites\sc8\Website\Static\scripts\vendor\ang" /i /e /y
Now rebuild and run the
Sitecore and preview the Test page
Wow, it Works!!! Now you can research Angular 2 features! )
This information is really awesome thanks for sharing most valuable information.
ReplyDeleteSitecore Training
Sitecore Online Training
Sitecore Training in Hyderabad