面向错误窗口未定义[已收集]

时间:2017-09-26 05:55:00

标签: javascript reactjs meteor

请参阅以下代码

render() {

    const post = this.props.post;

    let postClass = "posts-item";
    if (post.sticky) postClass += " posts-sticky";

    // ⭐ custom code starts here ⭐
    if (post.color) {
      postClass += " post-"+post.color;
    }
    // ⭐ custom code ends here ⭐
    const currentUrl = window ? window.location.href : undefined;
    .......
    ........
}

在此代码中,行const currentUrl = window ? window.location.href : undefined;正在生成以下错误

url:  /
{ [ReferenceError: window is not defined] queryErrors: [ [Circular] ] }

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我已通过以下代码修复此问题

let currentUrl;
    if(Meteor.isClient)
     currentUrl = window ? window.location.href : undefined;