服务 localstorage
1 创建服务
ng g service services/storage
2 app.modules.t中引入配置
import {StorageService} from './services/storage.service'
provides:[StorageService]
3 在使用的地方
import {StorageService} from './services/storage.service'
var storage = new StorageService()
或
constractor(public storage:StorageService){}
this.storage.set('name','123')
this.storage.get('name')
this.storage.remove('name')
4 服务封装
storage.service.ts
set(key:string,value:any){
localStorage.setItem(key,JSON.Stringify(value))
}
get(key:any){
return JSON.parse(localStorage.getItem(key))
}
remove(key){
localStorage.removeItem(key)
}
获取DOM节点
方法1 原生js 其中必须指定类型
let box:any = document.getElementById('box')
box.style.color='red'
**方法2 @ViewChild **
<div #myBox>
引入 import {ViewChild} from "@angular/core"
获取赋值 @ViewChild('myBox') boxs:any
使用 this.boxs.nativeElement