Component doesn't get contentChildren when using routerLink angular 6-7










0














I have an app with multiple pages (anguler-router, roter-outlet set up to show angular components...). On one of the pages I have an external lib, that I built myself, and have published to a company hosted NPM repo.
My lib works fine when I type the URL in manually, but when I route with routerLink, the parent component (afgm-map) gets an empty array for @ContentChildren (afgm-marker).
In the afgm-map component I have put in a console.log(markerRefs), which prints the marker/s when I go to the page, but when I route to the page with a link it prints an empty array.



:(



If I have used link to get to the page and press F5, it work fine. Any ideas?



On my pageTemplate I render the lib like so:



<afgm-map [center]="data.position" (toggleExpand)="expandMap($event)">
<afgm-marker [position]="data.position"></afgm-marker>
</afgm-map>


The map component ts code with a console log for debugging:



export class MapComponent implements OnInit 
@Input()
center!: google.maps.LatLng;
@Input()
zoom = 15;
@Output()
toggleExpand: EventEmitter<boolean> = new EventEmitter();
@ViewChild('mapViewPort')
mapViewPort: ElementRef;

@ContentChildren(MarkerComponent)
markersRefs: MarkerComponent = ;

expanded: boolean;
map: any;
markers: google.maps.Marker;

constructor()

ngOnInit()
...
this.onInit();


onInit()
this.map = new google.maps.Map(this.mapViewPort.nativeElement,
center: this.center,
zoom: +this.zoom,
disableDefaultUI: true,
scaleControl: true
);



console.log(this.markersRefs);




 this.markers = this.markersRefs.map(m => 
return new google.maps.Marker(
position: m.position,
icon: m.icon,
map: this.map
);
);

...



The marker component is just a shell-component with no real logic/template:



export class MarkerComponent implements OnInit 
@Input()
position!: google.maps.LatLng;

@Input()
icon = '/assets/img/mapMarker49px.png';

constructor()










share|improve this question




























    0














    I have an app with multiple pages (anguler-router, roter-outlet set up to show angular components...). On one of the pages I have an external lib, that I built myself, and have published to a company hosted NPM repo.
    My lib works fine when I type the URL in manually, but when I route with routerLink, the parent component (afgm-map) gets an empty array for @ContentChildren (afgm-marker).
    In the afgm-map component I have put in a console.log(markerRefs), which prints the marker/s when I go to the page, but when I route to the page with a link it prints an empty array.



    :(



    If I have used link to get to the page and press F5, it work fine. Any ideas?



    On my pageTemplate I render the lib like so:



    <afgm-map [center]="data.position" (toggleExpand)="expandMap($event)">
    <afgm-marker [position]="data.position"></afgm-marker>
    </afgm-map>


    The map component ts code with a console log for debugging:



    export class MapComponent implements OnInit 
    @Input()
    center!: google.maps.LatLng;
    @Input()
    zoom = 15;
    @Output()
    toggleExpand: EventEmitter<boolean> = new EventEmitter();
    @ViewChild('mapViewPort')
    mapViewPort: ElementRef;

    @ContentChildren(MarkerComponent)
    markersRefs: MarkerComponent = ;

    expanded: boolean;
    map: any;
    markers: google.maps.Marker;

    constructor()

    ngOnInit()
    ...
    this.onInit();


    onInit()
    this.map = new google.maps.Map(this.mapViewPort.nativeElement,
    center: this.center,
    zoom: +this.zoom,
    disableDefaultUI: true,
    scaleControl: true
    );



    console.log(this.markersRefs);




     this.markers = this.markersRefs.map(m => 
    return new google.maps.Marker(
    position: m.position,
    icon: m.icon,
    map: this.map
    );
    );

    ...



    The marker component is just a shell-component with no real logic/template:



    export class MarkerComponent implements OnInit 
    @Input()
    position!: google.maps.LatLng;

    @Input()
    icon = '/assets/img/mapMarker49px.png';

    constructor()










    share|improve this question


























      0












      0








      0


      0





      I have an app with multiple pages (anguler-router, roter-outlet set up to show angular components...). On one of the pages I have an external lib, that I built myself, and have published to a company hosted NPM repo.
      My lib works fine when I type the URL in manually, but when I route with routerLink, the parent component (afgm-map) gets an empty array for @ContentChildren (afgm-marker).
      In the afgm-map component I have put in a console.log(markerRefs), which prints the marker/s when I go to the page, but when I route to the page with a link it prints an empty array.



      :(



      If I have used link to get to the page and press F5, it work fine. Any ideas?



      On my pageTemplate I render the lib like so:



      <afgm-map [center]="data.position" (toggleExpand)="expandMap($event)">
      <afgm-marker [position]="data.position"></afgm-marker>
      </afgm-map>


      The map component ts code with a console log for debugging:



      export class MapComponent implements OnInit 
      @Input()
      center!: google.maps.LatLng;
      @Input()
      zoom = 15;
      @Output()
      toggleExpand: EventEmitter<boolean> = new EventEmitter();
      @ViewChild('mapViewPort')
      mapViewPort: ElementRef;

      @ContentChildren(MarkerComponent)
      markersRefs: MarkerComponent = ;

      expanded: boolean;
      map: any;
      markers: google.maps.Marker;

      constructor()

      ngOnInit()
      ...
      this.onInit();


      onInit()
      this.map = new google.maps.Map(this.mapViewPort.nativeElement,
      center: this.center,
      zoom: +this.zoom,
      disableDefaultUI: true,
      scaleControl: true
      );



      console.log(this.markersRefs);




       this.markers = this.markersRefs.map(m => 
      return new google.maps.Marker(
      position: m.position,
      icon: m.icon,
      map: this.map
      );
      );

      ...



      The marker component is just a shell-component with no real logic/template:



      export class MarkerComponent implements OnInit 
      @Input()
      position!: google.maps.LatLng;

      @Input()
      icon = '/assets/img/mapMarker49px.png';

      constructor()










      share|improve this question















      I have an app with multiple pages (anguler-router, roter-outlet set up to show angular components...). On one of the pages I have an external lib, that I built myself, and have published to a company hosted NPM repo.
      My lib works fine when I type the URL in manually, but when I route with routerLink, the parent component (afgm-map) gets an empty array for @ContentChildren (afgm-marker).
      In the afgm-map component I have put in a console.log(markerRefs), which prints the marker/s when I go to the page, but when I route to the page with a link it prints an empty array.



      :(



      If I have used link to get to the page and press F5, it work fine. Any ideas?



      On my pageTemplate I render the lib like so:



      <afgm-map [center]="data.position" (toggleExpand)="expandMap($event)">
      <afgm-marker [position]="data.position"></afgm-marker>
      </afgm-map>


      The map component ts code with a console log for debugging:



      export class MapComponent implements OnInit 
      @Input()
      center!: google.maps.LatLng;
      @Input()
      zoom = 15;
      @Output()
      toggleExpand: EventEmitter<boolean> = new EventEmitter();
      @ViewChild('mapViewPort')
      mapViewPort: ElementRef;

      @ContentChildren(MarkerComponent)
      markersRefs: MarkerComponent = ;

      expanded: boolean;
      map: any;
      markers: google.maps.Marker;

      constructor()

      ngOnInit()
      ...
      this.onInit();


      onInit()
      this.map = new google.maps.Map(this.mapViewPort.nativeElement,
      center: this.center,
      zoom: +this.zoom,
      disableDefaultUI: true,
      scaleControl: true
      );



      console.log(this.markersRefs);




       this.markers = this.markersRefs.map(m => 
      return new google.maps.Marker(
      position: m.position,
      icon: m.icon,
      map: this.map
      );
      );

      ...



      The marker component is just a shell-component with no real logic/template:



      export class MarkerComponent implements OnInit 
      @Input()
      position!: google.maps.LatLng;

      @Input()
      icon = '/assets/img/mapMarker49px.png';

      constructor()







      angular angular6 angular-routerlink






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:34









      Vadim Kotov

      4,32153247




      4,32153247










      asked Nov 12 '18 at 16:57









      Dizzypointed

      6817




      6817






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Seems to be a race condition. My map component is dependent on external script to be loaded, but I still expect angular to mount component in a deterministic way. What I did to fix it was to let the markers to ad them selves to the parent (DI parent in the constructor of the child, and the child then runs "mapComp.addMarker(this)") when the parent is done with it's setup it checks markers.length > 0 and runs renderMarkers(this.markers) and sets this.initialized = true. In the addMarker I check this.initialized === true if so I run renderMarkers([marker]). So it's both "belt" and "braces"...






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266788%2fcomponent-doesnt-get-contentchildren-when-using-routerlink-angular-6-7%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Seems to be a race condition. My map component is dependent on external script to be loaded, but I still expect angular to mount component in a deterministic way. What I did to fix it was to let the markers to ad them selves to the parent (DI parent in the constructor of the child, and the child then runs "mapComp.addMarker(this)") when the parent is done with it's setup it checks markers.length > 0 and runs renderMarkers(this.markers) and sets this.initialized = true. In the addMarker I check this.initialized === true if so I run renderMarkers([marker]). So it's both "belt" and "braces"...






            share|improve this answer

























              0














              Seems to be a race condition. My map component is dependent on external script to be loaded, but I still expect angular to mount component in a deterministic way. What I did to fix it was to let the markers to ad them selves to the parent (DI parent in the constructor of the child, and the child then runs "mapComp.addMarker(this)") when the parent is done with it's setup it checks markers.length > 0 and runs renderMarkers(this.markers) and sets this.initialized = true. In the addMarker I check this.initialized === true if so I run renderMarkers([marker]). So it's both "belt" and "braces"...






              share|improve this answer























                0












                0








                0






                Seems to be a race condition. My map component is dependent on external script to be loaded, but I still expect angular to mount component in a deterministic way. What I did to fix it was to let the markers to ad them selves to the parent (DI parent in the constructor of the child, and the child then runs "mapComp.addMarker(this)") when the parent is done with it's setup it checks markers.length > 0 and runs renderMarkers(this.markers) and sets this.initialized = true. In the addMarker I check this.initialized === true if so I run renderMarkers([marker]). So it's both "belt" and "braces"...






                share|improve this answer












                Seems to be a race condition. My map component is dependent on external script to be loaded, but I still expect angular to mount component in a deterministic way. What I did to fix it was to let the markers to ad them selves to the parent (DI parent in the constructor of the child, and the child then runs "mapComp.addMarker(this)") when the parent is done with it's setup it checks markers.length > 0 and runs renderMarkers(this.markers) and sets this.initialized = true. In the addMarker I check this.initialized === true if so I run renderMarkers([marker]). So it's both "belt" and "braces"...







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 9:05









                Dizzypointed

                6817




                6817



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266788%2fcomponent-doesnt-get-contentchildren-when-using-routerlink-angular-6-7%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto