[英]Why can't my Angular2 app find my model?
錯誤:XHR錯誤(404)加載https://run.plnkr.co/aT9KvIPBnvAmjN8u/models/home
Plnkr鏈接: https ://plnkr.co/edit/gwa3NWArtWK0wjf2jr2h ? p = preview
我創建了models/home.ts
其中包含:
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']
}
]
}
];
}
然后在我的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());
});
}
}
卻找不到
將您的models
文件夾移至src
文件夾,
並這樣做:
import {homeData} from './models/home';
您的所有代碼都必須在src
文件夾下,因為在config.js
文件中,您告訴System.js您的應用程序在src
文件夾中:
//config.js
map: {
app: "./src"
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.