class – 在Angular 2和Ionic 2中访问整个应用程序的关键数据
发布时间:2020-12-17 07:56:53 所属栏目:安全 来源:网络整理
导读:什么是存储数据的最佳方式,我可以在Angular 2和Ionic 2 – typescript中的整个应用程序中访问这些数据. 对于用户信息,我最初的想法是在每个所需的文件中导入User类,但由于我需要的是一个用户ID,我认为最好有一些配置文件,也许是App类 – 我也可以存储环境信
什么是存储数据的最佳方式,我可以在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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |