2017-06-06 140 views
4

如何在不重新加載應用程序的情況下重新啓動服務?我不想使用window.location.reload()我如何重新加載/角度重新啓動服務?

+1

'window.location.reload()'會重新加載應用程序...你不能「重啓」的服務而不這樣做。 .. –

+1

重新啓動後,你到底意味着什麼?因爲它是單身人士,所以只發生一次。 – SaiUnique

回答

5

您可以使用工廠而不是簡單的服務,並在其上創建一個reset方法,將其設置爲其「初始狀態」。

例如,您可以工廠看起來是這樣的:

function MyFactory() { 

    // generate the initial structure of the service 
    let Factory = _getDefaultFactory(); 

    // expose the service properties and methods 
    return Factory; 

    /** 
    * Reset the factory back to its default state 
    */ 
    function reset() { 
     // generate the initial structure again 
     // and override the current structure of the service 
     Factory = _getDefaultFactory(); 
    } 

    /** 
    * Generate a default service structure 
    */ 
    function _getDefaultFactory() { 

     return { 

      // include the reset method so you can call it later 
      // e.g. MyFactory.reset(); 
      reset: reset 

      // include other factory properties and methods here 

     }; 

    } 

} 
相關問題