如何在 Angular 5 中动态读取外部模板
How to read dynamically external templates in Angular 5
向社区问好
通常在Angular 5,当我使用外部模板时,@Component 有以下格式,它工作正常
@Component({
selector: 'app-home',
templateUrl: '../tpl/default/home.html',
styleUrls: ['./home.component.css']
})
我需要根据从 api 获得的数据,我在其中查找 html 的文件夹名称是动态的
我尝试了几种动态传递路由的方法,但总是returns以下错误:
Failed to compile.
./src/app/home/home.component.ts
Module not found: Error: Can't resolve './{{template}}' in 'C:\angular\src\app\home'
有什么办法可以解决吗?
感谢您的帮助
由于 angular AOT 编译,您无法动态更新代码以在 angular 中加载新模板。
正在解决您的问题:
您可以创建 dynamic components for each view and an base controller which contains logic for each view. Your base controller can be extended by each components controller individually. So now your components can have their logic as well as base controller logic. Use angular routing 以在 运行 时动态更改视图。
向社区问好
通常在Angular 5,当我使用外部模板时,@Component 有以下格式,它工作正常
@Component({
selector: 'app-home',
templateUrl: '../tpl/default/home.html',
styleUrls: ['./home.component.css']
})
我需要根据从 api 获得的数据,我在其中查找 html 的文件夹名称是动态的
我尝试了几种动态传递路由的方法,但总是returns以下错误:
Failed to compile. ./src/app/home/home.component.ts Module not found: Error: Can't resolve './{{template}}' in 'C:\angular\src\app\home'
有什么办法可以解决吗?
感谢您的帮助
由于 angular AOT 编译,您无法动态更新代码以在 angular 中加载新模板。
正在解决您的问题: 您可以创建 dynamic components for each view and an base controller which contains logic for each view. Your base controller can be extended by each components controller individually. So now your components can have their logic as well as base controller logic. Use angular routing 以在 运行 时动态更改视图。