我是 Polymer 的新手,目前正在学习Polymer 3.0。我正在尝试制作一个工作如下的应用程序:
- 从公共 url 获取播放列表数据(使用 iron-ajax 元素)
- 在handleResponse() 函数中处理数据,并将值分配给' playlistNames '变量。
- 使用路由显示播放列表和歌曲
问题:(我被困在哪里)
在刷新页面时http://127.0.0.1:8081/playlist1
,路由器正在加载 404 页面而不是播放列表页面,因为playlistName
是空的。但是,如果我单击任何播放列表(刷新页面后),路由器正在工作。
我无法playlistNames
在不同的函数“_routePageChanged”中访问更新后的数组。代码片段如下。
在handleResponse() func
我得到正确的输出(带有播放列表列表的数组)。但在_routePageChanged() func'
输出中是 [ ],一个空数组。
问题:
我的理解:app-route
元素在iron-ajax
元素之前处理。
如何访问playlistNames
in的新值_routePageChanged
?
文件:my-element.js
class MyApp extends PolymerElement {
static get template() {
return html `
<!-- make api calls to fetch the playlists -->
<iron-ajax
auto
url=""
handle-as = "json"
on-response="handleResponse"
last-response="{{ajaxResponse}}">
</iron-ajax>
<app-location route="{{route}}" url-space-regex="^[[rootPath]]">
</app-location>
<app-route route="{{route}}" pattern="[[rootPath]]:page" data="{{routeData}}" tail="{{subroute}}">
</app-route>
...
<iron-pages selected="[[page]]" attr-for-selected="name" role="main">
<my-view404 name="view404"></my-view404>
<data-send name="playlist" prop1="[[selectedPlaylist]]"></data-send>
</iron-pages>
`;
}
static get properties() {
return {
page: {
type: String,
reflectToAttribute: true,
observer: '_pageChanged'
},
playlists: {
type: Array,
reflectToAttribute: true,
value: 'waiting...',
reflectToAttribute: true
},
playlistNames: {
type: Array,
value: function() {
return [];
}
},
selectedPlaylist: {
type: String,
value: 'selectedPlaylist is empty'
},
routeData: Object,
subroute: Object
};
}
static get observers() {
return [
'_routePageChanged(routeData.page, this.playlistNames.splices)'
];
}
// this function is called on api call
handleResponse(event, request) {
// get all the playlists from the api call response
this.playlists = request.response.playlists;
// get all the playlist names
for(let playlist of this.playlists) {
if(this.playlistNames.length < this.playlists.length) {
// this.playlistNames.push(playlist.name);
this.push('playlistNames', playlist.name);
}
}
console.log('handleResponse playlists', this.playlistNames); // ['playlist1', 'playlist2' ...]
// on refreshing url, if any route is present, then find its id to fetch songs list from api
if(this.routeData.page){
for(let playlist of this.playlists){
if(playlist.name === this.routeData.page){
this.selectedPlaylist = playlist.id;
}
}
}
}
// this func is called on route change or page refresh
_routePageChanged(page, data) {
console.log('routeData.page=', page);
console.log('data=', data); // undefined
console.log('_routePageChanged playlists.length=', this.playlistNames.length); // 0
console.log('_routePageChanged playlists=', this.playlistNames); // empty
if (!page) {
this.page = 'view404';
} else if (["playlist1", "playlist2"].indexOf(page) !== -1) {
//playlistNames is empty[],
// } else if (this.playlistNames.indexOf(page) !== -1) {
this.page = 'playlist';
} else {
this.page = 'view404';
}
}
_pageChanged(page) {
switch (page) {
case 'view404':
import('./my-view404.js');
break;
case 'playlist':
import('./playlist-songs.js');
break;
}
}
}