我正在 Angular v11.0.2 中做一个 Web 应用程序。我需要进行 HTTP 调用才能在屏幕上显示一些信息。为了节省时间并利用async管道的优势,我在模板中使用它,但是,它导致多个请求永远不会停止。
服务
export class MyDataService {
constructor(
private http: HttpClient,
) {
}
fetchMyEmployeeData(): Observable<any[]> {
return this.http.post<any[]>('ENDPOINT', {});
}
}
Run Code Online (Sandbox Code Playgroud)
成分
export class MyAwesomeComponent {
constructor(
public myDataService: MyDataService,
) {
}
}
Run Code Online (Sandbox Code Playgroud)
模板
<ng-container *ngIf="(myDataService.fetchMyEmployeeData() | async) as data">
</ng-container>
Run Code Online (Sandbox Code Playgroud)
这会导致多个请求并且永远不会停止。
如果我使用也会发生同样的情况*ngFor:
<tr *ngFor="let d of (myDataService.fetchMyEmployeeData() | async)">
<td>.</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下操作:
使用shareReplay运算符:
fetchMyEmployeeData(): Observable<any[]> {
return this.http.post<any[]>('ENDPOINT', {}).pipe(shareReplay());
}
Run Code Online (Sandbox Code Playgroud)
使用一个简单的 div:
<div *ngIf="(myDataService.fetchMyEmployeeData() | …Run Code Online (Sandbox Code Playgroud) 我正在使用NestJS和 TypeScript 结合Passport JWT的实现来做一个服务器端应用程序。
首先了解一些背景:
我的 JwtStrategy (这里没有问题):
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private userService: UserService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'hi',
});
}
async validate(payload: IJwtClaims): Promise<UserEntity> {
const { sub: id } = payload;
// Find the user's database record by its "id" and return it.
const user = await this.userService.findById(id);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
根据有关该validate()方法的文档:
Passport 将根据 validate() 方法的返回值构建一个用户对象,并将其作为属性附加到 Request 对象上。
由于这种行为,我可以 …
我正在使用带有 HATEOAS 和 Gradle 的 Spring Boot 2.2.0.M1。
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
Run Code Online (Sandbox Code Playgroud)
目前,ResourceIDE (IntelliJ IDEA 2018.3) 未找到,并ControllerLinkBuilder标记为deprecated。
package com.example.restfulwebservicegradle.user;
import static org.springframework.hateoas.server.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
import com.example.restfulwebservicegradle.User;
import com.example.restfulwebservicegradle.UserDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.server.mvc.ControllerLinkBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.validation.Valid;
import java.net.URI;
import java.util.List;
@RestController
public class UserResource {
@Autowired
private UserDaoService service;
@GetMapping("users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
User user = service.findOne(id);
if (user == null)
throw new UserNotFoundException("id-" + id);
// Resource …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Bull/Redis 实现 Nodemailer 来处理 NestJS 中的电子邮件类型的任务。
\n我有一个名为的共享模块EmailService,它将一个作业添加到我的队列中,为此,它需要注入Queuefrom \'bull\'。
\n\nNest 无法解析 EmailService 的依赖关系(?)。请确保索引 [0] 处的参数 BullQueue_mailqueue 在 SharedModule 上下文中可用。
\n
我的结构
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.module.ts\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n| | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nodemailer\n| | \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nodemailer.module.ts\n| | \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nodemailer.service.ts\n| \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 modules\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 modules that imports the SharedModule to send emails.\n| \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 shared\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 processors\n| | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 email.processor.ts\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 services\n| | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 email\n| \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 shared.module.ts\nRun Code Online (Sandbox Code Playgroud)\n应用程序模块
\n@Module({\n imports: [\n NodemailerModule,\n // Al …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Ionic 6 中的新ion-accordion来实现一个菜单。
文档说,当我们在槽中ion-icon使用时,会自动添加an 。他们提供了一个组件属性来更改图标名称,甚至使用自定义图标(不是 ),但文档没有提到如何更改默认图标的大小和颜色。ion-itemheaderion-icon
<ion-accordion-group>
<ion-accordion>
<ion-item slot="header">
<ion-label>
Home
</ion-label>
</ion-item>
</ion-accordion>
</ion-accordion-group>
Run Code Online (Sandbox Code Playgroud)
我努力了:
ion-accordion-group {
ion-accordion {
ion-item {
// Attempt 1
ion-icon {
// Note. The default icon has this class
&.ion-accordion-toggle-icon {
font-size: 128px !important;
color: red;
}
}
// Attempt 2
ion-icon[slot="end"] {
font-size: 128px;
color: red;
}
}
// Attempt 3
ion-item[slot="header"] {
ion-icon[slot="end"] {
font-size: 128px;
color: red;
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Boot 构建后端3.1.0-SNAPSHOT,它使用Spring Framework 6x.
拦截器:
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("preHandle");
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("afterCompletion");
}
}
Run Code Online (Sandbox Code Playgroud)
在之前的版本(Spring Boot 2)中,添加Interceptor的方式是:
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor( new MyInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
现在,添加此类配置类的正确方法是:
@Configuration
public class AppConfig {
// Not …Run Code Online (Sandbox Code Playgroud) 我正在 Angular 6 中开发一个 Web 应用程序,并且在一个页面中有一个新闻列表。我有一个名为“查看详细信息”的按钮,我想将用户带到另一个页面(组件)以显示所有信息。
// Object to pass
News {
title: string;
date: Date;
body: string;
}
// Routing
{path: 'news-details/:data', component: NewsDetailsComponent},
<ng-container *ngFor="let data of newsFromToday; index as i">
<button type="button" [routerLink]="['/news-details', data]">See details</button>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
在我想要接收数据的组件中:
constructor(private route: ActivatedRoute) { }
ngOnInit() {
const data = this.route.snapshot.paramMap.get('data');
console.log(data);
}
Run Code Online (Sandbox Code Playgroud)
找不到网页
我努力了:
<button type="button" routerLink="/news-details/{{JSON.stringify(data)}}">See details</button>
ngOnInit() {
const competitionData = JSON.parse(this.route.snapshot.paramMap.get('data'));
console.log(competitionData);
}
Run Code Online (Sandbox Code Playgroud)
读取 Object.eval 中未定义的属性“stringify”[作为 updateDirectives]
我的目标是接收变量中的对象以显示模板中的所有信息。
我在Angular 6中做了一个Web应用程序.我从Firebase接收了一组对象,我正在尝试向用户显示详细信息ngFor.我的对象数组是这样的:
export interface Competition {
createdAt: Date;
sessions: Session[];
}
export interface Session {
date: Date;
program: Program[];
events: Event[];
}
export interface Program {
name: string;
}
export interface Event {
name: string;
}
Run Code Online (Sandbox Code Playgroud)
在我正在做的模板内:
<ng-container *ngFor="let competition of competitions; index as i">
<h3>{{competition.name}}</h3>
{{competition.sessions[i].program.length}}
{{competition.sessions[i].events.length}}
</ng-container>
Run Code Online (Sandbox Code Playgroud)
读取未定义的属性"program",读取未定义的属性"events"
我试过了:
{{competition[i].sessions[i].program.length}}
{{competition.sessions[i].program.length}}
{{competition[i].sessions[i].program[i].length}}
Run Code Online (Sandbox Code Playgroud)
我的目标是显示的长度program和events.
我正在 Angular 6 中做一个 web 应用程序,我正在使用 Reactive Form。我有一个name字段,我想验证它是否唯一。我正在将其值与数组进行比较。
companies: Company[];
this.companyFormGroup = this.fb.group({
name: [null, Validators.required],
emailAddress: [null, Validators.compose([Validators.required, Validators.email])]
}, {validator: this.uniqueValidator});
uniqueValidator(control: AbstractControl) {
const name = control.get('name').value;
if (control.get('name').valid) {
const isNameUnique = this.companies.map(company => company.name).some(value => value === name);
if (!isNameUnique) {
console.log(isNameUnique);
control.get('name').setErrors({unavailable: true});
}
}
}
Run Code Online (Sandbox Code Playgroud)
无法读取在 push../src/app/modules/create-company/create-company.component.ts.CreateCompanyComponent.uniqueValidator (create-company.component.ts:79) at forms.js 处未定义的属性“公司”: 602 at Array.map () at _executeValidators (forms.js:602) at FormGroup.validator (forms.js:567)
我的目标是在用户输入的名称已经存在时显示错误,如果名称是唯一的,则删除错误。
我正在 Angular 6 中开发一个 Web 应用程序,并在模板中添加了一个选项卡窗格。我想动态生成href和id。
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">Session 1</a></li>
<ng-container *ngFor="let session of data?.sessions; index as i">
<li class="" *ngIf="i > 0">
<a href="#tab_{{i}}" data-toggle="tab" aria-expanded="false">Session {{i + 1}}</a>
</li>
</ng-container>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab_1">
Working good.
</div>
<!-- /.tab-pane -->
<ng-container *ngFor="let session of data?.sessions; index as i">
<div class="tab-pane" id="tab_{{i}}" *ngIf="i > 0">
Not working.
</div>
<!-- /.tab-pane -->
</ng-container>
</div>
<!-- /.tab-content …Run Code Online (Sandbox Code Playgroud)