菜鸟教程小白 发表于 2022-12-12 13:35:56

javascript - 有没有办法导出默认的 2 个常量?


                                            <p><p><em>注意:我是学习 React Native 的初学者。</em> 我有两个 js 文件(Inputs.js 和 Styles.js),我试图将它们都放在 <code>const</code> 在我的主 js 文件 (App.js) 中,但我只能 <code>export default</code> 其中一个。有没有办法可以同时导出它们,或者我应该以另一种方式重新排列我的代码?</p>

<p><strong>App.js:</strong></p>

<pre><code>import React from &#39;react&#39;;
import { StyleSheet, Text, View } from &#39;react-native&#39;;

const Home1 = () =&gt; {
   return (
      &lt;Style/&gt;
   )
}
const Home2 = () =&gt;{
   return (
      &lt;Inputs /&gt;
   )
}

export default Home1; //I am unable to export both Home1 and Home2 here
</code></pre>

<p><strong>Style.js:</strong></p>

<pre><code>import React, { Component } from &#39;react&#39;;
import { View, Text, Image, StyleSheet } from &#39;react-native&#39;

const Style = () =&gt; {
    return ( &lt;View style = {styles.container}&gt;
         &lt;Text style = {styles.text}&gt;
            &lt;Text style = {styles.capitalLetter}&gt;
               Title Here
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}{&#34;\n&#34;}Location: &lt;/Text&gt;
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}Time:&lt;/Text&gt;
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}Time: &lt;/Text&gt;
            &lt;/Text&gt;
         &lt;/Text&gt;
      &lt;/View&gt;
   )
}

export default Style

const styles = StyleSheet.create ({
   container: {
      //alignItems: &#39;center&#39;,
      marginTop: 50,
   },

   text: {
      color: &#39;#41cdf4&#39;,
   },

   capitalLetter: {
      color: &#39;red&#39;,
      fontSize: 20
   },

   textInput: {
      padding: 22,
      //fontWeight: &#39;bold&#39;,
      color: &#39;black&#39;
   },


   textShadow: {
      textShadowColor: &#39;red&#39;,
      textShadowOffset: { width: 2, height: 2 },
      textShadowRadius : 5
   }
})
</code></pre>

<p><strong>Inputs.js:</strong></p>

<pre><code>import React, { Component } from &#39;react&#39;
import { View, Text, TouchableOpacity, TextInput, StyleSheet } from &#39;react-native&#39;

class Inputs extends Component {

   state = {
      email: &#39;&#39;,
      password: &#39;&#39;
   }

   handleEmail = (text) =&gt; {
      this.setState({ email: text })
   }

   handlePassword = (text) =&gt; {
      this.setState({ password: text })
   }

   login = (email, pass) =&gt; {
      alert(&#39;email: &#39; + email + &#39; password: &#39; + pass)
   }

   render(){
      return (
         &lt;View style = {styles.container}&gt;
            &lt;TextInput style = {styles.input}
               underlineColorAndroid = &#34;transparent&#34;
               placeholder = &#34;Email&#34;
               placeholderTextColor = &#34;#9a73ef&#34;
               autoCapitalize = &#34;none&#34;
               onChangeText = {this.handleEmail}/&gt;

            &lt;TextInput style = {styles.input}
               underlineColorAndroid = &#34;transparent&#34;
               placeholder = &#34;Password&#34;
               placeholderTextColor = &#34;#9a73ef&#34;
               autoCapitalize = &#34;none&#34;
               onChangeText = {this.handlePassword}/&gt;

            &lt;TouchableOpacity
               style = {styles.submitButton}
               onPress = { () =&gt; this.login(this.state.email, this.state.password)}&gt;
               &lt;Text style = {styles.submitButtonText}&gt;
                  Submit
               &lt;/Text&gt;
            &lt;/TouchableOpacity&gt;
         &lt;/View&gt;
      )
   }
}

export default Inputs

const styles = StyleSheet.create({
   container: {
      paddingTop: 200
   },

   input: {
      margin: 15,
      height: 40,
      borderColor: &#39;#7a42f4&#39;,
      borderWidth: 1
   },

   submitButton: {
      backgroundColor: &#39;#7a42f4&#39;,
      padding: 10,
      margin: 15,
      height: 40,
   },

   submitButtonText:{
      color: &#39;white&#39;
   }
})
</code></pre>

<p>****以下错误的更新代码:<code>元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。</code>:*****</p>

<p><strong>App.js:</strong></p>

<pre><code>import React from &#39;react&#39;;
import { StyleSheet, Text, View } from &#39;react-native&#39;;

module.exports = {
Home1() {
    return (
      &lt;Style/&gt;
    );
},
Home2() {
    return (
      &lt;Inputs/&gt;
    );
}
};
</code></pre>

<p><strong>Style.js</strong></p>

<pre><code>import React, { Component } from &#39;react&#39;;
import { View, Text, Image, StyleSheet } from &#39;react-native&#39;
import { Inputs } from &#34;./App.js&#34;
import Home1, {Home2} from &#39;./App.js&#39;

const Style = () =&gt; {
    return ( &lt;View style = {styles.container}&gt;
         &lt;Text style = {styles.text}&gt;
            &lt;Text style = {styles.capitalLetter}&gt;
               Title Here
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}{&#34;\n&#34;}Your address or location (eg, Nashville, TN): &lt;/Text&gt;
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}Contact 2:&lt;/Text&gt;
            &lt;/Text&gt;
            &lt;Text&gt;
                &lt;Text style = {styles.textInput}&gt; {&#34;\n&#34;} {&#34;\n&#34;}Contact 3: &lt;/Text&gt;
            &lt;/Text&gt;
         &lt;/Text&gt;
      &lt;/View&gt;
   )
}

export default Style

const styles = StyleSheet.create ({
   container: {
      //alignItems: &#39;center&#39;,
      marginTop: 50,
   },

   text: {
      color: &#39;#41cdf4&#39;,
   },

   capitalLetter: {
      color: &#39;red&#39;,
      fontSize: 20
   },

   textInput: {
      padding: 22,
      //fontWeight: &#39;bold&#39;,
      color: &#39;black&#39;
   },


   textShadow: {
      textShadowColor: &#39;red&#39;,
      textShadowOffset: { width: 2, height: 2 },
      textShadowRadius : 5
   }
})
</code></pre>

<p><strong>Inputs.js</strong></p>

<pre><code>import React, { Component } from &#39;react&#39;
import { View, Text, TouchableOpacity, TextInput, StyleSheet } from &#39;react-native&#39;
//import { Style } from &#34;./App.js&#34;
import Home1, {Home2} from &#39;./App.js&#39;

class Inputs extends Component {

   state = {
      email: &#39;&#39;,
      password: &#39;&#39;
   }

   handleEmail = (text) =&gt; {
      this.setState({ Location: text })
   }

   handlePassword = (text) =&gt; {
      this.setState({ Time: text })
   }

   login = (email, time1) =&gt; {
      alert(&#39;Location: &#39; + email+ &#39; Time: &#39; + time1)
   }

   render(){
      return (
         &lt;View style = {styles.container}&gt;
            &lt;TextInput style = {styles.input}
               underlineColorAndroid = &#34;transparent&#34;
               placeholder = &#34;Location&#34;
               placeholderTextColor = &#34;#9a73ef&#34;
               autoCapitalize = &#34;none&#34;
               onChangeText = {this.handleEmail}/&gt;

            &lt;TextInput style = {styles.input}
               underlineColorAndroid = &#34;transparent&#34;
               placeholder = &#34;Time&#34;
               placeholderTextColor = &#34;#9a73ef&#34;
               autoCapitalize = &#34;none&#34;
               onChangeText = {this.handlePassword}/&gt;

            &lt;TouchableOpacity
               style = {styles.submitButton}
               onPress = { () =&gt; this.login(this.state.email, this.state.password)}&gt;
               &lt;Text style = {styles.submitButtonText}&gt;
                  Submit
               &lt;/Text&gt;
            &lt;/TouchableOpacity&gt;
         &lt;/View&gt;
      )
   }
}

export default Inputs

const styles = StyleSheet.create({
   container: {
      paddingTop: 200
   },

   input: {
      margin: 15,
      height: 40,
      borderColor: &#39;#7a42f4&#39;,
      borderWidth: 1
   },

   submitButton: {
      backgroundColor: &#39;#7a42f4&#39;,
      padding: 10,
      margin: 15,
      height: 40,
   },

   submitButtonText:{
      color: &#39;white&#39;
   }
})
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在这种情况下,您可以使用 <code>module.exports</code> 通过两种方法导出对象。用法:</p>

<pre><code>export function Home1() {
return (
    &lt;Style/&gt;
);
};
export function Home2() {
return (
    &lt;Inputs/&gt;
);
};
</code></pre>

<p>这是您名为 App.js 的文件的一部分,因此要将其导入另一个文件,您需要:</p>

<pre><code>import { Home1, Home2 } from &#34;./App.js&#34;
</code></pre>

<p>在这种情况下,您使用的是<strong>命名导出</strong>。为了正确导入它,您明确需要使用 <code>import { Home1, Home2 }</code> ,其中 Home1 和 Home2 是导出函数的对应名称。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - 有没有办法导出默认的 2 个常量?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45201326/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45201326/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - 有没有办法导出默认的 2 个常量?