angular - 动态更改 TeplateUrl
问题描述
我正在尝试
templateUrl
动态制作,但它似乎在 UI 中返回 URL 字符串。
这是我的代码
@Component({
selector: 'app-login',
templateUrl: '../login/'+Setting.URL+'/login.html',
// templateUrl: '../login/web/login.html',
styleUrls: ['./login.component.scss']
});
问题是,模板 url 不读取 html 文件位置。
解决方案
动态 url 的正确方法是通过深度链接,始终使用相同的 html 模板但具有不同的信息。
在您的路由器中:
const routes: Routes = [
{
path: 'login/:name',
component: LoginComponent,
}
];
...
RouterModule.forRoot(routes, {useHash: true})
在您的组件中:
@Component({
selector: 'app-login',
templateUrl: '../login/web/login.html',
styleUrls: ['./login.component.scss']
});
constructor(
private route: ActivatedRoute,
) {
this.route.params.subscribe((params) => {
console.log(params.name);
});
}
推荐阅读
- google-analytics - Google Signals/Remarketing - check if it's enabled?
- amazon-web-services - 使用 Cognito 和 Lambda 函数的 AWS 无服务器 DynamoDB 行级授权
- android - AAPT:错误:找不到资源 attr/(各种)
- dictionary - 如何在颤动中操作地图?
- javascript - 在单选按钮中显示布尔值
- python - 另一个框架内的框架在python Tkinter中
- mysql - 使用多种编码生成包含列的表
- ms-access - 访问功能区 OnAction
- android - TextView 文本更改但布局不更新(Kotlin - Android)
- oracle - 为什么我不能创建包含多个图像的 ORACLE 12c 报告?