如何在本机中创建一个对象?

时间:2022-11-09 19:42:03

I just started learning react native and I am having issues with creating an object. Here is the code

我刚刚开始学习本机反应,我遇到创建对象的问题。这是代码

import React, { Component } from 'react';
import {
  View,
  Text,
  StyleSheet,
} from 'react-native';

export default class object {
    name: 'joe'
    age: 27
    country: 'France'
}

And when I try to instantiate it in another class like this

当我尝试在另一个类中实例化它时

 import React, { Component } from 'react';
    import {
      View,
      Text,
      StyleSheet,
    } from 'react-native';

    import object from './object.js'

    export default class MyComponent extends Component {

      var man = new Object();

      render() {
        return (
          <View style={styles.container}>
            <Text>{man.age}</Text>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
    })

I get a syntax error Unexpected token var ->man = new Object(). How do I fix this?

我得到一个语法错误意外的令牌var - > man = new Object()。我该如何解决?

1 个解决方案

#1


-1  

the wrong part is you export default class object, FYI: CLASS is not OBJECT

错误的部分是导出默认类对象,FYI:CLASS不是OBJECT

the correct using of export is

正确使用出口是

export const object = {
    name: 'joe',
    age: 27,
    country: 'France'
}

then you can import it with

然后你可以导入它

import {object} from './object.js'

#1


-1  

the wrong part is you export default class object, FYI: CLASS is not OBJECT

错误的部分是导出默认类对象,FYI:CLASS不是OBJECT

the correct using of export is

正确使用出口是

export const object = {
    name: 'joe',
    age: 27,
    country: 'France'
}

then you can import it with

然后你可以导入它

import {object} from './object.js'

相关文章