角度 RouteReuseStrategy 滚动位置错误。
当我从另一个页面返回列表页面时,滚动条回到顶部
import {ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy} from '@angular/router';
export class SystemRouteReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
if (route.data.reuse) {
return true;
}
return false;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
this.handlers[route.routeConfig.path] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
}
Run Code Online (Sandbox Code Playgroud) 我已经创建了 Angular 12 应用程序,并尝试使用“ ng2-charts 和 Chart.js ”将条形图包含在我的应用程序中
我已在 app.module.js 和 component.module.js 文件中导入了NgChartsModule 。但是,我仍然面临以下错误
“无法绑定到‘chartType’,因为它不是‘canvas’的已知属性”
包.json
"dependencies": {
"@angular/animations": "~12.2.0",
"@angular/common": "~12.2.0",
"@angular/compiler": "~12.2.0",
"@angular/core": "~12.2.0",
"@angular/forms": "~12.2.0",
"@angular/platform-browser": "~12.2.0",
"@angular/platform-browser-dynamic": "~12.2.0",
"@angular/router": "~12.2.0",
"bootstrap": "^5.0.0-beta3",
"chart.js": "^3.5.1",
"ng2-charts": "^3.0.0-rc.5",
"rxjs": "~6.6.0",
"tslib": "^2.3.0",
"zone.js": "~0.11.4"
Run Code Online (Sandbox Code Playgroud)
}
mycomponent.module.ts
import { Component, OnInit } from '@angular/core';
import { ChartDataset, ChartOptions, ChartType } from 'chart.js';
import { NgChartsModule } from 'ng2-charts'
@Component({
selector: 'app-expence-manipulation',
templateUrl: './expence-manipulation.component.html',
styleUrls: ['./expence-manipulation.component.css']
})
export …Run Code Online (Sandbox Code Playgroud) 我将 Angular 应用程序更新到 v16,并尝试使用 Signals。之前,我在没有初始值的情况下初始化变量
filters: Filter[];
Run Code Online (Sandbox Code Playgroud)
但当我使用信号时,它给了我一个错误。
An argument for 'initialValue' was not provided
有没有办法可以在不初始化信号值的情况下创建信号?
我尝试将其 Type 设置为可能为 null 并赋予它 null 值
signal<Category | null>(null);
Run Code Online (Sandbox Code Playgroud)
但文件中还发生了一些其他错误,例如
Object is possibly 'null'.
我不喜欢使用“任何”类型
那么请问您能提供另一种解决方案吗?
有了 Angular 16,我们现在可以使用信号了。
但我似乎不清楚如何简单地推入数组。
该文档来自Angular Signal 官方文档:
当处理包含对象的信号时,直接改变该对象有时很有用。例如,如果对象是数组,您可能希望推送新值而不完全替换数组。要进行这样的内部更改,请使用 .mutate 方法
例如:
messages = signal<Message[]>([]);
ngOnInit(): void {
const newMessage = ...
this.messages.mutate(values => values.push(newMessage));
}
Run Code Online (Sandbox Code Playgroud)
但两次引用该列表似乎很奇怪。第一次与this.messages,第二次与values。
如何将项目推入信号数组?
我正在为 Angular 应用程序编写单元测试来测试前端功能,并尝试生成一种方法来模拟以下场景的休息服务调用:
\n我有一个类定义如下:
\nimport { Component, OnInit } from '@angular/core';\nimport {RestService} from "../../../../../services/rest.service";\nimport {ActivatedRoute, Router} from "@angular/router";\n\n@Component({\n selector: 'app-example-class',\n templateUrl: './example-class.component.html',\n styleUrls: ['./example-class.component.scss']\n})\nexport class ExampleClass implements OnInit {\n\n myData: any = [];\n reloadInterval: any;\n constructor(private rest: RestService) {\n this.reloadInterval = setInterval(() => {\n this.getData();\n }, 10000);\n }\n\n ngOnInit() {\n this.getData();\n }\n\n ngOnDestroy(){\n clearInterval(this.reloadInterval);\n }\n\n getData() {\n this.rest.call(\xe2\x80\x98exampleClass\xe2\x80\x99, this.rest.id).subscribe((data: {}) => {\n if (data['rows']) {\n this.myData = data['rows'].reduce((accumulator, currRow)=> accumulator + currRow.value, 0);\n }\n });\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n我想使用 …
this.userService.addUser(this.user).subscribe(\n (data)=>{\n //success\n console.log(data);\n this._snack.open('Registered Successfully', 'OK', {\n duration: 2000,\n verticalPosition: 'top',\n horizontalPosition: 'center',\n panelClass: ['green-snackbar', 'login-snackbar'],\n });\n },\n (error)=>{\n //error\n console.log(error);\n this._snack.open('Something went Wrong', 'OK', {\n duration: 2000,\n verticalPosition: 'top',\n horizontalPosition: 'center',\n panelClass: ['red-snackbar','login-snackbar'],\n });\n }\n);\nRun Code Online (Sandbox Code Playgroud)\n我不知道到底发生了什么,但订阅显示已弃用,以下是消息
\n(method) Observable<Object>.subscribe(next?: ((value: Object) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription (+2 overloads)\n@deprecated \xe2\x80\x94 Instead of passing separate callback arguments, use an observer …Run Code Online (Sandbox Code Playgroud) 我有一个要求,需要我的字符串在每个逗号后允许有空格。我的数组看起来像这样 ['yes', 'no']。将其转换为字符串时,它的顺序变为 yes,no,但我希望输出像 yes, no 一样,每个逗号后面都允许有空格。
let array=['yes', 'no'];
let result = array.toString().substring(0, (array.toString()).length).split(",");
console.log(result);
Run Code Online (Sandbox Code Playgroud)
答案就像是,不,但我需要它是,不。我可以通过什么方式得到这个输出结果。
我正在从头开始创建一个独立的应用程序,我没有 ngModule。
我正在创建一个翻译服务和一个使用它的管道。
在 app.component.ts 上测试它
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { I18nPipe } from './pipes/i18n.pipe';
import { I18nService } from './services/i18n.service';
@Component({
selector: 'ct-v4',
standalone: true,
imports: [CommonModule, RouterOutlet, I18nPipe],
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'ct-v4';
constructor(private i18nService: I18nService) {}
ngOnInit(): void {
this.i18nService.initLanguage('en');
}
}
Run Code Online (Sandbox Code Playgroud)
我的国际化服务:
import { Injectable } from '@angular/core';
import { HttpClient …Run Code Online (Sandbox Code Playgroud) angular ×5
arrays ×1
chart.js ×1
chart.js2 ×1
javascript ×1
ng2-charts ×1
rxjs7 ×1
string ×1
typescript ×1
unit-testing ×1