美文网首页
angular-cli常用命令操作指南

angular-cli常用命令操作指南

作者: 华尔街的主导曲 | 来源:发表于2019-03-22 17:31 被阅读0次

初始化

1.安装cli: npm install -g @angular/cli

2.安装目录及初始化: ng new my-app

3.启动开发服务器 cd 到当前目录运行: ng serve (--open或-o)

常用命令

1.创建一个名为 heroes 的新组件: ng (generate 或 g) component heroes

注:组件可以指定目录如: component/heroes

2.创建类似于vuex的服务如: ng g service services/common

用法:

1.app.module.ts里引入服务

//引入服务

import { StorageService } from './services/storage.service';

//引用http(可选,用就引入)

import { HttpClientModule } from '@angular/common/http';

//引入form(可选,用就引入)

import { FormsModule } from '@angular/forms';

@NgModule({

  ...

  imports: [

    FormsModule, //引入form (可选,用就引入)

    HttpClientModule //引入http (可选,用就引入)

  ],

  providers: [StorageService], //服务

})

2.直接抛出定义方法即可

import { HttpClient } from '@angular/common/http';

@Injectable({

  providedIn: 'root'

})

export class CommonService {

  public domain = "http://xxx.com/";

  constructor(public http: HttpClient) { } //引入http方法

  get(api: string) { //定义方法

    return new Promise((resolve, reject) => {

      //http用法

      this.http.get(this.domain + api).subscribe((response) => {

        resolve(response)

      })

    });

  }

}

3.引用页面用法

import { CommonService } from '../../services/common.service';

@Component({

  selector: 'app-home',

  templateUrl: './home.component.html',

  styleUrls: ['./home.component.scss']

})

export class HomeComponent implements OnInit {

  public list: any[] = [];

  constructor(public common: CommonService) {} //类初始化引用

  ngOnInit() {

    //调用

    this.common.get("api/productlist").then((response: any) => {

      this.list = response.result;

    })

  }

}

模块化开发设置自定义模块

命令:ng g module module/user (加 --routing 是给模块里建路由用,实现模块懒加载)

1.模块创建完需要暴露出去:在创建的模块module里的 

@NgMoule({

...

exports:[xx(模块名)]

})

注:模块里创建的模块除非exports里暴露出去的组件,否则组件没法在全局引用

2.在app.module.ts里引入模块先头部import {xx} from '路径' 引入在把名字加到 

@NgMoule({

...

imports:[xx(模块名)]

})

给模块里建组件:ng g component module/user/component/home

注:创建服务或其他也是: ng g service module/user/services/common

模块路由懒加载

命令:ng g module module/user --routing

1.app-routing里引入

const routes: Routes = [

  //模块懒加载

  { path: 'login', loadChildren: './module/login/login.module#LoginModule' },

  //设置没有路由默认值

  { path: '**', redirectTo: 'login' }

];

相关文章

  • angular-cli常用命令操作指南

    初始化 1.安装cli: npm install -g @angular/cli 2.安装目录及初始化: ng n...

  • angular升级

    1. 升级angular-cli 1.1 卸载旧的angular-cli 首先先卸载一下,以防万一。 1.2 操作...

  • Anaconda总结

    参考网址:Anaconda完全入门指南Anaconda常用命令

  • docker常用命令总结

    参考链接 Docker 常用命令与操作 docker常用命令总结——安装、镜像、容器基本操作

  • Mysql NO.1 库&表级操作常用命令

    库级操作常用命令: 表级操作常用命令: 插入数据: 查询数据: 修改数据: 删除数据:

  • Linux常用命令总结

    Linux常用命令指南 @Date 2017.05.23 tail awk awk ' pattern {acti...

  • git常用命令

    初始配置 常用命令 git操作原则 vim基本操作

  • 大数据技术学习路线

    一、大数据技术基础1、linux操作基础 linux系统简介与安装linux常用命令–文件操作linux常用命令–...

  • angular-cli工程搭建指南

    前端工程搭建的三种手段 全程手动,一点点自己搭建,对技术人员要求比较高,必须对框架有深入的理解和丰富的经验,要能h...

  • Shell命令汇总

    1、一般常用命令 (1)Shell 常用命令总结 (2)Shell的18条常用命令整理 2、文件操作常用命令 (1...

网友评论

      本文标题:angular-cli常用命令操作指南

      本文链接:https://www.haomeiwen.com/subject/ntmrvqtx.html