![](/img/trans.png)
[英]If i use index inside my map the Reactjs SPFx web part will not return anything.. any advice?
[英]Modify my SPFx web part to reference css file instead of writing the css code inside the web part
我已經從 GitHub @ https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-enhanced-list-formatting構建了一個類似於此 Web 部件的 Web 部件。 Web 部件允許在 SharePoint 新式頁面中嵌入自定義 CSS 代碼。 但是是否可以修改 Web 部件以引用 CSS 文件,而不必在 Web 部件中編寫實際的 CSS 代碼? 因此,不要按如下方式編寫 CSS 代碼:-
引用 css 文件如下:-
這是EnhancedListFormattingWebPart.ts
:-
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
IPropertyPaneConfiguration,
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import * as strings from 'EnhancedListFormattingWebPartStrings';
import EnhancedListFormatting from './components/EnhancedListFormatting';
import { IEnhancedListFormattingProps } from './components/IEnhancedListFormattingProps';
import { PropertyPaneWebPartInformation } from '@pnp/spfx-property-controls/lib/PropertyPaneWebPartInformation';
import { PropertyFieldMonacoEditor } from '../../controls/PropertyFieldMonacoEditor';
export interface IEnhancedListFormattingWebPartProps {
css: string;
acceptedDisclaimer?: boolean;
}
export default class EnhancedListFormattingWebPart extends BaseClientSideWebPart <IEnhancedListFormattingWebPartProps> {
public render(): void {
const element: React.ReactElement<IEnhancedListFormattingProps> = React.createElement(
EnhancedListFormatting,
{
css: this.properties.css,
acceptedDisclaimer: this.properties.acceptedDisclaimer,
displayMode: this.displayMode,
context: this.context,
onAcceptDisclaimer: ()=>this._handleAcceptDisclaimer()
}
);
ReactDom.render(element, this.domElement);
}
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
// protected get dataVersion(): Version {
// return Version.parse('1.0');
// }
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyFieldMonacoEditor('css', {
label: strings.CSSFieldLabel,
key: "cssText",
value: this.properties.css,
defaultValue: this.properties.css,
language: "css",
theme: "vs-light",
showLineNumbers: false,
onPropertyChange: (_propertyPath: string, _oldValue: string, value: string) => this._handleSave(value),
}),
PropertyPaneWebPartInformation({
description: strings.CSSDisclaimer,
key: 'cssDisclaimer'
})
]
}
]
}
]
};
}
private _handleAcceptDisclaimer = () => {
this.properties.acceptedDisclaimer = true;
this.render();
}
private _handleSave = (value: string) => {
this.properties.css = value;
}
}
這是EnhancedListFormatting.tsx
:-
import * as React from 'react';
import styles from './EnhancedListFormatting.module.scss';
import * as strings from 'EnhancedListFormattingWebPartStrings';
import { IEnhancedListFormattingProps } from './IEnhancedListFormattingProps';
import { MessageBarButton, MessageBar, MessageBarType } from 'office-ui-fabric-react';
import { DisplayMode } from '@microsoft/sp-core-library';
export default class EnhancedListFormatting extends React.Component<IEnhancedListFormattingProps, {}> {
public render(): React.ReactElement<IEnhancedListFormattingProps> {
const { css, displayMode, acceptedDisclaimer } = this.props;
// If we accepted the disclaimer, let's work as expected
// Determine if there is a CSS value
const hasCSS: boolean = css !== undefined && css !== "";
// Create a style element
const styleElem: JSX.Element = <style type="text/css">{css}</style>;
// If we're not in Edit mode, hide this web part
if (displayMode !== DisplayMode.Edit) {
return styleElem;
}
// if we didn't accept the disclaimer, show a disclaimer and nothing else
if (acceptedDisclaimer !== true) {
return (<MessageBar
onDismiss={()=>this._onAcceptDisclaimer()}
dismissButtonAriaLabel={strings.DismissDisclaimerAriaLabel}
messageBarType={MessageBarType.warning}
actions={
<div>
<MessageBarButton onClick={()=>this._onAcceptDisclaimer()}>{strings.AcceptDisclaimerButton}</MessageBarButton>
</div>
}
>
<div className={styles.disclaimerText} dangerouslySetInnerHTML={{__html: strings.DisclaimerText}}></div>
</MessageBar>);
}
return (
<div className={styles.enhancedListFormatting}>
{styleElem}
<MessageBar
messageBarType={hasCSS ? MessageBarType.success : null}
isMultiline={false}
actions={
<div>
<MessageBarButton onClick={() => this._onConfigure()}>{hasCSS ? strings.PlaceholderButtonTitleHasStyles : strings.PlaceholderButtonTitleNoStyles}</MessageBarButton>
</div>
}
>
{hasCSS ? strings.PlaceholderDescriptionHasStyles : strings.PlaceholderDescriptionNoStyles}
</MessageBar>
</div>
);
}
private _onAcceptDisclaimer() {
this.props.onAcceptDisclaimer();
}
private _onConfigure() {
this.props.context.propertyPane.open();
}
}
任何想法,我怎么能做到這一點? 我想這樣做,因為我們計划創建很多具有自定義 css 的頁面。 並且以后不必修改所有這些頁面來更改其 css,我們只需要更改相關的 css 文件,更改將立即自動應用於所有頁面。
謝謝
SharePoint 中內置了對主題的支持。 它包括自定義 CSS 支持、自定義顏色模式支持等。您可以從這里重新開始:
https://docs.microsoft.com/en-us/sharepoint/dev/general-development/themes-overview-for-sharepoint
現代 Web 部件和移除“腳本編輯器”的想法是禁止用戶添加指向某些隨機來源的鏈接,因此只允許安全的批准內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.