什么是存储数据的最佳方式,我可以在Angular 2和Ionic 2 – typescript中的整个应用程序中访问这些数据.
对于用户信息,我最初的想法是在每个所需的文件中导入User类,但由于我需要的是一个用户ID,我认为最好有一些配置文件,也许是App类 – 我也可以存储环境信息和网址等.
我实际上不知道围绕这个的最佳实践是什么,并且在这个主题上找不到多少.
一种方法是使用您需要的所有属性创建类,并在引导应用程序时将其配置为单例.
服务:
import {Injectable} from 'angular2/angular2'; @Injectable() export class Config { constructor() {} public get USERID(): string { return "XCAMPLISHIOUS"; } }
的Bootstrap:
import {bootstrap} from 'angular2/angular2'; import {TaciIlieApp} from './app/taci-ilie'; import {Config} from './app/services/config/config'; bootstrap(TaciIlieApp,[Config]); // configuring the Config provider here will ensure a single instance is created
用法:
import {Component,Inject} from 'angular2/angular2'; import {Config} from '../../services/config/config'; @Component({ selector: 'game',templateUrl: 'app/components/game/game.html',styleUrls: ['app/components/game/game.css'],providers: [],directives: [],}) export class Game { constructor(private config: Config) { console.log(this.config.USERID); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。