SSR (Express) Angular 9 - Uncaught ReferenceError: $localize is not defined
SSR (Express) Angular 9 - Uncaught ReferenceError: $localize is not defined
升级到 Angular9 后,我遇到了错误:Uncaught ReferenceError: $localize is not defined
错误:您的应用程序或其依赖项之一似乎正在使用 i18n。
Angular 9引入了一个需要加载的全局$localize()
函数。
请从 Angular CLI 运行 ng add @angular/localize
。
(对于非 CLI 项目,将 import '@angular/localize/init';
添加到 polyfills.ts
文件。
对于服务器端呈现应用程序,将导入添加到您的 main.server.ts
文件。)
我的应用程序是 SSR(服务器端渲染),即使遵循了指导,我也无法理解这条消息。
- 我用 Express 服务我的文件
- 我输入了:导入
'@angular/localize/init';在 polyfill.ts
- 我也运行 ng 添加
@angular/localize
- 我只有一个 server.js Express 文件,我有
还要导入“@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);
});
感谢您的任何意见!!
我把这个:
import '../node_modules/@angular/localize/init';
在 polyfill 中,替换为:
import '@angular/localize/init';
没关系!
Angular 9引入了全局$localize()
函数,需要加载
请从 Angular CLI 运行 ng add @angular/localize
。
(对于非 CLI 项目,将 import '@angular/localize/init';
添加到您的 polyfills.ts 文件)
升级到 Angular9 后,我遇到了错误:Uncaught ReferenceError: $localize is not defined
错误:您的应用程序或其依赖项之一似乎正在使用 i18n。
Angular 9引入了一个需要加载的全局$localize()
函数。
请从 Angular CLI 运行 ng add @angular/localize
。
(对于非 CLI 项目,将 import '@angular/localize/init';
添加到 polyfills.ts
文件。
对于服务器端呈现应用程序,将导入添加到您的 main.server.ts
文件。)
我的应用程序是 SSR(服务器端渲染),即使遵循了指导,我也无法理解这条消息。
- 我用 Express 服务我的文件
- 我输入了:导入 '@angular/localize/init';在 polyfill.ts
- 我也运行 ng 添加 @angular/localize
- 我只有一个 server.js Express 文件,我有 还要导入“@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);
});
感谢您的任何意见!!
我把这个:
import '../node_modules/@angular/localize/init';
在 polyfill 中,替换为:
import '@angular/localize/init';
没关系!
Angular 9引入了全局$localize()
函数,需要加载
请从 Angular CLI 运行 ng add @angular/localize
。
(对于非 CLI 项目,将 import '@angular/localize/init';
添加到您的 polyfills.ts 文件)