问题
I'm unable to load images from the assets folder after deploy the angular app to firebase hosting. (build --prod) when run on localhost the images show. All other things are loading properly. this is my package json cli: ~6.2.0-beta.2 firebase-tools: 4.1.0
{
"name": "protocols",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"deploy": "ng build --prod && firebase deploy"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.1.2",
"@angular/cdk": "^6.4.5",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/material": "^6.4.5",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/pwa": "^0.8.0-beta.2",
"@angular/router": "^6.1.0",
"@angular/service-worker": "^6.1.0",
"angularfire2": "^5.0.0-rc.11",
"core-js": "^2.5.4",
"firebase": "^5.3.1",
"hammerjs": "^2.0.8",
"mat-progress-buttons": "^6.0.1",
"rxjs": "^6.0.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.8.0-beta.0",
"@angular/cli": "~6.2.0-beta.2",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.3.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~2.9.2"
}
}
tried at appComponent
<img src="../assets/image.jpg">
<img src="/assets/image.jpg">
<img src="assets/image.jpg">
none of this work.
hosting settings at firebase.json
"hosting": {
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
Are there some trick on the firebase? rewrites? someone can help me!
回答1:
Images loading now from assets in angular 6.1.3 after firebase deploy! Found the solution finally, well it works for me. The issue is the tsconfig.json change:
"outDir": "./dist/out-tsc",
to:
"outDir": "./dist",
回答2:
I also had the same issue. Here is the solution i tried
Open tsconfig.json change:
"outDir": "./dist/out-tsc"
,to:
"outDir": "./dist"
Open angular.json. make sure assets configurations are as like below
"assets": [
"src/favicon.ico",
"src/assets"
],
- Change in img tag
<br>
src="/src/assets/img/pricing_comparison.PNG"
to<br>
src="/assets/img/pricing_comparison.PNG"
.
回答3:
I had the same problem with static assets after deploying to Firebase hosting, even though they worked fine when serving from localhost. I had to change from<img src="/src/assets/img/pricing_comparison.PNG">
to<img src="/assets/img/pricing_comparison.PNG">
.
(p.s. I tried changing my tsconfig.json file according to the accepted answer, but this didn't change or resolve my error)
回答4:
If you're working in localhost
juste use url like
#login {
background-image: url('/assets/app-assets/img/gallery/login-bg.jpg');
background-size: 100% 100%;
background-repeat: no-repeat;
}
in your CSS or html component, this works for me.
just change the image url from /src/assets/your_image.png
to /assets/your_image.png
here is my angular version
Angular CLI: 8.2.2
Node: 12.8.0
OS: darwin x64
Angular: 8.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.802.2
@angular-devkit/build-angular 0.802.2
@angular-devkit/build-optimizer 0.802.2
@angular-devkit/build-webpack 0.802.2
@angular-devkit/core 8.2.2
@angular-devkit/schematics 8.2.2
@angular/cli 8.2.2
@ngtools/webpack 8.2.2
@schematics/angular 8.2.2
@schematics/update 0.802.2
rxjs 6.4.0
typescript 3.5.3
webpack 4.38.0
来源:https://stackoverflow.com/questions/51853387/angular-6-build-cant-load-images-on-firebase-hosting