0

I'm trying to learn Angular2, but i'm little bit confused about Component styles, in the component decorator we can specify the styles to apply them to our component, there are two ways to do this :

1- We can specify the css file in styleUrls property which is an array of strings, this is logic to me, because we might have multiple css files

@Component({
    selector: 'users-list',
    templateUrl: 'app/users-list.template.html',
    styleUrls: ['style1.css', 'style2.css']
})

2- We can write our style in the styles property which is an array of strings too, this is where i'm getting confused!! i mean why styles property is an array of strings ? why it's not just a single string !

@Component({
    selector: 'users-list',
    templateUrl: 'app/users-list.template.html',
    styles: [
        `
            .glyphicon{
                cursor: pointer;
            }
        `
    ]
})

In the official documentation of Angular2 they didn't say why they put an array of strings there instead of one string ! can anyone explain me!!

1
  • actually they are considering it as an json string and from that they selecting the class and to that class they applying the behaviors Commented Oct 20, 2016 at 12:00

3 Answers 3

1

All of the CSS strings provided in the styles array will be applied to the component in combination. At first glance, this might sound useless, but it comes in handy if you want to mix multiple styles that aren't all hard-coded to your component. For example:

import commonStyles from "./commonStyles";

@Component({
    selector: 'users-list',
    templateUrl: 'app/users-list.template.html',
    styles: [
        ".glyphicon { cursor: pointer; }",
        commonStyles.background // This string of style rules gets applied too!
    ]
})

EDIT: To answer the questions in the comments:

commonStyles in this case would just be a normal JavaScript object containing CSS strings:

export default {
    background: "div { background-color: #00FF00; }"
};

And an advantage of doing things this way is that your application doesn't have to make additional HTTP requests to go get styleUrls (that said, I'm not familiar enough with Angular 2 to say whether or not this makes a tangible difference).

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please exaplain more what's inside the commonStyles ? is it an ordinary stylesheet file ?
What's the difference or advantage of using styles with commonStyles.background to styleUrls?
0

You can for example use a shared style file

@Component({
    selector: 'users-list',
    templateUrl: 'app/users-list.template.html',
    styleUrls: ['users-list.css', 'common-theme.css']
})

1 Comment

Well i'm asking about the styles propoerty and not about styleUrls.
0

So they can get the files through iterating specially if the array items is more than two.

That's the way they program instead of using 'style1.css style2.css'

If they used string. They will add one function.

  1. Split the sting. Output array
  2. Iterate array to get the files.

They used array to shorten the process and make application much faster. If you're building a small app using angular2. It won't hurt you. But if you are dealing with millions of data. It can also help but not too much. Atleast.

Regards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.