如何使用Angular访问从TS文件到HTML文件的可变值?
front-layout.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-front-layout',
templateUrl: './front-layout.component.html',
styleUrls: ['./front-layout.component.css']
})
export class FrontLayoutComponent implements OnInit {
host:any;
constructor() { }
ngOnInit(): void {
this.host = "http://localhost:4200";
}
}
front-layout.component.html
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<title></title>
<link rel="stylesheet" href="'{{ host }}'/assets/front/css/bootstrap.min.css">
<link rel="stylesheet" href="'{{ host }}'/assets/front/plugins/select2/css/select2.min.css">
</head>
<body>
<div class="main-wrapper">
<router-outlet></router-outlet>
</div>
<script src="'{{ host }}'/assets/front/js/jquery-3.6.0.min.js"></script>
</body>
</html>
在上述代码中我只是声明host
host 并尝试在我的html组件中访问,但它会丢弃一个错误,即src/app/app/组件/front/front-layout/front-layout.component.ts:5:16-错误ng2008:找不到stylesheet文件''{{host}}'/assets/assets/css/css/bootstrap.min.min.css'链接从模板。
。不知道为什么变量不起作用?请帮我。
谢谢
front-layout.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-front-layout',
templateUrl: './front-layout.component.html',
styleUrls: ['./front-layout.component.css']
})
export class FrontLayoutComponent implements OnInit {
host:any;
constructor() { }
ngOnInit(): void {
this.host = "http://localhost:4200";
}
}
front-layout.component.html
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<title></title>
<link rel="stylesheet" href="'{{ host }}'/assets/front/css/bootstrap.min.css">
<link rel="stylesheet" href="'{{ host }}'/assets/front/plugins/select2/css/select2.min.css">
</head>
<body>
<div class="main-wrapper">
<router-outlet></router-outlet>
</div>
<script src="'{{ host }}'/assets/front/js/jquery-3.6.0.min.js"></script>
</body>
</html>
In the above code I simply declare host
and try to access in my html component but it throw an error i.e. src/app/component/front/front-layout/front-layout.component.ts:5:16 - error NG2008: Could not find stylesheet file ''{{ host }}'/assets/front/css/bootstrap.min.css' linked from the template.
. No idea why variable not working? Please help me.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您应该将链接和脚本移至
index.html
,然后从那里使用此路径I believe you should move the links and scripts to
index.html
, and from there, use this path要加载
主机
必须是
[HREF]
和[src]
而不是HREF
和src
而不是
href =“'{{host}}'/assets/front/css/bootstrap.min.css”
必须是
[href] =“ host +'/assets/front/css/bootstrap.min.css'”
,在Angular中,所有这些内容都是在
index.html
中完成的但 动态加载脚本
脚本
如何从任何组件
。
中 /code>函数
this.scriptservice.load('script's name')
或this.scriptservice.load('script's name1','script's name2','script's name3')
To load the content of
host
Must be
[href]
and[src]
instead ofhref
andsrc
and instead of
href="'{{ host }}'/assets/front/css/bootstrap.min.css"
must be
[href]="host + '/assets/front/css/bootstrap.min.css'"
But in angular all this stuff is done in
index.html
This is how you dynamically load scripts in angular from any component
script.service.ts
store.script.ts
interfaces/schema.script.ts
Then you import
script.service
in any component and callload
functionthis.scriptService.load('script's name')
orthis.scriptService.load('script's name1', 'script's name2', 'script's name3')