简体   繁体   English

Angular2从Wordpress WP API获取页面数据

[英]Angular2 get page data from Wordpress WP API

I'm using Wordpress WP API to parse data into my Angular2 app. 我正在使用Wordpress WP API将数据解析到我的Angular2应用程序中。

I need to get data to populate my pages and I need to get the page SLUG based on the ActivatedRoute. 我需要获取数据以填充页面,并且需要基于ActivatedRoute获取页面SLUG。 However, I'm not sure how to achieve this. 但是,我不确定如何实现这一目标。 In the "projects" page I need to have page data. 在“项目”页面中,我需要页面数据。

This is an example Json of my data for a page: 这是我页面数据的示例Json:

[{
  "id":16,
  "date":"2015-06-24T11:44:03",
  "slug":"projects",
  "type":"page",
  "title":{"rendered":"Projects"}
}]

First, I created a Page Service: 首先,我创建了一个页面服务:

@Injectable()
export class PageService {

constructor(private http: Http) {}

getPage(slug): Observable<Page>  {

    return this.http
        .get(`${PROJECT_API}pages?slug=${slug}`)
        .map((res: Response) => res.json());
}

} }

Then, in my app.component I try to get the activated route parameters, parse the SLUG into the getPage function from my Service: 然后,在我的app.component中,我尝试获取激活的路由参数,将SLUG从我的Service解析为getPage函数:

export class AppComponent implements OnInit {

   page: Page[]; // Interface of Page

   constructor(
       private router: Router,
       private activatedRoute: ActivatedRoute,
       private pageService: PageService
   ) {}

   ngOnInit() {
        this.activatedRoute.params
          .switchMap((data: Page) =>   this.pageService.getPage(data.slug))
          .subscribe((data: Page) => {
            console.log(data[0])
            this.page = data[0]
          });

   }

}

In my console I get "undefined". 在我的控制台中,我得到“未定义”。

I want for each page to dynamically get the page SLUG from the ActivatedRoute and fetch the data to populate each page. 我希望每个页面都从ActivatedRoute动态获取页面SLUG并获取数据以填充每个页面。

For each page (component template), I can then simply parse: 然后,对于每个页面(组件模板),我可以简单地解析:

<h1>{{ page?.title.rendered }}</h1>

What am I missing here? 我在这里想念什么?

I am working on a plugin, Xo for Angular, that sounds like the perfect solution to this situation. 我正在开发一个Xo for Angular插件,听起来像是针对这种情况的完美解决方案。

https://wordpress.org/plugins/xo-for-angular/ https://wordpress.org/plugins/xo-for-angular/

Main concepts: 主要概念:

  • Your Angular App is loaded and exists as a theme within WordPress. 您的Angular应用已加载,并作为主题存在于WordPress中。
  • Angular templates (modules) which can be selected for pages and posts. 可以为页面和帖子选择的角度模板(模块)。
  • Dynamic routing of pages, posts, and other content created in WordPress. 在WordPress中创建的页面,帖子和其他内容的动态路由。

Using the Xo API it is easy to get the data for any page or post within your WordPress. 使用Xo API,可以轻松获取WordPress中任何页面或帖子的数据。 For instance: https://angularxo.io/xo-api/posts/get?url=/ 例如: https : //angularxo.io/xo-api/posts/get?url=/

This retrieves the full post object with additions such as meta and ACF fields if configured. 这将检索带有完整内容的完整发布对象,例如meta和ACF字段(如果已配置)。

Xo also generates all the routes dynamically, this lets you define the page structures as normal through WordPress. Xo还动态生成所有路由,这使您可以通过WordPress正常定义页面结构。 There is no need to retrieve a page or post by its slug since both WordPress and Angular as Xo keeps them in complete synchronization. 由于WordPress和Angular都使Xo保持它们完全同步,因此无需按其子标题检索页面或帖子。

Example dynamic routes: https://angularxo.io/xo-api/routes/get 动态路由示例: https : //angularxo.io/xo-api/routes/get

There is also a node module angular-xo that gives a built in resolver to retrieve this data for each page view and make that data available in your template. 还有一个节点模块angular-xo ,它提供了一个内置的解析器,用于为每个页面视图检索此数据并使该数据在模板中可用。

More about this feature: https://angularxo.io/guides/routing 有关此功能的更多信息: https : //angularxo.io/guides/routing

Example theme which uses Xo: https://github.com/WarriorRocker/angular-xo-material 使用Xo的示例主题: https : //github.com/WarriorRocker/angular-xo-material

More docs (work in progress): https://angularxo.io/ 更多文档(正在进行中): https : //angularxo.io/

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

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