【问题标题】:Is there any way to export variables in react-native?有什么方法可以在 react-native 中导出变量?
【发布时间】:2020-11-25 10:55:35
【问题描述】:

我正在做应用程序,我检查图像大小,计算它的比例,因为 pdf 中的高度必须始终为 100px,宽度可以更改,我将其更改为原始图片的比例。

我试图将 const 变量导出到不同的组件。但它也给了我export { imageHeight };export { imageWidth }; 的语法错误。如何在 react-native 中进行此导入?这应该在反应中起作用。我不能使用导出默认值,因为我已经有了它。

相机组件:

Image.getSize(data.uri, (width, height) => { // KOODI TOIMII ja hakee tiedot
            let imageWidth = width;
            let imageHeight = height;
            
            console.log(`The image dimensions are ${imageWidth}x${imageHeight}`);
          }, (error) => {
            console.error(`Couldn't get the image size: ${error.message}`);
          });

export { imageHeight };
export { imageWidth};

然后,我有一个 pdf 创建组件:

import { imageHeight, imageWidth } from './Camera';

const pdfHeight = 100;
const ratio = imageHeight/imageWidth; // example 1200/1600=0,75

page.drawImage(arr[i].path.substring(7),'jpg',{
                    x: imgX,
                    y: imgY,
                    width: pdfHeight/ratio,
                    height: pdfHeight,
                })

【问题讨论】:

标签: react-native


【解决方案1】:
  1. 您无法导出无名 JSON 文件。

  2. 你不能使用 let 定义的变量超出范围

  3. 您可以使用export default variableNameexport variableName 导出

    const {imageWidth, imageHeight} = Image.getSize(data.uri, (width, height) => {
            let imageWidth = width;
            let imageHeight = height;
    
            console.log(`The image dimensions are ${imageWidth}x${imageHeight}`);
          return {imageWidth, imageHeight}
          }, (error) => {
            console.error(`Couldn't get the image size: ${error.message}`);
          });
    
    export imageHeight;
    export imageWidth;
    

    import { imageHeight, imageWidth } from './Camera';
    const pdfHeight = 100;
    const ratio = imageHeight/imageWidth; // example 1200/1600=0,75
    
    page.drawImage(arr[i].path.substring(7),'jpg',{
         x: imgX,
         y: imgY,
         width: pdfHeight/ratio,
         height: pdfHeight,
    })
    

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 2018-01-24
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2018-02-25
    • 2020-01-12
    相关资源
    最近更新 更多