错误 TS2339:属性 'router' 在类型 'HomePage' 上不存在
Error TS2339: Property 'router' does not exist on type 'HomePage'
我经常收到以下错误:
[ng] ERROR in src/app/home/home.page.ts(34,10): error TS2339: Property 'router' does not exist on type 'HomePage'.
我正在尝试将 Wordpress 与 Ionic 4 结合使用,到目前为止,我设法从我的网站上获取了最近的帖子。 现在我想让它们可点击并导航到它们,但是由于我 home.page.ts
中的这个片段,我收到了上述错误
openPost(postId) {
this.router.navigateByUrl('/post/' + postId);
}
整页:
import { Router } from '@angular/router';
import { Component } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { WordPressRestapiService, Post } from '../services/wordpress-restapi.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
categoryId: number;
private posts : Post[] = [];
constructor(
public loadingController: LoadingController,
private wordpressService: WordPressRestapiService) { }
async ngOnInit() {
const loading = await this.loadingController.create();
await loading.present();
this.loadPosts().subscribe((posts: Post[]) => {
this.posts = posts
loading.dismiss();
});
}
loadPosts() {
return this.wordpressService.getRecentPosts(this.categoryId);
}
openPost(postId) {
this.router.navigateByUrl('/post/' + postId);
}
}
我也试过删除 "this",但我在 SO 上找不到任何对我有用的东西。有人知道吗?
在构造函数中注入路由器
constructor(
public loadingController: LoadingController,
private wordpressService: WordPressRestapiService,
private router: Router) { }
在构造函数中声明路由器变量。
constructor( private router: Router ){
}
我经常收到以下错误:
[ng] ERROR in src/app/home/home.page.ts(34,10): error TS2339: Property 'router' does not exist on type 'HomePage'.
我正在尝试将 Wordpress 与 Ionic 4 结合使用,到目前为止,我设法从我的网站上获取了最近的帖子。 现在我想让它们可点击并导航到它们,但是由于我 home.page.ts
openPost(postId) {
this.router.navigateByUrl('/post/' + postId);
}
整页:
import { Router } from '@angular/router';
import { Component } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { WordPressRestapiService, Post } from '../services/wordpress-restapi.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
categoryId: number;
private posts : Post[] = [];
constructor(
public loadingController: LoadingController,
private wordpressService: WordPressRestapiService) { }
async ngOnInit() {
const loading = await this.loadingController.create();
await loading.present();
this.loadPosts().subscribe((posts: Post[]) => {
this.posts = posts
loading.dismiss();
});
}
loadPosts() {
return this.wordpressService.getRecentPosts(this.categoryId);
}
openPost(postId) {
this.router.navigateByUrl('/post/' + postId);
}
}
我也试过删除 "this",但我在 SO 上找不到任何对我有用的东西。有人知道吗?
在构造函数中注入路由器
constructor(
public loadingController: LoadingController,
private wordpressService: WordPressRestapiService,
private router: Router) { }
在构造函数中声明路由器变量。
constructor( private router: Router ){
}