【发布时间】:2020-01-29 02:38:15
【问题描述】:
我正在尝试创建一个 spfx 反应组件,它将向浏览器显示 rss 提要。我有这个在操场上工作,但 spfx 使用打字稿,不知道如何解决下面的类型错误。
RssFeed.ts
import * as React from 'react';
import styles from './RssFeed.module.scss';
import { IRssFeedProps } from './IRssFeedProps';
import { escape } from '@microsoft/sp-lodash-subset';
import * as $ from "jquery";
import { string } from 'prop-types';
export default class RssFeed extends React.Component<IRssFeedProps,
{}> {
constructor(props) {
super(props);
this.state = { news: [] };
}
componentDidMount() {
this.getNews();
}
getNews() {
$.get(
"https://www.feed.co.uk/news/feed",
function(data) {
var $xml = $(data);
var items = [];
$xml.find("item").each(function() {
var $this = $(this);
items.push({
title: $this.find("title").text(),
link: $this.find("link").text()
// link: $this.find("link").text(),
});
});
this.setState({ news: items }, function() {
// callback function to check what items going onto the array
console.log(this.state.news);
});
}.bind(this),
"xml"
);
}
public render(): React.ReactElement<IRssFeedProps> {
return (
<div className={ styles.rssFeed }>
{this.state.news.map(item => (
<div className="panel" key={item.title}>
<h2 className="panel-title">
{item.title}
</h2>
<span>{item.link}</span>
</div>
))}
</div>
);
}
}
IRssFeedProps.ts
export interface IRssFeedProps {
description: string;
}
这是错误: 错误 - [tsc] src/webparts/rssFeed/components/RssFeed.tsx(47,25):错误 TS2339:“Readonly”类型上不存在属性“news”。
【问题讨论】:
标签: reactjs typescript spfx