Issues in rendering css/bootstrap classes in angular2 - css

I am having some issues in CSS/Bootstrap work when it is rendered in a angular2 component.
when I try to render the CSS/html contents in Index.html file (with proper references for CSS,JS) the application/functionality works fine. But when the same html contents along with CSS classes is rendered in the angular2 component, it doesn't work.
Based on the searches on internet I have done all these changes but nothing makes it working. I have added encapsulation: ViewEncapsulation.None for that component, still the CSS functionality doesn't work along with angular 2.
also the order of JS references file are placed in the following order
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
I have replicated the issue and checked into Github url
can somebody help me with this issue?

This is because we cant simply add js files in angular 2 by adding the ref link,
try this stack over flow solution
How to use jQuery with Angular2?
For Bootstrap you can use ng-bootstrap
https://www.npmjs.com/package/ng2-bootstrap

Related

How to add <style> tag in Vue.js component

I'm on Vue.js v2. I have a CSS stylesheet stored as a string in a variable.
import sitePackCss from '!!raw-loader!sass-loader!../../app/javascript/styles/site.sass';
I need to create a tag from my component.
<style v-html="sitePackCss" />
OR
<style>{sitePackCss}</style>
When I do either of these, I get the following error in the console:
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <style>, as they will not be parsed.
How do I get this tag onto the page?
NOTE: I know this is a hacky, non-preferred way to include styles. This solution will only get used in the context of storybook, where I need to include specific CSS files for specific stories (without storybook/webpack adding them to every story). If I use normal webpack loaders, each tag is added to every story. Importing the styles as a string is the only way I've found to sidestep that behavior.
Try to add the style to the src tag of the style in your SFC :
<style lang="sass" src="../../app/javascript/styles/site.sass">
</style>
This seems to work!
import sitePackCss from '!!raw-loader!sass-loader!../../app/javascript/styles/site.sass';
In template:
<component is="style" type="text/css">${sitePackCss}</component>
Note: the sass files have references to fonts that were not working correctly using this technique. I had to update the staticDirs config to make those paths work. https://storybook.js.org/docs/react/configure/images-and-assets

How to solve "violate CSP directive: "default-src 'self'" in Angular 8?

