Yes I found this kind of solution too. My hope was that I can capsule the component with all parts in it for simpler reuse. Iām struggling a little bit with all this relative (is seems they are different in sass, in component, in browser, on deviceā¦) paths.
Not sure I get your question well Anna, so I would suggest to use a provider.
A provider is a simple service, that is available across all application. In this, I will just show it to use a constant value across all app, but it can do much more than that.
Create a provider to define a global āpathā value
Create a provider like this this in ionic cli: ionic generate provider mypath
Include this provider in all your app/project
Go to edit your global app file located at nameofproject/src/app/app.module.ts
Include the provider in it, so it will be global for all the app, example including a dummy page and your new provider
// Importing Pages
import { HomePage } from '../pages/home/home'
...
// Importing Providers
import { MyPath } from '../providers/mypath';
Now that provider is loaded everywhere in the app, we can edit it at: src/providers/mypath.ts, and make something like that:
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
/*
Generated class for the MyPath provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MyPath {
public broadcastedProviderPath: any;
constructor() {
let broadcastedProviderPath = "/src/images/assets"
}
}
In any page in your app, on top of page with other calls, call the provider (import { MyPath }) from āā¦/ā¦/providers/mypathā),
and try to console.log it inside the constructor () to check the value. Console.log should return ā/src/images/assetsā.
thanks for the advice. Yes, the concept of providers help to keep things together.
It seems that I am later must expand my knowledge how paths are created in components, sass and so on. Is there a resource How can I read about paths.
for example I have a www/lib folder. I have made this by advice, that is better, that scripts, that are not installed by npm or bower, will be placed there. Scripts seem working by declaring in index.html, but accessing a file in the same lib folder from a page i was not able to bring it to work.
And sass is in the moment for me a complete black hole.
As you probably guessed Iām bad with concepts, that being said, search on google and in ionic docs how a project is built (folder structure).
Sass is a supercharged version of CSS, all compiled with operations (like adding CSS rules with a plus sign, or using operators with CSS rules). But you donāt need to know Sass to use and modify stylesheet in Ionic, itās just the files are called like in sass, with the file extension .scss. Simple and classic css works in that files too.
I would pretty strongly disagree with this advice, especially if you are new to the framework. It will make it tempting for you to fall back on crutches that donāt really fit nicely into the structure of an Angular app. In general, there is probably an Angular way of doing whatever it is your things in www/lib are doing that is available via npm, and it is going to make your apps seem much more natural and idiomatic if you seek them out.
Furthermore, you do not want to be putting anything under www by yourself. You donāt own that directory. It belongs to the build system, and it can and will blast away anything under there without notice.
Once again, Iād like to read a suggestion from you. Iām trying to do the same as @anna_liebt. I built a custom component (it lives in src/components/inline-spinner/). Since itās an image preloader, Iād like to distribute it with a fallback error image. Of course I can access the entire src/assets directory, but the idea is to encapsulate everything inside this component. Iāve tried to access components/inline-spinner/my-image.svg but it looks like Ionic/Angular only is allowing me to access URLs/URIs starting only with assets, like assets/img/other.svg.
What am I missing here? I would appreciate any advice here. Thank you.
PS: I think an approach would be to insert an inline image directly in .ts/.html file, like: img.src = 'data:image/svg+xml;base64...' but I donāt like this and itās like a dirty way of doing it.