Angular2アプリの画像srcタグのassetsフォルダに画像の相対パスを入れようとしています。私は、コンポーネント内の変数を 'fullImagePath'に設定し、それをテンプレートで使用しました。私は多くの異なる可能な道を試みましたが、私のイメージを上げるように見えません。 Angular2にDjangoのような静的フォルダとの相対的な相対パスがある特別なパスがありますか?Angular2で画像を配信するにはどうすればよいですか?
コンポーネント
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-hero',
templateUrl: './hero.component.html',
styleUrls: ['./hero.component.css']
})
export class HeroComponent implements OnInit {
fullImagePath: string;
constructor() {
this.fullImagePath = '../../assets/images/therealdealportfoliohero.jpg'
}
ngOnInit() {
}
}
私も、このコンポーネントと同じフォルダに画像を入れ、同じフォルダ内のテンプレート、およびCSS以来働いているので、私はよく分からない理由と同様の相対パスイメージは機能していません。これは、同じフォルダ内の画像と同じコンポーネントです。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-hero',
templateUrl: './hero.component.html',
styleUrls: ['./hero.component.css']
})
export class HeroComponent implements OnInit {
fullImagePath: string;
constructor() {
this.fullImagePath = './therealdealportfoliohero.jpg'
}
ngOnInit() {
}
}
HTML
<div class="row">
<div class="col-xs-12">
<img [src]="fullImagePath">
</div>
</div>
アプリツリーは*私は宇宙にsrc/assets
フォルダへ
├── README.md
├── angular-cli.json
├── e2e
│ ├── app.e2e-spec.ts
│ ├── app.po.ts
│ └── tsconfig.json
├── karma.conf.js
├── package.json
├── protractor.conf.js
├── src
│ ├── app
│ │ ├── app.component.css
│ │ ├── app.component.html
│ │ ├── app.component.spec.ts
│ │ ├── app.component.ts
│ │ ├── app.module.ts
│ │ ├── hero
│ │ │ ├── hero.component.css
│ │ │ ├── hero.component.html
│ │ │ ├── hero.component.spec.ts
│ │ │ ├── hero.component.ts
│ │ │ └── portheropng.png
│ │ ├── index.ts
│ │ └── shared
│ │ └── index.ts
│ ├── assets
│ │ └── images
│ │ └── therealdealportfoliohero.jpg
│ ├── environments
│ │ ├── environment.dev.ts
│ │ ├── environment.prod.ts
│ │ └── environment.ts
│ ├── favicon.ico
│ ├── index.html
│ ├── main.ts
│ ├── polyfills.ts
│ ├── styles.css
│ ├── test.ts
│ ├── tsconfig.json
│ └── typings.d.ts
└── tslint.json
なぜ相対パスを使用しますか?ルートから開始するように設定することはできません(つまり、 'this.fullImagePath = '/ assets/images/therealdealportfoliohero.jpg'') –
残念ながらそれは機能しませんでした。アプリケーションは、ルートがどこにあるかをどのように知っていますか?それはプロジェクトのルートですか、それともCSS、画像、JavaScriptなどの静的ファイルのアセットですか? – JBT
それは本当にあなたの設定にもかかわらず依存します。私にとって、私はコード部分から資産を分離しています(そのために、さまざまなグループ・タスクを持つことによって)、資産がどこにあるのか正確に知ることができます。 –