Angular4.0入门

angular与其他的差别

angular cli安装

cnpm install -g @angular/cli 最新版本

cnpm uninstall -g @angular/cli 卸载全局版本

npm install -g @angular/[email protected] 指定版本

环境报错

ionic的安装

cnpm install -g cordova ionc

如果报错

npm install @ionic/app-scripts@latest --save-dev

项目解决问题

npm i --save-dev @angular-devkit/core 报错解决

"@angular/flex-layout": "^5.0.0-beta.13",

创建项目

ng new 项目名称 创建项目

cd 项目名

cnpm install // cnpm install --by=npm

ng serve --open //直接打开 npm sr

快速删除node_modules

npm install rimraf -g

rimraf node_modules

rm -rf node_modules

项目结构分析

.angular-cli.json Angular CLI 的配置文件

.editorconfig 给你的编辑器看的一个简单配置文件

.gitignore 一个Git的配置文件

karma.conf.js 给Karma的单元测试配置

src目录结构

app/app.component.{ts,html.css,spec,ts} 组件使用HTML模块,css样式和单元测试定义AppComponent组件,他是根组件

app/app.module.ts 定义AppModule,这个跟模块会告诉Angular如何申明组件

assets/* 静态文件

environments/* 这个文件夹中包括为各个环境准备的文件

favicon.ico 请把他换成你自己的图标

mian.ts 这是应用的主要入口点

jQuery和Bootstrap

安装jQuery和Bootstrap

npm install jquery --save

npm install bootstrap --save

cnpm i -S [email protected] 指定版本

让Typescript类型识别jquery和Bootstrap类型的描述文件

npm install @types/jquery --save-dev

npm install @types/bootstrap --save-dev

.angular-cli.josn 文件中的apps下scripts里分别写入

"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"

在全局引入css找到style.css文件.写入全局样式

@import "~bootstrap/dist/css/bootstrap.min.css";

生成组件

在根目录运行ng g component component/navbar 生成导航条组件(在component下创建navbar组件)并会app.module.ts 里面添加这个组件并且声明组件

组件名 用途
app 项目自带的默认component
navbar 网页/APP导航条
search 搜索区域
product 商品详情
stars 星级评分
foot 底部信息

打开我们的app.component.html把单页面应用的基本骨架搭建起来

组件

app.conponents.ts这个组件

@Component组件的装饰器

selector 用来定义组件渲染的标签名称,说白了就是组件的名字

templateUrl 用来指定组件的模板文件

styleUrls 一个数组,用来存放组件相关的样式文件

import {Components.ts} from "@angular/core"
@Component({
  selector:"app-root",
  templateUrl:"./app.component.html"
  styleUrls:["./app.component.css"]
})
export class AppComponent{
  title="app"; //组件的数据,或者函数
}

Angular的指令

  • 组件:用于构建UI组件,继承与Directive类
  • 属性指令:用于改变组件的外观或行为
  • 结构指令: 用于动态添加或者DOM元素来改变DOM布局

Component(组件)是整个框架的核心,也是终极目标,"组件化"的意义有2个:

  • 第一是分治,因为有了组件之后,我们可以把各种逻辑封装在组件内部,避免混在一起
  • 第二是复用,封装成组件之后不仅可以在项目内部复用,而且可以沉淀下来跨项目复用

typescript

npm install -g typescript 全局配置

tsc -version 查看安装的版本

tsc 文件路径 编译成js

总结

变量声明

  • let
  • const

基本数据类型

  • boolean 布尔值

  • number 数字

  • string 字符串

  • 数组 number[]或者Array

  • 元祖[number,string]

  • let arr3:[number,string]=[10,"jack"]

  • 对象object

  • 接口interface

  • interface Person{ name:string, age:number } let user:Person={ name:"jack", age:18 }

  • 任意类型any在不确定类型的情况下可以使用any但是尽量少用

  • 函数空返回值void

  • nullundefined

  • *ngFor *ngIf ng-template与[ngIf]直接给大盒子进行数据绑定

  • <li *ngFor="let todo of todos"> {{todo.title}}</li> const todos=[{id:1,title:'小明',done:true}] <footer class="footer" *ngIf="todos.length"></footer> <ng-template [ngIf]="todos.length"></ng-template>

  • ngClass="{类名:true}" //可以跟双向双向绑定一起进行绑定class

基础项目: TodoMVC

声明属性的几种方式

public 默认 可以再这个类中使用,也可以再累外面使用

protected 保护类里 他只有在当前类和他的子类里面可以访问

private 已有 只有在当前类才可以访问这个属性

<div id="{{msg}}">ffff</div>

<div [title]="msg">鼠标喵上去</div>

<div [innerHTML]="h"></div> //可以识别 constructor 里面this.h的标签内容

<li *ngFor="let item of list3;let key=index"> {{item.titlte}}---{{key}} </li>

双向数据绑定

在app.module.ts里面添加

import { FormsModule } from "@angular/forms";
 imports: [

    FormsModule //添加这个

  ],

在表单中双向绑定某个数据需要具有name属性

在html上添加双向绑定的数据

<input class="toggle" type="checkbox" [(ngModel)]="todo.done">

路由的基本处理

ng generate module app-routing --flat --module=app

app-routing.module.ts

import { RouterModule, Routes } from '@angular/router';

import { FooComponent } from './foo/foo.component'

import { BarComponent } from './bar/bar.component'

const routes: Routes = [

   {

    path: '',

    redirectTo: '/contacts', // 当请求根路径的时候,跳转到contacts联系人组件

    pathMatch: 'full' // 必须完全匹配到路径的时候才做重定向

  },

  // 路由嵌套导航

  {

    // 当我们访问contacts的时候,会先把LayoutComponent渲染出来

    path: 'contacts',

    component: LayoutComponent,

    children: [

      {

        path: '',

        component: ContactListComponent

      },

      {

        path: 'new', // 这里的new的请求路径是 /contacts/new

        component: ContactNewComponent

      },

      {

        path: 'edit',

        component: ContactEditComponent

      }

    ]

  },

  {

    path: 'foo',

    component: FooComponent

  },

  {

    path: 'bar',

    component: BarComponent

  }

]
@NgModule({

  imports: [

    RouterModule.forRoot(routes)

  ],

  exports: [ RouterModule ]

})
export class AppRoutingModule {}

打包发布

ng build --prod --aot

假设我想打包成www.w3cways.com/test/index.html则执行

ng build --base-href /test/

会生成dist文件夹,将dist文件夹中的文件,直接上传到服务器便可访问

在服务器中添加一个重定向文件(.htaccess)

````
RewriteEngine on

Don't rewrite files or directories

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

Rewrite everything else to index.html to allow html5 state links

RewriteRule ^ index.html [L]
````

压缩打包

ng build --prod --no-extract-license

倒置所有脚本资源的加载URL指向根目录。Angular Cli 提供一个参数改变该值。

ng build --prod --bh /v2/

猜你喜欢

转载自www.cnblogs.com/fangdongdemao/p/8972616.html
今日推荐