New angular project with scss support:
ng new my-project --style scss
if you want to use sass:
ng new my-sass-project --style scss
if you have an existing project and want to use sass:
ng set defaults.styleExt scss
This adds to .angular-cli.json:
"defaults": {
"styleExt": "scss",
"component": {
}
}
Serve project:
ng serve
Build project:
ng build --prod
Create a new component:
ng g c my-component
ng g c recipes --spec false
ng g c recipes/recipes-list
Create a new directive:
ng g d my-directive
Crete a new pipe:
ng generate pipe my-filter
ng update
Generates the webpack.config.js file:
ng eject
Install the new dependencies generated by CLI:
npm install
Now we have:
"ejected": true
in .angular-cli.jsonrun
scripts"ejected": false
to .angular-cli.json we can also use the ng
command:ng build --prod
ng build --prod --aot
Ampliar con usos mas avanzados:
https://medium.com/@s2dent/from-angular-cli-to-webpack-2-and-other-production-solutions-with-examples-f127abe06842
For example bootstrap javascript dependencies:
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
serve
or build
not --prod
)ng build -ec
Cuando hacemos ng build --prod
extrae automaticamente los estilos a un archivo externo
ng serve --app app2
ng build --app app2
ng serve --app app2 --public-host http://localhost:4200
dist
cuando ng serve
Importante por ejemplo si estamos usándolo en conjunto con bundleconfig en proyecto ASP NET. https://github.com/angular/angular-cli/issues/4366
ng serve --app app2 --delete-output-path false
src/
scss/
_variables.scss
_mixins.scss
styles.scss
@import './variables';
@import './mixins';
"styles": [
"scss/styles.scss"
],
install bootstrap:
npm install --save bootstrap
install dependencies:
npm install --save jquery popper.js
now you can:
@import "~bootstrap/scss/bootstrap";
You also can use includePaths
to tell sass to look in bootstrap folder (in angular.json
):
"styles": [
"styles.scss"
],
"stylePreprocessorOptions": {
"includePaths": [
"../node_modules/bootstrap/scss"
]
},
With this, we don't need to use the ~
. For example, let's import only the Bootstrap base tools:
src/sass/styles.scss:
@import
'functions',
'variables',
'mixins',
'print',
'reboot',
'type';
But you won’t be able to customise it as whenever you add a new path to the styles array inside .angular-cli.json
, they get appended as internal CSS between your <head>
tags in your index.html
The SASS version of Bootstrap has all the default colours, font sizes, backgrounds etc. defined in a separate file (https://github.com/twbs/bootstrap/blob/v4-dev/scss/_variables.scss). This allows you to customise it and have your own version of Bootstrap.
Create a _variables.scss
and import it in the styles.scss before bootstrap styles:
// styles.scss
@import "variables";
@import "~bootstrap/scss/bootstrap.scss";
In the _variables.scss
file provided by Bootstrap, you will see that all variables have the !default
keyword at the end.
This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one
This is why we need to import our custom variables first, otherwise they won’t be used when we import Bootstrap.
I usually tend to first start overriding the default colours provided by Bootstrap as such.
$primary: #3498db;
$secondary: #e67e22;
$success: #2ecc71;
$danger: #e74c3c;
_variables.scss
file.@import
with a relative path from the component. The CLI provides an easy way to import scss files using the ~
. No matter what component scss file we're in, we can do an import like so:@import '~src/scss/variables';
@import '~src/scss/mixins';
@import '~src/scss/base/typography';
OPTION 1: Available to all components
(https://medium.com/wdstack/angular-7-bootstrap-4-starter-25573dff23f6)
Incluir en angular.json
:
// projects > architect > build
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/popper.js/dist/umd/popper.min.js", // solo se necesitan para Dropdowns, Popovers y Tooltips, luego si usamos ngx-bootstrap no es necesario incluirlo
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
Cuando hacemos esto, se inyectan los scripts con etiquetas <script>
en el html.
Make Bootstrap available to all the apps' component: (ESTE PASO NO ES NECESARIO SI LOS INCLUIMOS EN EL angular.json NO?)
app.module.ts
import bootstrap from "bootstrap";
OPTION 2: Import scripts in your app
(https://getbootstrap.com/docs/4.3/getting-started/webpack/)
Add to your app entry point (usually index.js
or app.js
):
import 'bootstrap';
Alternatively, you may import plugins individually as needed:
import 'bootstrap/js/dist/util';
import 'bootstrap/js/dist/alert';
//...
There are two packages:
npm install --save @ng-bootstrap/ng-bootstrap
.forRoot()
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [
AppComponent, ...
],
imports: [
NgbModule.forRoot(), ...
],
bootstrap: [
AppComponent
]
})
export class AppModule {
}
NgbModule
:import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [OtherComponent, ...],
imports: [NgbModule, ...]
})
export class OtherModule {
}