gpt4 book ai didi

javascript - 以下 `src=' ${loadingGif }'` 如何在 Angular 组件内工作

转载 作者:太空狗 更新时间:2023-10-29 18:25:54 25 4
gpt4 key购买 nike

我正在阅读关于 angular 2 的书 ng-book 并且有以下内容:

let loadingGif: string = ((<any>window).__karma__) ? '' : require('images/loading.gif');

@Component({
selector: 'youtube-search',
template: `
<div class='container'>
<div class="page-header">
<h1>YouTube Search
<img
style="float: right;"
*ngIf="loading"
src='${loadingGif}' />
</h1>
</div>

我对这部分感兴趣:

src='${loadingGif}'

书中的简短说明如下:

Notice that our img has a src of ${loadingGif} - that loadingGif variable came from a require statement earlier in the program. Here we’re taking advantage of webpack’s image loading feature. If you want to learn more about how this works, take a look at the webpack config in the sample code for this chapter or checkout image-webpack-loader⁴².

但没有详细信息。有人可以请问这一切是如何运作的吗?

最佳答案

这仅适用于内联模板(*.ts 文件中的模板),但当模板位于 *.html 文件(如 templateUrl: './my.component.html).

src='${loadingGif}'

是 TypeScript 字符串插值,与 Angular 无关。它将 ${loadingGif} 替换为 loadingGif

的内容

关于javascript - 以下 `src=' ${loadingGif }'` 如何在 Angular 组件内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com