简体   繁体   English

SharePoint React pnpjs

[英]SharePoint React pnpjs

I am working on a react project for SharePoint framework. 我正在为SharePoint框架进行React项目。
My question is a more or less general question. 我的问题多少有些笼统。

I came across a certain problem which I don't understand: 我遇到了一个我不明白的问题:
I tried to use sp.search with an object matching the SearchQuery interface, but did this in a class extending react.component 我试图将sp.search与匹配SearchQuery接口的对象一起使用,但是在扩展react.component的类中这样做

Code: 码:

public search(query: string, properties: Array<string>, rowLimit: number): Promise<SearchResults> {
    return new Promise<SearchResults>(async (resolve) => {
        let result = await sp.search(<SearchQuery>{
            Querytext: query,
            SelectProperties: properties,
            RowLimit: rowLimit
        });
        resolve(result);
    });
}

As you can see, this is just a basic search function. 如您所见,这只是基本的搜索功能。 If I use sp.search in a class that does not extend react.component or if I don't use the searchquery instance, but a plain querystring everything works fine. 如果我在不扩展react.component的类中使用sp.search,或者如果我不使用searchquery实例,但使用普通的查询字符串,那么一切正常。

I seem to be a type error, but honestly, I don't get what's happening. 我似乎是类型错误,但老实说,我不知道发生了什么。
Error: 错误:

[ts]Argument of type 'Element' is not assignable to parameter of type 'SearchQueryInit'. Type 'Element' is not assignable to type 'ISearchQueryBuilder'.
Property 'query' is missing in type 'Element'. [2345]

[ts] JSX element 'SearchQuery' has no corresponding closing tag. [17008]
[ts] 'SearchQuery' only refers to a type, but is being used as a value here. [2693]

So I decided to just write a service that's using pnpjs and that's totally fine, especially since I didn't plan to use pnpjs in a component, that was just to be a bit faster and have something to test and to work with. 因此,我决定只写一个使用pnpjs的服务,这是完全正常的,特别是因为我不打算在组件中使用pnpjs,所以这只是要快一点,并要进行一些测试和使用。 but nevertheless, I really would like to understand what is going on. 但是,尽管如此,我真的很想了解发生了什么。 Btw I'm using TypeScript, if that's of interest. 顺便说一句,如果您感兴趣的话,我正在使用TypeScript。

Apparently those errors occur due to a limitation cased by the use of <T> for generic type parameter declarations combined with JSX grammar, refer this issue for a more details (which is marked as a Design Limitation ) 显然,这些错误是由于将<T>用于与JSX语法相结合的泛型类型参数声明而引起的限制而发生的,请参阅此问题以获取更多详细信息(标记为Design Limitation )。

To circumvent this error, create the instance of SearchQuery : 为了避免此错误,请创建SearchQuery的实例:

const searchQuery: SearchQuery = {
    Querytext: query,
    SelectProperties: properties,
    RowLimit: rowLimit
};

and then pass it into sp.search method: 然后将其传递给sp.search方法:

let result = await sp.search(searchQuery);

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

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