简体   繁体   English

如何在Meteor + React中从集合字段渲染文本预览?

[英]How to render a text preview from a collection field in Meteor + React?

I'm trying to show a text preview for a list of posts. 我正在尝试显示帖子列表的文本预览。 I have a list of posts that are rendered in the home blog page, each post only shows title, author and an anchor tag as a link to a view that shows a single post with all the data that contains the selected post. 我有一个主页博客页面中呈现的帖子列表,每个帖子仅显示标题,作者和锚标记,作为指向视图的链接,该视图显示包含包含所选帖子的所有数据的单个帖子。 Now in the home blog page I need to show a text preview of the post in addition to title and author but I don't have any idea of how to grab a part of the post content and show it as a preview. 现在在主页博客页面中,除了标题和作者之外,我需要显示帖子的文本预览,但是我不知道如何获取帖子内容的一部分并将其显示为预览。

I'm a beginner with Meteor and React. 我是Meteor和React的初学者。 If someone could give me some advise, it would be great. 如果有人可以给我一些建议,那就太好了。 Here is the code: 这是代码:

Collections.jsx Collections.jsx

export const Posts = new Mongo.Collection('Posts');
export const PostSchema = Astro.Class({
name: 'PostSchema',
collection: Posts,
fields: {
  title: {
    validator: Validators.and([
       // more validators...
      ]),
     },
  content: { // I need to show a preview of this content
    validator : Validators.and([
      Validators.required(),
      Validators.string(),
      Validators.minLength(3)
    ])
  },
  author: {
    validator: Validators.and([
    // more validators
    ])
  },
  category: {
    validator: Validators.and([
     // more validators
    ])
  },
  tags: {
    validator: Validators.and([
    // more validators
    ])
  },
 }
});

Here is where I show the posts list. 这是我显示帖子列表的地方。

Blog.jsx Blog.jsx

// other imports
import { Posts } from '../../lib/collections.jsx';

class Blog extends Component {
  render() {
   let content = (
   <div>
    <h1>Posts</h1>
    {this.props.posts.map((post, idx) => (
      <Post key={idx} post={post} />
    ))}
   </div>);

    if (!this.props.ready) {
      content = <p>LOADING...</p>;
   }
   return content;
 }
}
Blog.propTypes = {
 ready: React.PropTypes.bool.isRequired,
 posts: React.PropTypes.array.isRequired,
};

export default createContainer((params) => {
const handle = Meteor.subscribe('posts');
let posts = Posts.find({}, { sort: { createdAt: -1 } }).fetch();
if (params.category) {
  posts = Posts.find({ category: params.category },
        { sort: {   createdAt: -1 } }).fetch();
}
return {
  ready: handle.ready(),
  posts,
 };
}, Blog);

 /**
 * POST 
 * Here is where I don't know how to show the text preview
 */
const Post = (props) => {
 const {
  title,
  author,
  slug,
  category,
  content,
 } = props.post;
return (
   <div>
    <article className="new-resolution">
      <p>Title:{title}</p>
      <p>Author:{author}</p>
      <p>{content}</p>
      <a href={`/blog/kategorie/${category}/${slug}`}>
        Weiter lesen...
      </a>
     </article>
   </div>
  );
};
Post.propTypes = {
post: PropTypes.object.isRequired,
};

Since I din't have to much time to resolve this in a much fancy way, I just decide to add a new field in my collection named "preview". 由于我没有太多时间以花哨的方式解决此问题,因此我决定在集合中添加一个名为“ preview”的新字段。 In the form post I added a new text area with a limit of characters and this would be displayed in the Post.jsx <p>Preview:{preview}</p> . 在表单发布中,我添加了一个新的文本区域,该区域包含字符数,该区域将显示在Post.jsx <p>Preview:{preview}</p>

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

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