angular - 使用 Angular Universal 在 Angular 中执行服务器端渲染时,页面重定向后无法看到我的 html 源代码
问题描述
我正在开发一个 Angular 博客应用程序,我已经在我的博客应用程序中实现了服务器端渲染。我遇到的问题是,在我的博客主页上,我可以通过单击来自 Node.js API 的Ctrl+来查看所有博客及其源代码内容,单击u所选博客后,它将重定向到该博客并显示其内容,但是不是 HTML 源代码(存储在本地存储中以减少 API 调用,我从本地存储中获取它以显示选定的博客内容)。
我还读到 SSR 不支持窗口、文档和位置对象,并且本地存储是一个窗口属性,因此我已经按照此链接https://stackoverflow.com/a/57781883使用它。
在遵循所有教程和文档之后,我尝试了所有可能的方法,但对我没有用。帮助将不胜感激。提前致谢。
下面是我附上的代码
主页组件.html
<owl-carousel-o [options]="bannerSlider">
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class="penci-image-holder" src="{{recentPost[0]?.featured_image}}">
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[0])">{{recentPost[0]?.title}}</a></h3> <span
class="daytime">{{recentPost[0]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[1]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[1])">{{recentPost[1]?.title}}</a></h3> <span
class="daytime">{{recentPost[1]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[0]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[0])">{{recentPost[0]?.title}}</a></h3> <span
class="daytime">{{recentPost[0]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[1]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[1])">{{recentPost[1]?.title}}</a></h3> <span
class="daytime">{{recentPost[1]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
</owl-carousel-o>
主页组件.ts
bannerSlider: OwlOptions = {
loop: false,
navText: [''],
nav: true,
margin: 10,
dots: false,
responsive: {
0: {
items: 1
},
600: {
items: 2
},
1000: {
items: 2
}
}
};
relatedPost: OwlOptions = {
loop: true,
navText: [''],
nav: true,
margin: 15,
dots: false,
responsive: {
0: {
items: 1
},
600: {
items: 2
},
1000: {
items: 3
}
}
};
constructor(private _service: ServiceService, private router: Router) { }
recentPost = [];
ngOnInit(): void {
this.getRecentPost()
}
getRecentPost() {
let key = 'slider';
this._service.getRecentPostSlider(key).subscribe(res => {
if (!res.error) {
this.recentPost = res.data.data;
}
})
}
showDetails(post) {
localStorage.setItem('post', JSON.stringify(post));
this.router.navigate([`/${post.title_abbr}/`]);
}
BlogPost 组件.html
<div class="largeblog-area">
<div class="largeblog-content blogdetailtop">
<h6><a href=""> {{post?.category}} </a> </h6>
<h2>{{post?.title}}</h2>
<h5><span>written by {{post?.author}} </span> |
<span>{{post?.post_date * 1000 | date:'mediumDate'}}</span> </h5>
</div>
<div class="largeblogimg"><img alt="img" src="{{post?.featured_image}}"></div>
<div class="largeblog-content">
<p [innerHTML]="post?.description"></p>
<div class="post-tags">
<a href="#">fresh</a>
<a href="#">life</a>
<a href="#">style</a>
<a href="#">travel</a>
</div>
</div>
<div class="largeblog-bottombox">
<div class="largeblog-date">
<span><i class="fa fa-clock-o" aria-hidden="true"></i> July 5, 2017</span>
</div>
<div class="largeblog-social">
<ul>
<li><a href="" title="Like"><i class="fa fa-heart-o" aria-hidden="true"></i></a>
</li>
<li><a href="" title="Facebook"><i class="fa fa-facebook"
aria-hidden="true"></i></a> </li>
<li><a href="" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a>
</li>
<li><a href="" title="Pinterest"><i class="fa fa-pinterest"
aria-hidden="true"></i></a> </li>
</ul>
</div>
</div>
</div>
BlogPost 组件.ts
post: any;
constructor(private meta: Meta, private router: Router, private titleService: Title,
private globle: Globals, private _interaction: CommunicationService, private route: ActivatedRoute) {
this.post = JSON.parse(localStorage.getItem('post'));
if (this.post && this.post.meta_description !== '') {
this.meta.addTag({ name: 'title:', content: this.post.meta_description });
}
}
ngOnInit(): void {
console.log(this.post);
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.post = JSON.parse(localStorage.getItem('post'));
}
});
}
解决方案
仅当您的快速服务器没有崩溃时,SSR 才会显示视图源。SSR 不支持window 和 document object,它们在浏览器中可用。
如果您使用服务工作者进行缓存,即使那样它也不会显示视图源,因为它将由服务工作者提供服务。
所以你必须检查两个地方,如果它是从服务工作者那里得到的,然后检查你的服务器是否在终端中显示生成的 HTML。
推荐阅读
- php - Symfony 验证 - 条件约束
- android - 无法为 QualtricsFeedback 活动的未解决参考查明错误?在 Android Studio 中使用 Kotlin
- react-native - 导航生命周期 ReactNative
- microsoft-teams - Microsoft Teams 中的设置和策略之间的明确关系是什么?
- python - 如何使用 PyPDF2 选择 pdf 格式的单选按钮?
- android - Dagger 无法注入应用程序上下文
- mongoose - 之后如何在猫鼬文件上获得财产?
- python - Django - 重构项目和应用程序名称后没有迁移文件夹
- python - 如何计算无限数量的坐标输入的平均值?
- machine-learning - rapidminer中的逻辑回归最大似然