Angular2에서 이미지를 제공하는 방법은 무엇입니까?
Angular2 앱의 이미지 src 태그에있는 자산 폴더의 내 이미지 중 하나에 대한 상대 경로를 입력하려고합니다. 내 구성 요소의 변수를 'fullImagePath'로 설정하고 내 템플릿에서 사용했습니다. 여러 가지 가능한 경로를 시도했지만 내 이미지를 얻을 수없는 것 같습니다. 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>
앱 트리 * 공간을 절약하기 위해 노드 모듈 폴더를 생략했습니다.
├── 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
Angular only points to src/assets
folder, nothing else is public to access via url so you should use full path
this.fullImagePath = '/assets/images/therealdealportfoliohero.jpg'
Or
this.fullImagePath = 'assets/images/therealdealportfoliohero.jpg'
This will only work if the base href tag is set with /
You can also add other folders for data in angular/cli
. All you need to modify is angular-cli.json
"assets": [
"assets",
"img",
"favicon.ico",
".htaccess"
]
Note in edit : Dist command will try to find all attachments from assets so it is also important to keep the images and any files you want to access via url inside assets, like mock json data files should also be in assets.
If you do not like assets folder you can edit .angular-cli.json
and add other folders you need.
"assets": [
"assets",
"img",
"favicon.ico"
]
Add your image path like fullPathname='assets/images/therealdealportfoliohero.jpg'
in your constructor. It will work definitely.
I am using the Asp.Net Core angular template project with an Angular 4 front end and webpack. I had to use '/dist/assets/images/' in front of the image name, and store the image in the assets/images directory in the dist directory. eg:
<img class="img-responsive" src="/dist/assets/images/UnitBadge.jpg">
Just put your images in the assets folder refer them in your html
pages or ts
files with that link.
참고URL : https://stackoverflow.com/questions/40230473/how-to-serve-up-images-in-angular2
'Program Tip' 카테고리의 다른 글
HTTPS없이 로그인, 보안 방법은? (0) | 2020.10.19 |
---|---|
여러 프로세스간에 결과 큐 공유 (0) | 2020.10.19 |
C ++ : enum 값을 텍스트로 인쇄 (0) | 2020.10.19 |
OpenGL 셰이더에 대한 명시 적 및 자동 속성 위치 바인딩 (0) | 2020.10.19 |
중첩 된 Try / Catch 블록이 나쁜 생각입니까? (0) | 2020.10.19 |