【发布时间】:2022-01-15 01:23:04
【问题描述】:
我正在更改 Lit Web 组件的属性,但更改后的值不会呈现。
我有一个观察到的数组:reports[],它将在 firstUpdated() 中填充,其中包含从 rest api 获取的报告 url。数组的加载通过以下方式完成:
this.reports.push({ "name" : report.Name, "url" : this.apiUrl + "/" + report.Name + "?rs:embed=true" });
见下文:
import { LitElement, html, css } from 'lit';
import {apiUrl, restApiUrl} from '../../config';
export default class Homepage extends LitElement {
static properties = {
apiUrl: '',
restApiUrl: '',
reports: []
}
...
constructor() {
super();
this.apiUrl = apiUrl;
this.restApiUrl= restApiUrl;
this.reports = [];
}
firstUpdated() {
...
// Fetch all reports from restApiUrl:
rsAPIDetails(restApiUrl).then(reports =>{
for(const report of reports.value)
{
rsAPIDetails(restApiUrl + "(" + report.Id + ")/Policies").then(policies => {
for(const policy of policies.Policies)
{
if(policy.GroupUserName.endsWith(usernamePBI))
{
for(const role of policy.Roles)
{
if(role != null && (role.Name== "Browser" || role.Name== "Content Manager"))
{
// User has access to this report so i'll push it to the list of reports that will show in the navbar:
this.reports.push({ "name" : report.Name, "url" : this.apiUrl + "/" + report.Name + "?rs:embed=true" });
}
}
}
}
});
}
}).then(q => {
console.log(this.reports);
});
}
render() {
return html`
<div id="sidenav" class="sidenav">
...
<div class="menucateg">Dashboards</div>
${this.reports.map((report) =>
html`<a @click=${() => this.handleMenuItemClick(report.url)}>${report.name}</a>`
)}
<div class="menucateg">Options</div>
</div>
`;
}
在控制台我可以清楚地看到数组加载了正确的值。 但是 render() 函数不会用新的 reports[] 值更新 web 组件: The links should be added inside 'Dashboards' div
如果我用值(在 ctor 中)静态填充报告[],它会很好地呈现链接。
那么为什么当观察到的数组改变时组件没有更新呢?
谢谢!
【问题讨论】:
标签: web components shadow-dom lit-element