错误:未捕获(在承诺中):响应状态:升级到Angular-CLI + Angular 4时找不到404的URL

时间:2017-04-26 16:26:28

标签: angular

我更新了我的项目,Angular 2和现在4的一切都很好。我现在引入了{ "properties":{ "platform_type_description":"Weather and Sea Surface Data", "parameters":[ { "id":"wind_speed", "units":"m s-1", "depth":{ "value":"0", "units":"m" }, "observations":{ "times":[ "2017-04-21T14:08:00" ], "values":[ "5.586995" ], "quality_levels":[ "3" ] } }, { "id":"wind_speed_of_gust", "units":"m s-1", "depth":{ "value":"0", "units":"m" }, "observations":{ "times":[ "2017-04-21T14:08:00" ], "values":[ "7.39788" ], "quality_levels":[ "3" ] } } ] } } func getWind() { let url = URL(string: "http://windapp.dev/")! URLSession.shared.dataTask(with: url) { (data:Data?, response:URLResponse?, error:Error?) in if error == nil { if data != nil { do { let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any] guard let properties = json["properties"] as? [String:Any], let parameters = properties["parameters"] as? [Any], let windspeedDict = parameters[0] as? [String:Any], let observationsWindSpeedDict = windspeedDict["observations"] as? [String:[String]] else { print("Error!") return } for (key, value) in observationsWindSpeedDict { if key == "values" { print(value) } } } catch {} } } else { print("Error: Couldn't connect to server") } }.resume() } ,它找不到primeNG的json文件。

Angular-CLI 1.0.0

抛出错误,我尝试添加 constructor(private http: Http) {} getFiles() { return this.http.get('mock_data/data.json') .toPromise() .then(res => <TreeNode[]> res.json().data) .then(data => { return data; }); } 复制文件夹的路径进行仔细检查。

在我的./文件中,我有index.html,目录位于<base href="/">

错误:

src

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试将mock_data移至assets文件夹,然后使用'assets/mock_data/data.json'更新网址。

建议编辑:

另一种方式:"mock-data"添加到.angular-cli.json文件中:

"assets": [
            "assets",
            "favicon.ico",
            "mock-data"
        ]