resources/views/backend
directory there will be a view, which is the entry point for all Angular 2 routes. For each Angular 2 route, the Laravel 5 router will redirect us to this view;@RouteConfig
we will have to create a copy in the Laravel router;/templates/SomeComponent.main
, and will be requested by Laravel 5 router along the way resources/views/frontend/SomeComponent/main.blade.php
resources/views
directory into 2 subfolders:backend
- for views that Laravel 5 uses directly;frontend
(the article will use the frontend
, but someone wants to rename, for example, templates
) - for all the templates that will be used in Angular 2.app.component.ts
root component already created, as well as 2 components: components/MainComponent.ts
, which will be loaded by default, and components/EditComponent.ts
.@RouteConfig
directive: one '/' with the name Home
, leading to the MainComponent
, and the second '/ edit' with the name Edit
, leading to the EditComponent
. //app.component.ts @RouteConfig([ { path: '/', name: 'Home, component: MainComponent, useAsDefault: true }, { path: '/edit', name: 'Edit', component: EditComponent } ]) @Component({ 'directives': [ROUTER_DIRECTIVES], 'selector': 'app', 'template': '<router-outlet></router-outlet>' }) export class AppComponent { constructor () {} }
//components/MainComponent.ts @Component({ 'selector': 'state-template', 'templateUrl': '/templates/MainComponent.main' }) export class MainComponent { ... } //components/EditComponent.ts @Component({ 'selector': 'state-template', 'templateUrl': '/templates/EditComponent.main' }) export class EditComponent { ... }
AngularRoutesController
. //routes.php Route::get('/', [ 'uses' => 'MyApp\AngularRoutesController@index', 'as' => 'ngHome' ]); Route::get('/edit', [ 'uses' => 'MyApp\AngularRoutesController@index', 'as' => 'ngEdit' ]);
AngularRoutesController
we will simply render the view that is the entry point for Angular 2: public function index() { return view('backend.content'); }
views/backend/content.blade.php
should contain a tag into which the Angular 2 application will be loaded, we have this app
. @extends('backend.layout') @section('backend.content') <app> @include('backend.loading') </app> @include('backend.scripts-import') @stop
AngularTemplatesController
controller: //routes.php Route::get('/templates/{template}', [ 'uses' => 'MyApp\AngularTemplatesController@index', 'as' => 'ngTemplates' ]);
AngularTemplatesController
we need to add to $template
, which contains part of the path to the template view, the name of our Angular 2 template folder is frontend
. public function index($template) { $templatePath = 'frontend.' . $template; if (!view()->exists($templatePath)) { throw new NotFoundHttpException(); } return view($templatePath); }
/templates/SomeComponent.main
will be taken along the path of the form resources/views/frontend/SomeComponent/main.blade.php
. We can use Blade's template engine in Angular 2 templates.{{ ... }}
, for correct processing of double curly braces related to Angular 2, it will be necessary to put the @
symbol in front of them. <div>{{ bladeVariable }}</div> <li *ngFor="#file of files" class="list-group-item"> @{{ file.name }} </li>
Source: https://habr.com/ru/post/278599/
All Articles