gpt4 book ai didi

express - SSR(Express)Angular 9 - Uncaught ReferenceError : $localize is not defined

转载 作者:行者123 更新时间:2023-12-02 17:16:29 26 4
gpt4 key购买 nike

升级到 Angular 9 后,我遇到错误: Uncaught ReferenceError :$localize 未定义错误:您的应用程序或其依赖项之一似乎正在使用 i18n。Angular 9 引入了需要加载的全局 $localize() 函数。请从 Angular CLI 运行 ng add @angular/localize。(对于非 CLI 项目,请将 import '@angular/localize/init'; 添加到您的 polyfills.ts 文件中。对于服务器端渲染应用程序,请将导入添加到您的 main.server.ts 文件中。)

我的应用程序是 SSR(服务器端渲染),即使遵循指导,我也无法摆脱此消息。

  • 我通过 Express 提供文件
  • 我已经输入:导入'@角度/本地化/初始化';在polyfill.ts中
  • 我还运行了 ng add@角度/本地化
  • 我只有 Express 的 server.js 文件,并且我有还导入 '@angular/localize/init';就在其中。

这是我的 angular.json 文件:

{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"tableng": {
"i18n": {
"sourceLocale": "en-US",
"locales": {
"fr": {
"translation": "src/translate/messages.fr.xlf",
"baseHref": "/fr/"
}
}
},
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/tableng",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets",
"src/web.config",
"src/manifest.json"
],
"styles": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/font-awesome/css/font-awesome.css",
"node_modules/@fullcalendar/core/main.css",
"node_modules/@fullcalendar/resource-timeline/main.css",
"node_modules/@fullcalendar/timegrid/main.css",
"node_modules/@fullcalendar/timeline/main.css",
"src/assets/sb-admin-2.min.css",
"src/styles.css",
"src/morestyles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/jquery/dist/jquery.js",
"node_modules/popper.js/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"

]
},
"configurations": {
"fr": {
"localize": ["fr"]
},
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
],
"serviceWorker": true,
"ngswConfigPath": "src/ngsw-config.json"
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "tableng:build"
},
"configurations": {
"fr": {
"browserTarget": "tableng:build:fr"
},
"production": {
"serve:dist": "http-server -p 8080 -c-1 dist/tableng",
"browserTarget": "ng build --prod --localize --build-optimizer"
}
}
}
}
}

}}

和我的 server.js 文件:

'use strict';
const bodyParser = require('body-parser');
const $localize = require('@angular/localize');

var express = require('express');
var app = express();
var directory = '/' + (process.env.STATIC_DIR)

app.use(express.static(__dirname + directory));
app.use('/fr',function(req, res) {
res.sendFile(__dirname + '/dist/tableng/fr/index.html');

});

app.use('/',function(req, res) {
res.sendFile(__dirname + '/dist/tableng/en-US/index.html');
});

app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded({ extended: true})); // to support URL-encoded bodies

var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('express - Listening on', port);

});

感谢您的任何意见!!

最佳答案

Angular 9 引入了需要加载的全局 $localize() 函数。

请从 Angular CLI 运行 ng add @angular/localize

(对于非 CLI 项目,请将 import '@angular/localize/init'; 添加到您的 polyfills.ts 文件中)

了解更多信息:https://angular.io/guide/migration-localize

关于express - SSR(Express)Angular 9 - Uncaught ReferenceError : $localize is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59404276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com