我正在尝试从资产中读取文件。我的代码是:
import { HttpClient, HttpParams, HttpHeaders, HttpRequest } from "@angular/common/http";
.
.
assetURL: string = "../assets/";
androidAssetURL: string = "'../www/assets/";
.
.
public readAssetFile(fileName: string) {
var url = this.assetURL + fileName;
if (this.platform.is("cordova") && this.platform.is("android")) {
url = this.androidAssetURL + fileName;
}
this.log("reading data file location:" + url);
return this.http
.get(url, { responseType: "text" })
.first()
.toPromise();
}
该位置适用于网络
读取数据文件位置:../资产/数据/关于
但在设备上我得到了位置
读取数据文件位置:'../www/assets/data/about
但真正的内容不会被读取,而是将下面的 HTML 作为内容
data file:<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<script data-ionic="inject">
(function(w){var i=w.Ionic=w.Ionic||{};i.version='3.9.2';i.angular='5.2.11';i.staticDir='build/';})(window);
</script>
<meta charset="UTF-8">
<title>Ionic App</title>
<meta name="viewport"
content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<met
.
.
.
添加扩展程序解决了这个问题,它适用于浏览器和设备。
其中文件参数是"关于.txt"而不是"关于">
//strings
assetURL: string = "/assets/data/";
//to read
getData(file: string) {
this.readAssetData(file).subscribe(
it => {
this.core.log("data file:" + it);
this.pageText = it;
},
err => {
this.core.log("error read file:" + JSON.stringify(err));
}
);
}
public readAssetData(fileName: string) {
var url = this.assetURL + fileName;
this.log("reading data file location:" + url);
return this.http.get(url, { responseType: "text" });
}