简体   繁体   English

为什么我的Angular2应用找不到我的模型?

[英]Why can't my Angular2 app find my model?

Error: XHR error (404) loading https://run.plnkr.co/aT9KvIPBnvAmjN8u/models/home 错误:XHR错误(404)加载https://run.plnkr.co/aT9KvIPBnvAmjN8u/models/home

Plnkr link: https://plnkr.co/edit/gwa3NWArtWK0wjf2jr2h?p=preview Plnkr链接: https ://plnkr.co/edit/gwa3NWArtWK0wjf2jr2h ? p = preview

I created models/home.ts 我创建了models/home.ts

在此处输入图片说明

Which contains: 其中包含:

export function homeData() {
  return [
    { id: 0,
      title: '2017 Oscars',
      graphic: 'https://wikitags.com/images/OscarsBanner.png',
      categorycards: [
        {
            type: 'image',
            graphic: 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Ryan_Gosling_2_Cannes_2011_%28cropped%29.jpg/1024px-Ryan_Gosling_2_Cannes_2011_%28cropped%29.jpg?width=440',
            title: '2017 Oscar Nominee for Best Actor',
            listings: ['Rayn Gosling', 'Denzel Washington', 'Andrew Garfield', 'Casey Affleck', 'Viggo Mortensen']
        },
        {
            type: 'image',
            graphic: 'https://commons.wikimedia.org/wiki/Special:FilePath/Meryl_Streep_February_2016.jpg?width=440',
            title: '2017 Oscar Nominee for Best Actress',
            listings: ['Meryl Streep', 'Emma Stone', 'Natalie Portman', 'Ruth Negga']
        },
        {
            type: 'image',
            graphic: 'https://commons.wikimedia.org/wiki/Special:FilePath/Dev_Patel_(29870651654).jpg?width=440',
            title: '2017 Oscar Nominee for Best Supporting Actor',
            listings: ['Dev Patel', 'Jeff Bridges', 'Michael Shannon', 'Lucas Hedges', 'Mahershala Ali']
        },
        {
            type: 'image',
            graphic: 'https://commons.wikimedia.org/wiki/Special:FilePath/Michelle_Williams_by_Gage_Skidmore.jpg?width=440',
            title: '2017 Oscar Nominee for Best Supporting Actress',
            listings: ['Michelle Williams', 'Nicole Kidman', 'Viola Davis', 'Octavia Spencer']
        }
      ]
    }
  ];
}

Then in my categoryService.ts I import it like so: 然后在我的categoryService.ts中,将其导入为:

import {Injectable} from 'angular2/angular2';
import {Http} from 'angular2/http';
import {homeData} from 'models/home'; // <- here it is with correct path

@Injectable()
export class CategoryService {
  constructor(http:Http) {
    this.categories = http.get('api/categories.json').map(res => {
      // console.log(res)
      // return res.json();
      return res.send(homeData());
    });
  }
}

Yet it can't be found 却找不到

在此处输入图片说明

Move your models folder to the src folder, 将您的models文件夹移至src文件夹,

and do this : 并这样做:

import {homeData} from './models/home';

all your code must be under the src folder because in your config.js file you tell System.js the your app is in the src folder: 您的所有代码都必须在src文件夹下,因为在config.js文件中,您告诉System.js您的应用程序在src文件夹中:

//config.js
map: {
    app: "./src"
  },

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

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