2016-10-08 12 views
9

मेरे पास GoComponent नामक एक साझा घटक है जिसे मैं 2 मॉड्यूल में उपयोग करना चाहता हूं: FindPageModule और AddPageModule।एक ही घटक का उपयोग कर कई मॉड्यूल त्रुटि का कारण बनता है - कोणीय 2

find:21 Error: (SystemJS) Type GoComponent is part of the declarations of 2 modules: FindPageModule and AddPageModule! Please consider moving GoComponent to a higher module that imports FindPageModule and AddPageModule. You can also create a new NgModule that exports and includes GoComponent then import that NgModule in FindPageModule and AddPageModule.

तो मैं यह आप दोनों को लेने के लिए और AppModule घोषणाओं में यह जोड़ने के लिए, जो करता है:

जब मैंने इसे "FindPageModule" की घोषणाओं में और मेरी "AddPageModule" में जोड़ने मैं कोई त्रुटि मिलती है आयात FindPageModule और AddPageModule, और मैं एक घटक "FindFormComponent" कहा जाता है जो FindPageModule की घोषणाओं में है और "GoComponent" का उपयोग करता है में कोई त्रुटि मिलती है:

zone.js:355 Unhandled Promise rejection: Template parse errors: 
'go' is not a known element: 
1. If 'go' is an Angular component, then verify that it is part of this module. 
2. If 'go' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" style="height:64px;"> 
      <div style="position: relative; display: inline-block; width: 100%;"> 
       [ERROR ->]<go></go> 
      </div> 
     </div> 
"): [email protected]:4 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: 
'go' is not a known element: 
1. If 'go' is an Angular component, then verify that it is part of this module. 
2. If 'go' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" style="height:64px;"> 
      <div style="position: relative; display: inline-block; width: 100%;"> 
       [ERROR ->]<go></go> 
      </div> 
     </div> 
"): [email protected]:4 

मैं ऐसे FindFormComponent के रूप में घटक है कि FindPageModule घोषित किये गए हैं कैसे दे सकता हूँ GoComponent ए का उपयोग करें nd AddPageModule द्वारा घोषित घटकों को GoComponent का उपयोग करने दें?

उत्तर

19

हां, घटकों को केवल एक मॉड्यूल में घोषित किया जा सकता है, और न ही उनकी पहुंच किसी भी तरह से विरासत में मिली है, जिसका अर्थ यह है कि मुख्य ऐप मॉड्यूल में इसे घोषित करने से आपको किसी अन्य मॉड्यूल में इसका उपयोग नहीं मिलेगा।

@NgModule({ 
    declarations: [ SharedComponent ], 
    exports: [ SharedComponent ] 
}) 
class SharedModule {} 

उपयोग कैसे करें:

वे जिस तरह से जाने के लिए आप एक घटक है, आम तौर पर है कि अन्य मॉड्यूल द्वारा प्रयोग किया जाता है, तो

एक साझा मॉड्यूल में घटक शामिल एक साझा मॉड्यूल में यह डाल करने के लिए है कहीं और साझा मॉड्यूल:

@NgModule({ 
    imports: [ SharedModule ] 
}) 
class ModuleWithComponentThatUsesSharedComponent {} 

देखें इसके अलावा

+2

ऐसा लगता है वहाँ कुछ लोग है कि इस विचार के लिए असली हिप नहीं हैं और अधिक से अधिक कर रहे हैं: https://github.com/angular/angular/issues/10646 – Brandon

1

यदि आप कई मॉड्यूल में GoComponent का उपयोग करना चाहते हैं, तो आपको "साझा" मॉड्यूल बनाया जाना चाहिए और साझा मॉड्यूल के निर्यात में GoComponent जोड़ें। फिर आप साझा मॉड्यूल को अपने अन्य मॉड्यूल में आयात करते हैं जहां आप इस घटक का उपयोग करना चाहते हैं।

पर here

आशा इस मदद में अधिक जानकारी का पता लगाएं!

संबंधित मुद्दे