Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2窗口全局对象未定义_Javascript_Angular_Angular Universal - Fatal编程技术网

Javascript 角度2窗口全局对象未定义

Javascript 角度2窗口全局对象未定义,javascript,angular,angular-universal,Javascript,Angular,Angular Universal,供参考: 正在全局添加Universal cache对象,初始状态如下所示,在发送到客户端的html中: <script> window.UNIVERSAL_CACHE = { } // stuff gets loaded here </script> 不幸的是,win[UNIVERSAL\u KEY]始终是未定义的,即使我可以console.log(win)查看它,或者在开发工具中键入console.log(window.UNIVERSAL\u CACHE)查看它

供参考:

正在全局添加Universal cache对象,初始状态如下所示,在发送到客户端的html中:

<script>
  window.UNIVERSAL_CACHE = { } // stuff gets loaded here
</script>
不幸的是,
win[UNIVERSAL\u KEY]
始终是未定义的,即使我可以
console.log(win)
查看它,或者在开发工具中键入
console.log(window.UNIVERSAL\u CACHE)
查看它。你知道为什么会这样吗?

(我没有足够的声誉来发表评论)

问题来自此功能,该功能似乎尚未完成:

当我想让消息在生产中消失时,我会手动从我的generate js文件中删除
injectCacheInDocument
函数。

(我没有足够的声誉来发表评论)

问题来自此功能,该功能似乎尚未完成:


当我想让消息在生产中消失时,我会手动从我的generate js文件中删除
injectCacheInDocument
函数。

Hey。谢谢我想有人提到在webpack.client.ts中使用这个,这对我有帮助:
var ScriptExtHtmlWebpackPlugin=require('script-ext-html-webpack-plugin');module.exports={plugins:[new-ScriptExtHtmlWebpackPlugin({async:['client.bundle.js'],defaultAttribute:'sync'})]嘿。谢谢我想有人提到在webpack.client.ts中使用这个,这对我有帮助:
var ScriptExtHtmlWebpackPlugin=require('script-ext-html-webpack-plugin');module.exports={plugins:[new-ScriptExtHtmlWebpackPlugin({async:['client.bundle.js'],defaultAttribute:'sync'})]
// imports

export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';

@ngModule({
  // bootstrap, imports, providers, etc.
})
export class AppBrowserModule {

    constructor(public cache: CacheService) {
        this.doRehydrate();
    }

    // Universal Cache "hook"
    doRehydrate() {
        let defaultValue = {};
        let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
        this.cache.rehydrate(serverCache);
    }

    // Universal Cache "hook
    _getCacheValue(key: string, defaultValue: any): any {
        // Get cache that came from the server
        const win: any = window;
        /* I can console.log(win) to see the window object with .UNIVERSAL_CACHE, however if I console.log(win[UNIVERSAL_KEY]) it is undefined. */
        if (win[UNIVERSAL_KEY] && win[UNIVERSAL_KEY][key]) {
            let serverCache = defaultValue;
            try {
                serverCache = JSON.parse(win[UNIVERSAL_KEY][key]);
                if (typeof serverCache !== typeof defaultValue) {
                    console.log('Angular Universal: The type of data from the server is different from the default value type');
                    serverCache = defaultValue;
                }
            } catch (e) {
                console.log('Angular Universal: There was a problem parsing the server data during rehydrate');
                serverCache = defaultValue;
            }
            return serverCache;
        } else {
            console.log('Angular Universal: UNIVERSAL_CACHE is missing');
        }
        return defaultValue;
    }
}