我正在将 angular-cli 应用程序从“@angular/http”迁移到“@angular/common/http”,但在使其正常工作时遇到一些问题。
我有一个函数可以向我的 API 发出请求并返回以下 json。
{"Categories":[
{"name":"my first category","routetype":"category"},
{"name":"my second category","routetype":"category"}
]}
Run Code Online (Sandbox Code Playgroud)
我正在将其映射到类
export class Category {
name: string;
routetype: string;
}
Run Code Online (Sandbox Code Playgroud)
当前代码(工作):
import { Http, Response } from '@angular/http';
...
getCategories(context: SiteContext): Observable<Array<Category>> {
return this.http.get(routes.getCategories(context), { cache: true })
.map((res: Response) => <Array<Category>>res.json().Categories)
.do(data => console.log(data))
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
新代码(不起作用)。
import { HttpClient } from '@angular/common/http';
...
getCategories(context: SiteContext): Observable<Array<Category>> {
return this.httpClient.cache().get(routes.getCategories(context))
.map((res: Response) => <Array<Category>>res.json().Categories)
.do(data => console.log(data))
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
在 vs.code 中我收到以下错误消息:
[ts] …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个 Angular 应用程序。在某些时候,我注意到每次应用程序刷新(使用应用更改时ng serve)需要越来越长的时间。目前,我最多可能需要 10 秒才能看到浏览器中的更改(Chrome、也测试了 Firefox 和 Safari)。
首先我忽略了它,因为我认为这是我的浏览器。但当我后来检查时,我发现开发大小main.js高达27 MB。我做过其他更大的项目,但从未遇到过这样的问题。我也不明白这是怎么发生的。即使ng build --prod输出仍然超过 8 MB,这在我看来太大了。
当然我可以开始使用 git 回滚,但我希望可能有更好的方法来确定文件大小爆炸的原因?我希望有人能给我一些提示,从哪里开始。
这是ng version输出:
Angular CLI: 7.0.6
Node: 8.11.2
OS: darwin x64
Angular: 7.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.10.6
@angular-devkit/build-angular 0.10.6
@angular-devkit/build-ng-packagr 0.10.6
@angular-devkit/build-optimizer 0.10.6
@angular-devkit/build-webpack 0.10.6
@angular-devkit/core 7.0.6
@angular-devkit/schematics 7.0.6
@angular/cli 7.0.6
@ngtools/json-schema 1.1.0
@ngtools/webpack 7.0.6
@schematics/angular 7.0.6
@schematics/update 0.10.6
ng-packagr …Run Code Online (Sandbox Code Playgroud) 我有一个要求,我必须从子组件调用父组件函数。显而易见,我们可以使用@Output参数 withEventEmitter来实现这一点。但这里的挑战是父组件是这样的
func(param1,param2){
}
Run Code Online (Sandbox Code Playgroud)
我想从孩子那里调用这个方法,所以我使用了类似的方法。
@Output() childToParent = new EventEmitter<object>();
callParentMethod(){
this.childToParent.emit({param1:this.param1,param2:this.param2});
}
Run Code Online (Sandbox Code Playgroud)
它能够调用我见过的函数,但参数没有被传递。谁能建议我做错了什么或遗漏了什么?
我将 angualr7 与 angular-cli 一起使用,并且可以使用以下命令创建新库。
E:\Coucou>ng generate library my-lib
Run Code Online (Sandbox Code Playgroud)
默认情况下它会将其添加到项目文件夹下,但我需要在项目的子文件夹下生成库。有没有办法与 Angular-CLI 相关?
我运行 ng build --configuration=production 构建了我的应用程序并将其上传(通过 FTP 手动)到服务器(我尝试了两个不同的)。访问像 myurl.com 这样的根目录工作正常,但每当我想直接访问子页面(例如 myurl.com/subpage)时,页面都是空白的,并且出现以下错误:\nLoading module from \xe2\x80\x9chttps://myurl.com/runtime-es2015.js\xe2\x80\x9d was blocked because of a disallowed MIME type (\xe2\x80\x9ctext/html\xe2\x80\x9d).对于 polyfills-es2015 也出现同样的错误。 js, main-es2015.js
如果我访问根目录并导航到子页面,一切正常。
\n\n直到现在我尝试:
\n\n<base href=".">在index.html中设置
"target": "es5"在 tsconfig.json 中设置
手动将脚本类型从 更改type="module"为type="text/javascript"\n,如下所示:<script src="runtime-es2015.js" type="text/javascript">
Angular CLI 和 Angular 版本8.1
打字稿版本3.4.5
网页包版本4.35.2
在服务器上,我还有一个需要的 .htaccess 文件,否则访问 myurl.com/subpage 会导致 404
\n\n<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase /\nRewriteRule ^index\\.html$ …Run Code Online (Sandbox Code Playgroud) 我已发出以下命令,如图所示。我收到一条成功消息,说 Angular cli 已更新到 10.0 版本。
\n但是当我运行 ng v 命令时,我得到了旧版本号。
\n那么它真的更新了吗? \n这里有什么问题?\n如何全局更新到最新版本,以便当我通过 cli 生成新的 Angular 项目时,我可以在该项目中获得最新版本的 Angular ?
\n\n请注意-我已尝试关闭 cmd 并打开一个新的命令来检查ng v。还是一样。整个过程我做了两次。
即使之后也无法工作npm cache clean force
\n c:\\>cd Users\n\nc:\\Users>ng version\n\n _ _ ____ _ ___\n / \\ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|\n / \xe2\x96\xb3 \\ | \'_ \\ / _` | | | | |/ _` | \'__| | …Run Code Online (Sandbox Code Playgroud) ng build使用 Angular cli 命令(例如,特别是在我的情况下更改或设置环境中的变量)时,有什么方法可以传递一些参数,ng serve而无需每次更改环境文件或定义新环境?
例如我有这个environment.ts文件
export const environment = {
apiData: true,
authentication: true,
checkValidation: true,
signData: false,
mockFileDirectoryUrl: '/assets/',
saveUrl: 'http://localhost:5000/'
getUrl: 'http://localhost:5005/'
};
Run Code Online (Sandbox Code Playgroud)
我想在本地运行我的应用程序以使用signData=true标志进行测试,并SaveUrl使用以下命令进行不同的测试:ng serve signData true saveUrl "https:/localhost:5050"
我找到了这种方法How to passenvironmentvariables at build time in an Angular application using .env files,但问题是每次环境文件被完全重写时这个解决方案,但我想在运行时传递这些变量。
我正在尝试使用 GitHub Actions 在默认 Angular 项目上设置 CI 管道。我可以在本地成功运行单元测试,但遇到 GitHub Actions 问题。我需要更改什么才能使其成功运行?
\nname: Node.js CI\n\non:\n push:\n branches: [ master ]\n pull_request:\n branches: [ master ]\n\njobs:\n build:\n\n runs-on: ubuntu-latest\n\n strategy:\n matrix:\n node-version: [10.x, 12.x, 14.x]\n\n steps:\n - uses: actions/checkout@v2\n - name: Use Node.js ${{ matrix.node-version }}\n uses: actions/setup-node@v1\n with:\n node-version: ${{ matrix.node-version }}\n - run: npm ci\n - run: npm run build --if-present\n - run: npm test\nRun Code Online (Sandbox Code Playgroud)\n这是此步骤的日志输出
\n> ng test\n\nCompiling @angular/core/testing : es2015 as esm2015\nCompiling @angular/platform-browser/testing : es2015 …Run Code Online (Sandbox Code Playgroud) 我是 Angular 的新手,目前正在开发一个 Angular 应用程序,该应用程序通过 Visual Studio 创建,最近更新到最新的 Angular 版本(12)。
我的应用程序没有任何延迟加载的实现。但是当我发布应用程序时,它会生成大量名为Lazy Chunk Files的 js 文件。
我的 tsconfig.json 文件如下,
而且,我的 angular.json 文件如下,
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"Website": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"progress": true,
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"node_modules/font-awesome/css/font-awesome.min.css",
"src/styles.css"
],
"scripts": [],
"allowedCommonJsDependencies": ["@ctrl/ngx-codemirror", …Run Code Online (Sandbox Code Playgroud)visual-studio asp.net-core angular-cli asp.net-core-webapi angular
如果我想使用 ngClass、ngStyle 或 ngIf 我收到以下错误消息:
我的 app.module.ts 包含 BrowserModule 和 CommonModule 导入。我的组件在声明中。
我尝试重新启动 ngserve 和 npm install 。
任何想法如何解决这一问题?
angular-cli ×10
angular ×8
node.js ×2
angular8 ×1
asp.net-core ×1
html ×1
libraries ×1
ng-build ×1
typescript ×1