简体   繁体   English

使用 fetch 后,离子角度组件未呈现

[英]ionic angular component is not rendering after using fetch

This is what I'm trying to make: (I think it's called angular carousel)这就是我想要做的:(我认为它被称为角度旋转木马) 在此处输入图片说明

And I'm trying to do it with the ionic components ion-slides and ion-slide as I'm showing down below我正在尝试使用离子成分ion-slidesion-slide来做,因为我在下面展示

 <div class="top-rated-movies">
    <div class="header">
      <img src="../../assets/img/top-rated-movies.png" alt="">
    </div>
    <div class="carousel-top-rated-movies">         <!-- carousel -->
      <ion-slides [options]="sliderConfigMovie">
        <ion-slide  *ngFor="let peli of movie"  >            
  
          <div class="template">
            <div class="movie">  
              <img src="{{peli.poster}}" alt="">            
            </div>
            
            <div class="nombre">
              <ion-label>{{peli.name}}</ion-label>
            </div>
          </div>            
        </ion-slide>
      </ion-slides>
    </div>
  </div>

This code behind creates dinamically the template in which the poster and name is going to rest.后面的代码以动态方式创建了海报和名称将在其中放置的模板。 As you can see the variable "movie" in the *ngFor is in the export class from the .ts as you can see down below(this is the home.page.ts ):如您所见, *ngFor 中的变量“电影”位于 .ts 的导出类中,如下所示(这是home.page.ts ):

    import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {


private movie = array;


 /* 
  private movie = [
    {id:1,
    nombre:'Forrest Gump',
    url: '../../assets/img/forrest-gump.png'
    },
    {id:2,
      nombre:'Harry Potter 1',
      url: '../../assets/img/harry-potter.png'
    },
    {id:3,
      nombre:'Hachiko',
      url: '../../assets/img/hachiko.png'
    },
    {id:4,
      nombre:'Rapido y Furioso 1',
      url: '../../assets/img/ff1.png'
    },
    {id:5,
      nombre:'American Reunion',
      url: '../../assets/img/american-reunion.png'
    },
    {id:6,
      nombre:'El Conjuro 2',
      url: '../../assets/img/conjuro2.png'
    }
    ] */
 
   sliderConfigMovie = {
    slidesPerView: 2.7      
  }

  constructor() {   }
}

  const api_url = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=2120307b21e02afa98bde189c351c683';
  const img_url = "https://image.tmdb.org/t/p/w500";

 
  var array = [];

  fetch(api_url)
    .then(res => res.json())
    .then(data => {      
    
        var obj = data.results;  
        
        let arr;       
    
        obj.forEach(pelicula=>{          
              let arr = [
              {nombre:pelicula.title,                  
              poster: img_url+pelicula.poster_path
              }]
              array.push(arr)               
        })        
    })

    console.log(array)

It appears twice(the movie variable), one is commented(this way works like a charm but since it's not dynamic it doesn't do the job for me) and the other one, well it doesn't work.它出现两次(电影变量),一个被评论(这种方式就像一种魅力,但由于它不是动态的,它对我不起作用),另一个,它不起作用。 I suspect it's because that line is compiling before fetch provides the data.我怀疑这是因为该行在 fetch 提供数据之前正在编译。 I'm working with the TMDB api fetching for movie data to show in a carousel-something way.我正在使用 TMDB api 获取电影数据,以某种方式以轮播方式显示。 So that is the problem, the html is not rendering and I'm not sure why, I'm new to angular.所以这就是问题所在,html 没有渲染,我不知道为什么,我是 angular 的新手。 This is the stuff that I'm using:这是我正在使用的东西:

  • Ionic 6.16.3离子 6.16.3
  • Angular 11.2.12角 11.2.12
  • Node: 14.16.1节点:14.16.1

I'm not sure if this is helpful but with the command ionic start I created the project with a blank template and then I found the next 6 files in the home folder:我不确定这是否有帮助,但是使用ionic start命令,我使用空白模板创建了项目,然后在主文件夹中找到了接下来的 6 个文件:

- home.module.ts
- home.page.ts
- home.page.html
- home.page.scss
- home.page.spec.ts
- home-routing.module.ts

by the way, that console.log(array) at the end of the home.page.ts code shows this in console:顺便说一下, home.page.ts代码末尾的console.log(array)在控制台中显示了这一点:

在此处输入图片说明

This is what the variable movie it's supposed to have when I said private movie = array but for some reason it's not having it and because of it the html has no data to work with therefor nothing to render这就是当我说private movie = array时它应该拥有的变量电影,但由于某种原因它没有它,因此 html 没有可用的数据,因此没有任何渲染

EDIT: I've just tested this other way but same result, I put the fetch inside a ngOnInit编辑:我刚刚以另一种方式进行了测试,但结果相同,我将提取放在 ngOnInit 中

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit{

  ngOnInit(){

    fetch(api_url)
    .then(res => res.json())
    .then(data => {      
    
        var obj = data.results;  
        
        let arr;       
    
        obj.forEach(pelicula=>{          
              let arr = [
              {nombre:pelicula.title,
              rating:pelicula.vote_average,
              poster: img_url+pelicula.poster_path
              }]
              array.push(arr)               
        })        
    })

  }//end of ngOnInit

private movie = array;

   sliderConfigMovie = {
    slidesPerView: 2.7      
  }

  constructor() {   }
}//end of export class HomePage

  const api_url = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=2120307b21e02afa98bde189c351c683';
  const img_url = "https://image.tmdb.org/t/p/w500";

 
  var array = [];

Several things I'm noticing within your last edit:我在您上次编辑中注意到的几件事:

  1. Your variable array is defined outside of the component.您的变量array是在组件外部定义的。
  2. Your movie member is private, this could affect you when you try to do a build.您的movie成员是私人的,这可能会在您尝试构建时影响您。
  3. You want movie to be an array with movie objects inside so your template will work- <img src="{{peli.poster}}" /> (perhaps call this movies too ;) )您希望movie是一个包含电影对象的数组,以便您的模板可以工作 - <img src="{{peli.poster}}" /> (也许也称此movies ;) )

From your last edit:从您上次编辑开始:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit{

  ngOnInit(){

    fetch(api_url)
    .then(res => res.json())
    .then(data => {      
    
        var obj = data.results;         
    
        obj.forEach(pelicula => {          
              let peli = {
                 nombre: pelicula.title,
                 rating: pelicula.vote_average,
                 poster: img_url+pelicula.poster_path
              }
              movie.push(peli)               
        })        
    })

  }//end of ngOnInit

  public movie = [];

   sliderConfigMovie = {
    slidesPerView: 2.7      
  }

  constructor() {   }
}//end of export class HomePage

  const api_url = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=2120307b21e02afa98bde189c351c683';
  const img_url = "https://image.tmdb.org/t/p/w500";

  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM