Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React Native中使用构造函数创建对象_Javascript_React Native - Fatal编程技术网

Javascript 在React Native中使用构造函数创建对象

Javascript 在React Native中使用构造函数创建对象,javascript,react-native,Javascript,React Native,我试图用传递json对象的构造函数创建一个RN对象,但我得到了“ReferenceError:找不到变量:Product” Product.js export default class Product { constructor(product) { this.name = product.name this.items = product.Items this.price = product.Price this.pro

我试图用传递json对象的构造函数创建一个RN对象,但我得到了“ReferenceError:找不到变量:Product”

Product.js

export default class Product {
    constructor(product) {
        this.name = product.name
        this.items = product.Items
        this.price = product.Price
        this.productID = product.ProductID 
        this.medias = product.Medias
        this.imageSmall = BASE_IMAGES_URL + product.MediaSmall
        this.imageLarge = this.getImageLarge(product.Medias)
    }
}
import { Product } from '../models/Product'
class PDP extends Component {
 render() {

    var imagesProd = [];
    var product = new Product(this.props.navigation.state.params.currentProduct);
      ....
}
}
PDP.js

export default class Product {
    constructor(product) {
        this.name = product.name
        this.items = product.Items
        this.price = product.Price
        this.productID = product.ProductID 
        this.medias = product.Medias
        this.imageSmall = BASE_IMAGES_URL + product.MediaSmall
        this.imageLarge = this.getImageLarge(product.Medias)
    }
}
import { Product } from '../models/Product'
class PDP extends Component {
 render() {

    var imagesProd = [];
    var product = new Product(this.props.navigation.state.params.currentProduct);
      ....
}
}
问题在于直接使用
this.props.navigation.state.params.currentProduct()
可以正常工作

编辑

根据您的提示,我将导入更改为从“../models/Product”导入产品,但我得到了

TypeError:TypeError:TypeError:TypeError:undefined不是 构造函数(评估“新的P.default”


问题在于你的进口。您在Product类中使用了默认导出,因此您的导入应该是

import Product from '../models/Product'

PDP.js中的第一行应该是从“../models/Product”导入产品

请在此处查看有关导入的一些详细信息。

谢谢!我更改了导入,但现在出现了其他错误。(更多细节编辑)谢谢!我更改了导入,但现在出现了其他错误。(详细内容编辑)