gpt4 book ai didi

javascript - 如何从外部文件访问React组件

转载 作者:行者123 更新时间:2023-12-02 23:14:27 24 4
gpt4 key购买 nike

首先,对我的语法错误表示歉意。我的英语水平不好。

我创建了一个项目,您可以在其中创建自己的图层 map ,好吗?

我将 map 导出到 iframe 内,这样我就可以在 index.html 文件中加载一个或多个 map 。

我在项目中创建了一个 API,我想从我的 index.html 访问此 API。

如何访问它?

我已经了解到我可以通过以下方式访问:

哪个是更好的选择?

如何在我的index.html中声明它?

这是我的代码:

class APIService extends Component {

constructor(props) {
super(props);
this.logged = (this.props.userLogged && this.props.userLogged.logged) ? true : false;
this.interval = undefined;
}

componentWillUnmount() {
clearInterval(this.interval);
this.interval = undefined;
};

getUserLayers = () => {
if (!this.logged) return null;
const userlayers = this.props.layers.filter(l => l.name).map(l => {
return { id: l.id, name: l.name, order: l.order };
});
return userlayers;
};

getVisibilityLayerById = (id) => {
if (!this.logged) return null;
let index = this.props.layers.findIndex(l => l.id === id);
if (index === -1) return null;
return (this.props.layers[index] && this.props.layers[index].layout && this.props.layers[index].layout.visibility) ? this.props.layers[index].layout.visibility : null;
};

getVisibilityLayerByOrder = (order) => {
if (!this.logged) return null;
let index = this.props.layers.findIndex(l => l.order === order);
if (index === -1) return null;
return (this.props.layers[index] && this.props.layers[index].layout && this.props.layers[index].layout.visibility) ? this.props.layers[index].layout.visibility : null;
};

changeVisibilityLayerById = (id) => {
if (!this.logged) return null;
let index = this.props.layers.findIndex(l => l.id === id);
if (index === -1) return null;
let layer = this.props.layers[index];
this.props.changeLayerVisibility(layer);
};

changeVisibilityLayerByOrder = (order) => {
if (!this.logged) return null;
let index = this.props.layers.findIndex(l => l.order === order);
if (index === -1) return null;
let layer = this.props.layers[index];
this.props.changeLayerVisibility(layer);
};

changeLayerVisibilityWithIntervalById = (id, interval) => {
if (!this.logged) return null;
setInterval( () => {
this.changeVisibilityLayerById(id);
}, interval);
};

changeLayerVisibilityWithIntervalByOrder = (order, interval) => {
if (!this.logged) return null;
setInterval( () => {
this.changeVisibilityLayerByOrder(order);
}, interval);
};

getCenter = () => {
if (!this.logged) return null;
let properties = (this.props.properties !== null) ? Object.assign({}, this.props.properties) : null;
let hasCenter = (properties !== null && Object.keys(properties).filter(p => p === "center")) ? true : false;
let hasLatAndLong = (hasCenter) ? Object.keys(properties.center).filter(c => c === 'latitude' || c === 'longitude') : null;
let center = (hasLatAndLong !== null && hasLatAndLong.length === 2) ? true : false;
return (center) ? properties.center : null;
};

setCenter = (latitude, longitude) => {
if (!this.logged) return null;
if ( isNaN(latitude) || isNaN(longitude) ) return null;
this.props.setCenter({ latitude: latitude, longitude: longitude });
};

getZoom = () => {
if (!this.logged) return null;
let properties = (this.props.properties !== null) ? Object.assign({}, this.props.properties) : null;
let hasZoom = (properties !== null && Object.keys(properties).filter(p => p === "zoom")) ? true : false;
return (hasZoom !== null) ? properties.zoom : null;
};

setZoom = (zoom) => {
if (!this.logged) return null;
let checkedZoom;
if (Array.isArray(zoom)) checkedZoom = zoom;
else checkedZoom = [zoom];
this.props.setZoom(checkedZoom);
};

render() { return null };
}

function selectStateApp(state) {
return {
userLogged: state.auth.userLogged,
layers: state.maplayers.mapGLlayers,
properties: state.map.properties,
};
}

export default compose(withTranslation('maps'), connect(
selectStateApp,
dispatch => ({
changeLayerVisibility: (layer) => LayerActions.changeLayerVisibility(layer)(dispatch),
setCenter: (center) => MapActions.setCenter(center)(dispatch),
setZoom: (zoom) => MapActions.setZoom(zoom)(dispatch),
setProperties: (properties) => MapActions.setProperties(properties)(dispatch),
})
))(APIService);

最佳答案

如果我理解正确的话,你基本上是想将你的 React 组件渲染到 DOM 中,对吗?如果是这样,这里有官方的 React 文档:Rendering Elements .

或者,如果您想像使用任何其他可在任何地方使用的 HTML 元素一样使用 React 组件,您可以考虑将 React 组件包装在 Web Component 中。 。 React 官方文档也有这样的内容:Using React in your Web Components 。对于您的组件来说,它看起来像这样:

class APIServiceElement extends HTMLElement {
connectedCallback() {
const mountPoint = document.createElement('div');
this.attachShadow({ mode: 'open' }).appendChild(mountPoint);

ReactDOM.render(<APIService />, mountPoint);
}
}
customElements.define('api-service', APIServiceElement);

之后您可以使用<api-service></api-service>在你的index.html文件。

关于javascript - 如何从外部文件访问React组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57215147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com