Javascript 在React Native中使用构造函数创建对象
我试图用传递json对象的构造函数创建一个RN对象,但我得到了“ReferenceError:找不到变量:Product” Product.jsJavascript 在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
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”导入产品
请在此处查看有关导入的一些详细信息。
谢谢!我更改了导入,但现在出现了其他错误。(更多细节编辑)谢谢!我更改了导入,但现在出现了其他错误。(详细内容编辑)