• <small id='DQz9X'></small><noframes id='DQz9X'>

    1. <tfoot id='DQz9X'></tfoot>
        • <bdo id='DQz9X'></bdo><ul id='DQz9X'></ul>
        <i id='DQz9X'><tr id='DQz9X'><dt id='DQz9X'><q id='DQz9X'><span id='DQz9X'><b id='DQz9X'><form id='DQz9X'><ins id='DQz9X'></ins><ul id='DQz9X'></ul><sub id='DQz9X'></sub></form><legend id='DQz9X'></legend><bdo id='DQz9X'><pre id='DQz9X'><center id='DQz9X'></center></pre></bdo></b><th id='DQz9X'></th></span></q></dt></tr></i><div id='DQz9X'><tfoot id='DQz9X'></tfoot><dl id='DQz9X'><fieldset id='DQz9X'></fieldset></dl></div>

        <legend id='DQz9X'><style id='DQz9X'><dir id='DQz9X'><q id='DQz9X'></q></dir></style></legend>

        在 Ionic 2/Angular 2 beta 10 中访问窗口对象

        时间:2023-09-08
        <i id='jpjIR'><tr id='jpjIR'><dt id='jpjIR'><q id='jpjIR'><span id='jpjIR'><b id='jpjIR'><form id='jpjIR'><ins id='jpjIR'></ins><ul id='jpjIR'></ul><sub id='jpjIR'></sub></form><legend id='jpjIR'></legend><bdo id='jpjIR'><pre id='jpjIR'><center id='jpjIR'></center></pre></bdo></b><th id='jpjIR'></th></span></q></dt></tr></i><div id='jpjIR'><tfoot id='jpjIR'></tfoot><dl id='jpjIR'><fieldset id='jpjIR'></fieldset></dl></div>
            <bdo id='jpjIR'></bdo><ul id='jpjIR'></ul>

              1. <legend id='jpjIR'><style id='jpjIR'><dir id='jpjIR'><q id='jpjIR'></q></dir></style></legend>

                  <tbody id='jpjIR'></tbody>
                <tfoot id='jpjIR'></tfoot>

                  <small id='jpjIR'></small><noframes id='jpjIR'>

                  本文介绍了在 Ionic 2/Angular 2 beta 10 中访问窗口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 Angular 1.x 和 Ionic 1.x 中,我可以通过依赖注入访问窗口对象,如下所示:

                  In Angular 1.x and Ionic 1.x I could access the window object through dependency injection, like so:

                  angular.module('app.utils', [])
                  
                  .factory('LocalStorage', ['$window', function($window) {
                      return {
                          set: function(key, value) {
                            $window.localStorage[key] = value;
                          },
                          get: function(key, defaultValue) {
                            return $window.localStorage[key] || defaultValue;
                          }
                      };
                  }]);
                  

                  我怎样才能在 Angular 2 中做同样的事情?离子 2?

                  How can I do the same in Angular 2 & Ionic 2?

                  推荐答案

                  您可以在不导入任何内容的情况下使用 window 对象,只需在您的打字稿代码中使用它即可:

                  You can use the window object without importing anything, but by just using it in your typescript code:

                  import { Component } from "@angular/core";
                  
                  @Component({
                       templateUrl:"home.html"
                  })
                  export class HomePage {
                  
                    public foo: string;
                  
                    constructor() {
                      window.localStorage.setItem('foo', 'bar');
                  
                      this.foo = window.localStorage.getItem('foo');
                    }
                  }
                  

                  您还可以将 window 对象包装在服务中,以便您可以模拟它以进行测试.

                  You could also wrap the window object inside a service so then you can mock it for testing purposes.

                  一个简单的实现是:

                  import { Injectable } from '@angular/core';
                  
                  @Injectable()
                  export class WindowService {
                    public window = window;
                  }
                  

                  然后,您可以在引导应用程序时提供它,以便它在任何地方都可用.

                  You can then provide this when bootstrapping the application so it's available everywhere.

                  import { WindowService } from './windowservice';
                  
                  bootstrap(AppComponent, [WindowService]);
                  

                  只需在您的组件中使用它.

                  And just use it in your components.

                  import { Component } from "@angular/core";
                  import { WindowService } from "./windowservice";
                  
                  @Component({
                       templateUrl:"home.html"
                  })
                  export class HomePage {
                  
                    public foo: string;
                  
                    constructor(private windowService: WindowService) {
                      windowService.window.localStorage.setItem('foo', 'bar');
                  
                      this.foo = windowService.window.localStorage.getItem('foo');
                    }
                  }
                  

                  更复杂的服务可以包装方法和调用,因此使用起来更愉快.

                  A more sophisticated service could wrap the methods and calls so it's more pleasant to use.

                  这篇关于在 Ionic 2/Angular 2 beta 10 中访问窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Ionic 2:本地通知图标 下一篇:如何在 Ionic 中使用 CORS 编写 Angular 2 服务?

                  相关文章

                  最新文章

                  <small id='PrP6m'></small><noframes id='PrP6m'>

                  <i id='PrP6m'><tr id='PrP6m'><dt id='PrP6m'><q id='PrP6m'><span id='PrP6m'><b id='PrP6m'><form id='PrP6m'><ins id='PrP6m'></ins><ul id='PrP6m'></ul><sub id='PrP6m'></sub></form><legend id='PrP6m'></legend><bdo id='PrP6m'><pre id='PrP6m'><center id='PrP6m'></center></pre></bdo></b><th id='PrP6m'></th></span></q></dt></tr></i><div id='PrP6m'><tfoot id='PrP6m'></tfoot><dl id='PrP6m'><fieldset id='PrP6m'></fieldset></dl></div>

                    • <bdo id='PrP6m'></bdo><ul id='PrP6m'></ul>
                  1. <tfoot id='PrP6m'></tfoot>

                      <legend id='PrP6m'><style id='PrP6m'><dir id='PrP6m'><q id='PrP6m'></q></dir></style></legend>