Autocomplete 使用远程json的Nativescript vue自动完成
我拼命地想让它工作,但除了 但是,它似乎是用TypeScript编写的(这很奇怪),因此使用此代码会带来两个错误: “类型”只能在.ts文件中使用 代码如下:Autocomplete 使用远程json的Nativescript vue自动完成,autocomplete,nativescript,Autocomplete,Nativescript,我拼命地想让它工作,但除了 但是,它似乎是用TypeScript编写的(这很奇怪),因此使用此代码会带来两个错误: “类型”只能在.ts文件中使用 代码如下: import * as http from 'tns-core-modules/http'; export default { template: ` <Page> <StackLayout> <Label text="Select airport"></Label&
import * as http from 'tns-core-modules/http';
export default {
template: `
<Page>
<StackLayout>
<Label text="Select airport"></Label>
<RadAutoCompleteTextView ref="autocomplete"
displayMode="plain"
suggestMode="Suggest"
:items="dataItems">
<SuggestionView ~suggestionView suggestionViewHeight="300">
<StackLayout v-suggestionItemTemplate orientation="vertical" padding="10">
<v-template>
<Label :text="item.text"></Label>
</v-template>
</StackLayout>
</SuggestionView>
</RadAutoCompleteTextView>
</StackLayout>
</Page>
`,
data () {
return {
dataItems: new ObservableArray(),
};
},
mounted () {
const jsonUrl = 'https://raw.githubusercontent.com/telerik/nativescript-ui-samples/master/examples-data/airports.json';
this.$refs.autocomplete.setLoadSuggestionsAsync((text) => {
const promise = new Promise((resolve, reject) => {
http.getJSON(jsonUrl).then((r: any) => {
const airportsCollection = r.airports;
const items: Array<TokenModel> = new Array();
for (let i = 0; i < airportsCollection.length; i++) {
items.push(new TokenModel(airportsCollection[i].FIELD2, null));
}
resolve(items);
}).catch((err) => {
const message = `Error fetching remote data from ${jsonUrl}: ${err.message}`;
console.log(message);
alert(message);
reject();
});
});
return promise;
});
},
};
import*作为http从“tns核心模块/http”导入;
导出默认值{
模板:`
`,
数据(){
返回{
dataItems:新的ObservableArray(),
};
},
挂载(){
常量jsonUrl=https://raw.githubusercontent.com/telerik/nativescript-ui-samples/master/examples-data/airports.json';
此.$refs.autocomplete.setLoadSuggestionAsync((文本)=>{
持续承诺=新承诺((解决、拒绝)=>{
http.getJSON(jsonUrl).then((r:any)=>{
const airports集合=r.机场;
常量项:数组=新数组();
for(设i=0;i{
const message=`从${jsonUrl}获取远程数据时出错:${err.message}`;
控制台日志(消息);
警报(信息);
拒绝();
});
});
回报承诺;
});
},
};
谢谢你的启发 试着去掉打字
http.getJSON(jsonUrl).then((r: any) => {
将成为
http.getJSON(jsonUrl).then((r) => {
const items = new Array();
也
试着去掉打字
http.getJSON(jsonUrl).then((r: any) => {
将成为
http.getJSON(jsonUrl).then((r) => {
const items = new Array();
也
我只是遇到了一些类似的情况,虽然我可以实现这一点,但我有点困惑,当我将自动完成项设置为本地数据项数组时,我从未在setLoad中看到过,我只是遇到了一些类似的情况,虽然我可以实现这一点,但我对如何将自动完成项设置为本地数据项感到困惑我从未在setLoad中看到的数组执行此操作,但现在我有一个新错误:ObservableArray未定义您应该导入它-
import{ObservableArray}来自“tns核心模块/数据/ObservableArray”
这样做了,但TokenModel也有同样的错误,所以我也从'nativescript ui autocomplete'导入了它(导入{TokenModel}),现在它工作正常了!谢谢!这样做了,但现在我有一个新的错误:ObservalArray没有定义您应该导入它-导入{ObservalArray}从“tns核心模块/data/observable array”中;
这样做了,并且与TokenModel有相同的错误,所以我也从“nativescript ui自动完成”中导入了它(导入{TokenModel}
,现在它工作正常了!谢谢!