升级到Angular 9后,我遇到了错误:Uncaught ReferenceError: $localize未定义
错误:看起来您的应用程序或其某个依赖项正在使用i18n。
Angular 9引入了一个全局的
我的应用程序是SSR(服务器端渲染),即使按照指导进行操作,我仍无法摆脱此消息。
$localize()
函数,需要加载它。
请从Angular CLI运行ng add @angular/localize
。
(对于非CLI项目,请将import '@angular/localize/init';
添加到您的polyfills.ts
文件中。
对于服务器端渲染应用程序,请将导入添加到您的main.server.ts
文件中。)我的应用程序是SSR(服务器端渲染),即使按照指导进行操作,我仍无法摆脱此消息。
- 我使用Express提供我的文件
- 我已经在polyfill.ts中放置了:import '@angular/localize/init';
- 我还运行了ng add @angular/localize
- 我只有一个server.js文件用于Express,并在其中也放置import '@angular/localize/init';。
{
"$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);
});
谢谢任何输入!!
package.json
文件中缺少了它。 - bvdb