博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular中的服务和持久化实现
阅读量:4693 次
发布时间:2019-06-09

本文共 1558 字,大约阅读时间需要 5 分钟。

1.创建服务:

ng g service my-new-service创建到指定目录下面ng g service services/storage

2.app.module.ts 里面引入创建的服务

 

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

 

NgModule 里面的 providers 里面依赖注入服务import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { FormsModule } from '@angular/forms';import { AppRoutingModule } from './app-routing.module';import { AppComponent } from './app.component';import { HomeComponent } from './components/home/home.component';import { StorageService } from './services/storage.service';@NgModule({  declarations: [    AppComponent,    HomeComponent  ],  imports: [    BrowserModule,    FormsModule,    AppRoutingModule  ],  providers: [StorageService],  bootstrap: [AppComponent]})export class AppModule { }
3.使用的页面引入服务,注册服务
import { Component } from '@angular/core';import { StorageService } from './services/storage.service';@Component({  selector: 'app-root',  templateUrl: './app.component.html',  styleUrls: ['./app.component.scss']})export class AppComponent {  constructor(private storage : StorageService){  }  title = 'angulardemo';    key= 1  name='张三';  list:any=[]    addData(){    // alert(this.username);    this.list.push(this.name);     this.storage.set('todolist',this.list); } removerData(key){      console.log(key);       this.list.splice(key,1);      this.storage.set('todolist',this.list);}}

页面设计

Welcome to {
{ title }}!

效果:

 

 

转载于:https://www.cnblogs.com/loaderman/p/10895421.html

你可能感兴趣的文章
LeetCode OJ 238. Product of Array Except Self 解题报告
查看>>
使用外网访问阿里云服务器ZooKeeper
查看>>
Java代码检查工具
查看>>
深入了解VC++编译器【转】
查看>>
响应式图片
查看>>
如何选择 compileSdkVersion, minSdkVersion 和 targetSdkVersion
查看>>
iOS音频播放(一):概述
查看>>
Android之使用AchartEngineActivity引擎绘制柱状图、曲线图
查看>>
android对象巧用Android网络通信技术,在网络上直接传输对象
查看>>
android下载手动下载Android SDK
查看>>
oracle12c(oracle12.1.0.1.0)安装指南--实测OEL5.9(RH5)
查看>>
北京邮电大学 程序设计课程设计 电梯 文件输入版本(已调试,大致正确运行==)...
查看>>
HashMap的两种排序方式
查看>>
Spring的第一个例子
查看>>
从Firefox升级说学习方法
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
【杂文】记一些有用的神奇网站
查看>>
什么是“闭包”(closure)为什么要用它?
查看>>
log4j2简单使用
查看>>
wget使用小技巧
查看>>