angular4入门 (angluar与elementUI环境搭建、路由配置、get请求运用)

环境搭建说明:
1、全局安装angluar脚手架
    npm install -g @angular/cli
    
2、初始化项目(支持scss)
    ng new 项目名称 --style=scss
    // 进入项目
    cd 项目名称
    运行代码可以是:serve或者 npm install(安装依赖)  npm start(运行)
    
3、安装element
    npm i --save element-angular
    a、在跟模快app/app.module.ts文件中引入
        import { NgModule } from '@angular/core'
        import { BrowserModule } from '@angular/platform-browser'
        import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
        import { RouterModule } from '@angular/router'
        import { AppComponent } from './app.component'
        import { ElModule } from 'element-angular'
        // 引入实例组件ExAppComponent
       import { ExAppComponent } from '路径'
        @NgModule({
           declarations: [
              AppComponent,
              ExAppComponent, // 实例组件
           ],
           imports: [
              BrowserModule,
              BrowserAnimationsModule,
              ElModule.forRoot(),
           ],
           providers: [],
           bootstrap: [ExAppComponent],
        })
        export class AppModule {
        }
        
    b、在src/style.scss文件下添加一行代码
       @import "~element-angular/theme/index.css"; (引入element样式)
       
4、安装路由
    npm i --save @angular/router
    a、在跟模快app/app.module.ts文件中引入
    import { RouterModule, Routes } from '@angular/router';
    import { Demo01Component } from './demo01/demo01.component';
    const appRoutes: Routes = [
      { path: 'demo', component: Demo01Component },];   // 这里需要注意,路由地址不能加/
    @NgModule({
      imports: [
        RouterModule.forRoot(
          appRoutes,
          { enableTracing: true } // <-- debugging purposes only
        ),
        其他的模块写入
      ]
    })
    
    b、去模块下进行调用路由
        <nav>
          <a routerLink="/demo" routerLinkActive="active">Crisis Center</a>
        </nav>
        <router-outlet></router-outlet>
    
5、get接口调用
   a、在app文件下创建一个文件夹名为config,在config文件下创建service服务
      在config.service.ts中输入
        import { Injectable } from '@angular/core';
        import { HttpClient } from '@angular/common/http';
        @Injectable({
          providedIn: 'root'
        })
        export class ConfigService {
          constructor(private http: HttpClient) {
          }
          getConfig(myUrl: string) {
            return this.http.get(myUrl);
          }
        }
   b、app/app.module.ts文件中引入
      import { HttpClientModule } from '@angular/common/http';
      import { ConfigService } from './config/config.service';
      @NgModule({
        declarations: [
          AppComponent,   // 主文件
          ProjectActivityComponent,  // 子文件
        ],
        imports: [
          BrowserModule,
          HttpClientModule
        ],
        providers: [ConfigService],
        bootstrap: [AppComponent]
      })
      
   c、去project-activity.component.ts文件下进行调用接口
       引入文件  import { ConfigService } from '../config/config.service';
       export class ProjectActivityComponent implements OnInit {
         constructor(private myServce: ConfigService) {}
         ngOnInit() {
           // 界面初始化的时候进行调用
           this.showConfig();
         }
         showConfig() {
           this.myServce.getConfig('http://jsonplaceholder.typicode.com/users')
             .subscribe((result) => {
              console.log(result, 'ddd');
             });
         }
       }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
angular5.x中文手册,移动APp开发工具目录 一、安装最新版本的nodejs.............................................................................................................1 二、全局安装Angular CLI 脚手架工具.......................................................................................... 2 三、创建项目..................................................................................................................................... 2 四、目录结构分析.............................................................................................................................2 1. app.module.ts、组件分析..................................................................................................... 3 2 创建angualr 组件...................................................................................................................4 五、创建angualr 组件使用组件.................................................................................................... 5 1. 数据文本绑定........................................................................................................................5 2.绑定属性绑定html.............................................................................................................. 6 3、*ngFor 普通循环........................................................................................................... 6 4、条件判断...............................................................................................................................7 5、执行事件...............................................................................................................................7 6、双向数据绑定.......................................................................................................................7 六、创建angualr 服务使用服务...................................................................................................... 8 七、angular5.x get post 以及jsonp 请求数据........................................................................... 10 八、路由........................................................................................................................................... 14 8.1 创建路由.............................................................................................................................14 8.2 动态路由.............................................................................................................................15 8.3 默认跳转路由.....................................................................................................................16 8.4 routerLinkActive 设置routerLink 默认选中路由..............................................................17 8.5 路由的js 跳转.................................................................................................................... 17 8.6 父子路由.............................................................................................................................18 九、父子组件传值...........................................................................................................................19 十、获取dom 节点操作dom 节点..............................................................................................21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值