We have an Angular 8 single page web app deployed on the customer server. They set one of the CSP directive to: default-src 'self'. We build the Angular app using ng build --prod like any other Angular applications. After deploying, we get this error:
main-es2015.47b2dcf92b39651610c0.js:1 Refused to apply inline style because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'style-src' was not explicitly set, so 'default-src' is used as a fallback.
Look into the html code on the browser, I see something like this:
As you can see, Angular actually use tag <style> to serve the css (please correct me if I'm wrong). This violates the CSP directive mentioned in the question.
After searching around, I think Angular/React is quite bad at handling this issue, those frameworks are not built with CSP in mind. You can check out Angular github page, there is an open issue for this. Now I'm searching for a solution to overcome this, of course changing CSP policy is not an option because the customers don't want to.
How can I tell Angular not to use tag <style> in production to serve css? I think to make it works we need to set Angular in a way that it will load the css files, and then use styles in those files instead of injecting <style> into html which causes CSP issue.
Edit 1: Our project is using scss.
Edit 2: After searching around, I have found out that Angular will inject your component's styles into the DOM by using <style> element. As shown here:
Now I have an idea, because for each compinent's style will be injeced into the DOM through <style> element, we can prevent this from happening by bundling all component's style .scss file into a single style.scss file. From the image above you can see that we always have an empty <style> element, so if this works, we will endup with only one <style> element and a <link> element that link to our global style scss file. We can have multiple way to remove that empty <style> element before the page got rendered by the browser.
Now I'm stuck at configuring custom webpack to make this happen. We cant use ng eject to get the webpack.config.js file since Angular CLI 6. I've been using Angular CLI 8 so the only way for me to add custom configuration into Webpack is to use custom-webpack npm. I cant find a good config file that has the same output as my desire, please help if you know how to config webpack to bundle all component's styles scss files in Angular into a global scss file.
I think this can be an acceptable answer for my question:
First of all, my recommendation is stay away from using styleUrls. Angular will inject styles of your component into the DOM using <style> element. Secondly, if it's possible, you should know / ask for the CSP policy on the deployment server/environment. What I have been doing to resolve the issue (my project is reletively small with just a couple dozen of components):
Copy (one by one) relative link of components, put them into angular.json, in styles attribute. This is because Angular will bundle all styles in this attribute as a single css/scss file. I copy one by one because the css/scss file was designed to work with Angular View Encapsulation in the first place. Gathering all of them into one place might introduct unexpected issue, this will break the UI. Whenever copy a component style and put into styles, I check if the UI breaks because of that. This will help me narrow down the root cause if such issue happens.
For each component, after copy its component style file's relative path into styles, I remove styleUrls in #Component. This prevents Angular from injecting <style> into the DOM.
Caveats:
Gathering all styles into one single file and load them at once might cause performance issue. Luckily my company project is relatively small.
Now you need to document the new way of making styling work in your project.

Page specific css not loading

I use nativescript with VueJs
My problem is that my page specified CSS files aren't used.
My start page is start.js and in the same folder I have a start.css
but the styles aren't applied.
Do I need to something else, or configure?
Because at the docs I said that it normally should work like this.
Always refer the appropriate docs for the flavour, since you are using Vue you must follow the docs here. What you were referring to was for core js one.
With NativeScript Vue you have to write scoped styles within your component, just the same way how you would do it for a Vue based web app.
An external file can be used as Page-Specific CSS as follows in NativeScript-Vue:
<style scoped src="./Home.css"></style>
Where, Home.css is located in your components folder.
Similarly, for SCSS:
<style lang="scss" scoped src="./Home.scss"></style>
Note: You'll need to rebuild your app by if its running on an emulator/device when you make this addition to your .Vue file.

Can't get shared-styles to work in Polymer when in different folder

I need to share the same CSS with all of my Polymer components. From what I've searched, placing the link with the stylesheet directly is deprecated, so is core-style.
So I tried to use what I believe is called style module. In a separate file, named shared-styles.html, I've inserted the CSS I wanted and then I import it through <style is="custom-style" include="shared-styles"></style>.
This works when shared-styles is in the same folder as my element/component, but it wouldn't be very practical to copy and paste the CSS again and again in all the folders, so I wanted to keep the styles in its own folder. However, the styles disappear when I do that.
The importing path seems correct to me, but either way I tried to change it and nothing happens.
I've tried many other things such as adding or removing "async" from the link tag, adding or removing is=custom-style from the style tag, but no change so far.
One idea I had is, maybe the problem is how I'm testing my components, since polymer server --open only gets the files that are in the component folder.
Here's a pen, but you can't really see the problem there, since I can't put the CSS in a different folder.
Any guidance would be deeply appreciated.
Console errors:
127.0.0.1/:1 GET http://127.0.0.1:8081/shared-styles/shared-styles.html 404 (Not Found)
polymer.html:2654 Could not find style data in module named shared-styles
Seems you are not defining the dom-module id for your shared-styles.
The correct shared-styles/shared-styles.html file should be:
<dom-module id="shared-styles">
<template>
<style>
/** your CSS goes here **/
</style>
</template>
</dom-module>
Right guys, sorry if my question was confusing.
The issue was indeed related to polymer server --open only getting the files that are in the component folder, therefore ignoring the styles in a different folder.
The way I fixed it was by creating a shortcut to the outside folder inside the component folder, and then just normally inserting the style like this:
<link rel="import" href="shared-styles/shared-styles.html">
Hope it helps someone!

adding CSS to Grails

I am still relatively new to Grails. I am trying to use Bootstrap for the CSS. I have added the bootstrap files to the CSS folder. I have added the plugin. I have added links to the head of the main.gsp as links and the pathing looks correct. But still no joy. I do not want to have a <style></style> in my gsp's. Any advice on how to get the gsp to call the proper CSS?

Resources