错误 TS2339:“主页”类型上不存在属性“路由器”
Posted
技术标签:
【中文标题】错误 TS2339:“主页”类型上不存在属性“路由器”【英文标题】:Error TS2339: Property 'router' does not exist on type 'HomePage' 【发布时间】:2019-10-23 00:52:43 【问题描述】:我经常收到以下错误:
[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
中的这个sn-p,我收到了上面提到的错误
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 上找不到任何东西对我有用。有人知道吗?
【问题讨论】:
需要在构造函数中注入路由器 【参考方案1】:在构造函数中注入路由器
constructor(
public loadingController: LoadingController,
private wordpressService: WordPressRestapiService,
private router: Router)
【讨论】:
啊,这么简单?!非常感谢!关于Angular和Ionic的新手,现在很明显:)【参考方案2】:在构造函数中声明路由器变量。
constructor( private router: Router )
【讨论】:
以上是关于错误 TS2339:“主页”类型上不存在属性“路由器”的主要内容,如果未能解决你的问题,请参考以下文章
错误 TS2339:类型“”上不存在属性“contenido”
TypeScript:TS2339 错误——“对象”类型上不存在属性