简体   繁体   English

如何在 Angular 中正确配置路由

[英]How to configure routes in Angular properly

I'm learning Routes in Angular.我正在学习 Angular 中的路由。 I'm following some tutorials from Pluralsight.我正在学习 Pluralsight 的一些教程。 Here is the stackblitz for the same.这是相同的堆栈闪电战 I tried a very basic code to understand routing.我尝试了一个非常基本的代码来理解路由。 But I'm getting this error:但我收到此错误:

"Unexpected value 'PostList' declared by the module 'PostModule'. Please add a @Pipe/@Directive/@Component annotation." “模块‘PostModule’声明的意外值‘PostList’。请添加@Pipe/@Directive/@Component 注释。”

I debugged the code, and here's the culprit:我调试了代码,这是罪魁祸首:

post-list.component.ts post-list.component.ts

import { PostService } from './post.service';

export class PostList {

  constrcutor(private postservice: PostService) {
    postservice.getAllPosts();
  }
}

I'm trying to read posts which are already stored somewhere (I'm using JSONPlaceholder).我正在尝试阅读已经存储在某处的帖子(我正在使用 JSONPlaceholder)。 I want to show all the posts on one page and then on click event I want to navigate into the details of that particular post.我想在一页上显示所有帖子,然后在单击事件时我想导航到该特定帖子的详细信息。 Here is my:这是我的:

post.service.ts post.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class PostService {
  constructor() {}

  getAllPosts() {
    fetch('https://jsonplaceholder.typicode.com/posts/1')
      .then(response => response.json())
      .then(json => console.log(json))
  }
}

and here's my: post.module.ts这是我的: post.module.ts

import { NgModule } from '@angular/core';
import { PostDetails } from './post-details.component';
import { PostList } from './post-list.component';
import { RouterModule } from '@angular/router';

@NgModule({
  imports:[
    RouterModule.forChild([
      {path: 'posts', component: PostList},
      {path: 'posts/:id', component: PostDetails}
    ])
  ],
  declarations:[PostList, PostDetails]
})
export class PostModule {}

My problem is Routing.我的问题是路由。 I'm not able to set routes correctly.我无法正确设置路线。 I've created a stackblitz for the same.为此创建了一个stackblitz Please point out my mistakes.请指出我的错误。 I really want to learn Angular with all best practices.我真的很想通过所有最佳实践来学习 Angular。 Please correct me.请纠正我。

you have to add @Component to your class你必须将@Component添加到你的类中

import { PostService } from './post.service';

// added this below ↓
@Component({
  selector: 'whatever',
  templateUrl: './my-template.template.html',
})
export class PostList {

  constrcutor(private postservice: PostService) {
    postservice.getAllPosts();
  }
}

The problem is that since your class is not being passed that component decorator, adding it to declarations seems like you added the wrong thing to angular, since it only expects components in there (which have @component)问题是,由于您的类没有被传递给组件装饰器,因此将它添加到declarations似乎您向 angular 添加了错误的内容,因为它只需要组件(具有 @component)

edit: checked your stackblitz and fixed the issue but then you get an error with the PostDetails declaration, for the same reasons.编辑:检查了您的 stackblitz 并修复了问题,但是由于相同的原因,您在 PostDetails 声明中遇到了错误。 Please find the Component's options and learn how to use it properly, you need to add the @Component decorator to all instances, and you have to add a selector and template to it (selector is how is it going to be targeted from the html, and template is what the component html is), you can have templateUrl to target another file with the html, you can add styles or styleUrls, etc....请找到组件的选项并学习如何正确使用它,您需要将@Component 装饰器添加到所有实例,并且您必须为其添加选择器和模板(选择器是如何从 html 中定位的,模板是组件 html 的内容),您可以使用 templateUrl 将 html 定位到另一个文件,您可以添加样式或 styleUrls 等....

hope this helps, feel free to ask anything further希望这会有所帮助,请随时提出任何进一步的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM