如何在 tsx 文件中的渲染函数中从外部文件获取数据
How to get data from external file in render function in tsx file
我正在使用 visual studio 代码和 React 以及 Typesctipt 为 SharePoint Online 开发 ApplicationCustomizer SPFX 扩展。我正在尝试在 header 部分生成命令栏结构 ui 组件以呈现结构 ui 下拉菜单。我已根据在线提供的入门示例设法使它正常工作。
我需要从存储在 SharePoint 上的 SiteAssets 文件夹中的外部文本文件中读取菜单项字符串,该文件采用 required 菜单项格式。有人可以 gui 告诉我将 getItems() 函数中的代码更新为 return 来自外部文件的文本的正确方法吗,下面是我的 tsx 代码文件:
import * as React from "react";
import { Link } from 'office-ui-fabric-react/lib/Link';
import { CommandBar } from 'office-ui-fabric-react/lib/CommandBar';
import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } from '@microsoft/sp-http';
export interface IReactHeaderProps { }
export default class ReactHeader extends React.Component<IReactHeaderProps> {
constructor(props: IReactHeaderProps) {
super(props);
}
public render(): JSX.Element {
return (
<div className={"ms-bgColor-themeDark ms-fontColor-white"}>
<CommandBar items={this.getItems()} />
</div>
);
}
// Data for CommandBar
private getItems = () => {
return [
**
{
key: 'menu1',
name: 'Main Menu 1',
cacheKey: 'myCacheKey',
iconProps: { iconName: 'ChevronDown' },
href: '#' ,
subMenuProps: {
items: [
{
key: 'submenu1',
name: 'Option 1',
href: '#',
},
{
key: 'submenu2',
name: 'Option 2',
href: '#',
},
],
},
},
{
key: 'menu2',
name: 'Main Menu 2',
cacheKey: 'myCacheKey',
iconProps: { iconName: 'ChevronDown' },
href: '#',
subMenuProps: {
items: [
{
key: 'submenu3.1',
name: 'Option 1',
href: '#',
subMenuProps: {
items: [
{
key: 'submenu3.2',
name: 'Option 1.1',
href: '#',
},
{
key: 'submenu4.2',
name: 'Option 1.2',
href: '#',
},
],
},
},
{
key: 'submenu4',
name: 'Option 2',
href: '#',
},
],
},
},
];
}
}
@Osden Pereira,
请参考以下代码:
import * as React from 'react';
import styles from './Externalfile.module.scss';
import { IExternalfileProps } from './IExternalfileProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { CommandBar } from 'office-ui-fabric-react/lib/CommandBar';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { IExternalfileState } from './IExternalfileState';
export default class Externalfile extends React.Component<IExternalfileProps, IExternalfileState> {
constructor(props: IExternalfileProps) {
super(props);
this.state = {
Items: []
};
}
public render(): React.ReactElement<IExternalfileProps> {
return (
<div className={styles.externalfile}>
<div className={"ms-bgColor-themeDark ms-fontColor-white"}>
<CommandBar items={this.state.Items} />
</div>
</div>
);
}
public componentDidMount() {
this.getItems01();
}
// Data for CommandBar
private getItems01() {
let url = this.props.ctx.pageContext.site.serverRelativeUrl + "/SiteAssets/testjson.txt";
this.props.ctx.spHttpClient.get(url, SPHttpClient.configurations.v1).then(res => {
return res.json();
}).then(e => {
this.setState({
Items: e
});
});
}
}
IExternalfileState.ts:
export interface IExternalfileState {
Items: [];
}
Json 文件:
[
{
"key":"menu1",
"name":"Main Menu 1",
"cacheKey":"myCacheKey",
"iconProps":{
"iconName":"ChevronDown"
},
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu1",
"name":"Option 1",
"href":"#"
},
{
"key":"submenu2",
"name":"Option 2",
"href":"#"
}
]
}
},
{
"key":"menu2",
"name":"Main Menu 2",
"cacheKey":"myCacheKey",
"iconProps":{
"iconName":"ChevronDown"
},
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu3.1",
"name":"Option 1",
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu3.2",
"name":"Option 1.1",
"href":"#"
},
{
"key":"submenu4.2",
"name":"Option 1.2",
"href":"#"
}
]
}
},
{
"key":"submenu4",
"name":"Option 2",
"href":"#"
}
]
}
}
]
BR
我正在使用 visual studio 代码和 React 以及 Typesctipt 为 SharePoint Online 开发 ApplicationCustomizer SPFX 扩展。我正在尝试在 header 部分生成命令栏结构 ui 组件以呈现结构 ui 下拉菜单。我已根据在线提供的入门示例设法使它正常工作。
我需要从存储在 SharePoint 上的 SiteAssets 文件夹中的外部文本文件中读取菜单项字符串,该文件采用 required 菜单项格式。有人可以 gui 告诉我将 getItems() 函数中的代码更新为 return 来自外部文件的文本的正确方法吗,下面是我的 tsx 代码文件:
import * as React from "react";
import { Link } from 'office-ui-fabric-react/lib/Link';
import { CommandBar } from 'office-ui-fabric-react/lib/CommandBar';
import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } from '@microsoft/sp-http';
export interface IReactHeaderProps { }
export default class ReactHeader extends React.Component<IReactHeaderProps> {
constructor(props: IReactHeaderProps) {
super(props);
}
public render(): JSX.Element {
return (
<div className={"ms-bgColor-themeDark ms-fontColor-white"}>
<CommandBar items={this.getItems()} />
</div>
);
}
// Data for CommandBar
private getItems = () => {
return [
**
{
key: 'menu1',
name: 'Main Menu 1',
cacheKey: 'myCacheKey',
iconProps: { iconName: 'ChevronDown' },
href: '#' ,
subMenuProps: {
items: [
{
key: 'submenu1',
name: 'Option 1',
href: '#',
},
{
key: 'submenu2',
name: 'Option 2',
href: '#',
},
],
},
},
{
key: 'menu2',
name: 'Main Menu 2',
cacheKey: 'myCacheKey',
iconProps: { iconName: 'ChevronDown' },
href: '#',
subMenuProps: {
items: [
{
key: 'submenu3.1',
name: 'Option 1',
href: '#',
subMenuProps: {
items: [
{
key: 'submenu3.2',
name: 'Option 1.1',
href: '#',
},
{
key: 'submenu4.2',
name: 'Option 1.2',
href: '#',
},
],
},
},
{
key: 'submenu4',
name: 'Option 2',
href: '#',
},
],
},
},
];
}
}
@Osden Pereira,
请参考以下代码:
import * as React from 'react';
import styles from './Externalfile.module.scss';
import { IExternalfileProps } from './IExternalfileProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { CommandBar } from 'office-ui-fabric-react/lib/CommandBar';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { IExternalfileState } from './IExternalfileState';
export default class Externalfile extends React.Component<IExternalfileProps, IExternalfileState> {
constructor(props: IExternalfileProps) {
super(props);
this.state = {
Items: []
};
}
public render(): React.ReactElement<IExternalfileProps> {
return (
<div className={styles.externalfile}>
<div className={"ms-bgColor-themeDark ms-fontColor-white"}>
<CommandBar items={this.state.Items} />
</div>
</div>
);
}
public componentDidMount() {
this.getItems01();
}
// Data for CommandBar
private getItems01() {
let url = this.props.ctx.pageContext.site.serverRelativeUrl + "/SiteAssets/testjson.txt";
this.props.ctx.spHttpClient.get(url, SPHttpClient.configurations.v1).then(res => {
return res.json();
}).then(e => {
this.setState({
Items: e
});
});
}
}
IExternalfileState.ts:
export interface IExternalfileState {
Items: [];
}
Json 文件:
[
{
"key":"menu1",
"name":"Main Menu 1",
"cacheKey":"myCacheKey",
"iconProps":{
"iconName":"ChevronDown"
},
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu1",
"name":"Option 1",
"href":"#"
},
{
"key":"submenu2",
"name":"Option 2",
"href":"#"
}
]
}
},
{
"key":"menu2",
"name":"Main Menu 2",
"cacheKey":"myCacheKey",
"iconProps":{
"iconName":"ChevronDown"
},
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu3.1",
"name":"Option 1",
"href":"#",
"subMenuProps":{
"items":[
{
"key":"submenu3.2",
"name":"Option 1.1",
"href":"#"
},
{
"key":"submenu4.2",
"name":"Option 1.2",
"href":"#"
}
]
}
},
{
"key":"submenu4",
"name":"Option 2",
"href":"#"
}
]
}
}
]
BR