Динамический шаблон URL в Angular 2 - PullRequest
0 голосов
/ 08 июня 2018

У меня есть две разные боковые панели, и я хочу из переменной, которую я получаю из URL, переключаться между двумя templateURL.

@Component({
    selector: 'sidebar',
    templateUrl: './sidebar.component.html',
    styleUrls: [
        './sidebar.component.css'
    ]
})

Я сделал это, но у меня есть сообщение об ошибке:

import {
  Compiler, Component, Injector, VERSION, ViewChild, NgModule, NgModuleRef,
  ViewContainerRef, AfterViewInit, OnInit
} from '@angular/core';
@Component({
    selector: 'sidebar',
    templateUrl: '<ng-container #dynamicTemplate></ng-container>',
    styleUrls: [
        './sidebar.component.css'
    ]
})

export class SidebarComponent implements OnInit {
@ViewChild('dynamicTemplate', {read: ViewContainerRef}) dynamicTemplate;
constructor(
private _compiler: Compiler,
        private _injector: Injector,
        private _m: NgModuleRef<any>
) {}
ngAfterViewInit() {
        let myTemplateUrl = './sidebar.component.html';

        if (this.modee === 'char') {
            myTemplateUrl = './Othersidebar.component.html';
        }
        const tmpCmp = Component({
            moduleId: module.id, templateUrl: myTemplateUrl
        })(class {
        });
        const tmpModule = NgModule({declarations: [tmpCmp]})(class {
        });

        this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
            .then((factories) => {
                const f = factories.componentFactories[0];
                const cmpRef = f.create(this._injector, [], null, this._m);
                cmpRef.instance.name = 'dynamic';
                this.dynamicTemplate.insert(cmpRef.hostView);
            });
    }

и вот сообщение об ошибке:

client?afea:119 ..../app/sidebar.component.ts
Module not found: Error: Can't resolve './<ng-container #dynamicTemplate></ng-container>' in '.....\app\sidebar'
 @ ./src/main/webapp/app/layouts/sidebar/sidebar.component.ts 303:18-77
 @ ./src/main/webapp/app/layouts/index.ts
 @ ./src/main/webapp/app/app.module.ts
 @ ./src/main/webapp/app/app.main.ts
 @ multi (webpack)-dev-server/client?http://localhost:9060 webpack/hot/dev-server ./src/main/webapp/app/app.main

есть ли решение для этого?а что это за ошибка?ТНХ

Ответы [ 2 ]

0 голосов
/ 26 июня 2018

Почему бы не иметь защиту маршрутизатора с шаблоном боковой панели, а затем переключаться в зависимости от маршрута или URL.Модуль боковой панели с 2 различными боковыми панелями и 2 маршрутами для этого модуля.Шаблон1, Темпалте2.

Только мои 2 цента.

0 голосов
/ 26 июня 2018

я нахожу решение:

templateUrl: '<ng-container #dynamicTemplate></ng-container>',

изменить temlateURL на шаблон

но он не работает

...