ng new project-name --style=scss --routing
初始化工程文件以后,若是运行 ng serve -o
会出现以下错误:ERROR in ./src/styles.scss (./node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/raw-css-loader.js!./node_modules/postcss-loader/src??embedded!./node_modules/sass-loader/lib/loader.js??ref--14-3!./src/styles.scss)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):
Error: Cannot find module 'node-sass'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.sassLoader (/home/local/BROCELIA/zhli/WebstormProjects/ng6-project/node_modules/sass-loader/lib/loader.js:46:72)
ℹ 「wdm」: Failed to compile.
复制代码
由于缺乏依赖包:node-sass
,可是只用sudo npm install node-sass
是行不通的,须要使用:sudo npm install --save-dev --unsafe-perm node-sass
才能够正常安装这个依赖包。css
sudo
初始化工程文件,文件夹会被锁定,致使webstorm
没法获取权限没法编辑文本,因此在terminal中使用sudo chmod 777 ng6-project
来给文件夹赋予全部权限,而后使用sudo chown -R zhli /home/local/BROCELIA/zhli/WebstormProjects
来给父文件夹赋予权限,此时就能够正常编辑文件了。Angular
工程中使用Material icons
时候,先在src/index.html
的<head>
中添加依赖:<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,700" rel="stylesheet">
复制代码
而后使用引用模板:<i class="material-icons">account_circle</i>
便可,若是图片没有刷新,尝试sudo npm install material-design-icons
而后 ng -serve -o
重启服务器。html
// 动画依赖
sudo npm install @angular/animations@latest --save
// Material icons
sudo npm install material-design-icons
复制代码
// 建立新组件
ng generate component details
// Request API 服务
ng generate service data
复制代码
在app.module,ts中引用import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
并在import中添加BrowserAnimationsModule
。 而后在component中添加依赖import {trigger, style, transition, animate, keyframes, query, stagger} from '@angular/animations';
并在@component标识符中定义动画。node
animations: [
trigger('listStagger', [
transition('* <=> *', [
query(
':enter',
[
style({ opacity: 0, transform: 'translateY(-15px)' }),
stagger(
'50ms',
animate(
'550ms ease-out',
// animate ('duration delay easing')
style({ opacity: 1, transform: 'translateY(0px)' })
)
)
],
{ optional: true }
),
query(':leave', animate('50ms', style({ opacity: 0 })), {
optional: true
})
])
])
复制代码
其中:web
open => close
, close => open
, * => open
, * => close
, close => *
, open => *
,void => *
, ':enter'
, ':leave'
camelCase
state('open', style({
height: '200px',
opacity: 1,
backgroundColor: 'yellow'
})),
复制代码
transition('open => closed', [
animate('1s')
]),
复制代码
This function targets specific HTML elements within a parent component and applies animations to each element individually
<div [@triggerName]="expression">...</div>;
import { HttpClientModule } from '@angular/common/http';
import { DataService } from './data.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
/**....**/
HttpClientModule,
],
providers: [DataService],
bootstrap: [AppComponent]
})
复制代码
Angular支持数据双向绑定: express
[hero]="selectedHero"
)。用户的修改经过事件绑定流回组件,把属性值设置为最新的值((click)="selectHero(hero)"
)。 template 内联模板的书写:包在 ECMAScript 2015 反引号 (`) 中的一个多行字符串。 反引号 (`) — 注意,不是单引号 (') — 容许把一个字符串写在多行上, 使 HTML 模板更容易阅读。npm
template: ` <h1>{{title}}</h1> <h2>My favorite hero is: {{myHero}}</h2> `
复制代码
使用模板能够不用再编写模板文件(按照需求):bootstrap
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: ` <h1>{{title}}</h1> <h2>My favorite hero is: {{myHero}}</h2> `
})
export class AppComponent {
title = 'Tour of Heroes';
myHero = 'Windstorm';
}
复制代码
ng generate class data
export class Data {
constructor( public id: number, public name: string) { }
}
复制代码
dataa = [
new Data(1, 'Windstorm'),
new Data(13, 'Bombasto'),
new Data(15, 'Magneta'),
new Data(20, 'Tornado')
];
myData = this.dataa[0];
复制代码
<button (click)="onSave($event)">Save</button>
<button *ngFor="let hero of heroes" (click)="deleteHero(hero)">{{hero.name}}</button>
<form #heroForm (ngSubmit)="onSubmit(heroForm)"> ... </form>
复制代码
模板上下文中的变量名的优先级高于组件上下文中的变量名,也就是说上面的 deleteHero(hero)
中,hero
是一个模板输入变量,而不是组件中的 hero 属性api