gpt4 book ai didi

angular - 在 Angular 8 应用程序中实现 session 存储

转载 作者:行者123 更新时间:2023-12-03 15:13:28 29 4
gpt4 key购买 nike

我正在构建一个电影应用程序来帮助我的学习。我想知道如何在 session 存储中捕获和保存按钮的点击次数。
我已经能够让点击工作。它增加并显示每个按钮的点击次数,我用一个指令做到了这一点。我还尝试将按钮的 id 作为键和点击次数作为值附加到 session 存储中,我可以看到它在我记录时有效,但每当我刷新页面时它似乎都不会保留。
注意:我正在使用 API 来获取我的数据
登陆页面组件

import { CountClicks } from './counter.directive';
import { HttpService } from './../http.service';
import { Component, OnInit, ElementRef } from "@angular/core";


@Component({
selector: "app-landing-page",
templateUrl: "./landing.component.html",
styleUrls: ["./landing.component.scss"]
})
export class LandingPageComponent implements OnInit {
constructor(private _http: HttpService, private elRef:ElementRef) {}
movies: Object;
title = "CORNFLIX";
ids:any;
storage:any;
public likeCounter: number = 0;
ngOnInit() {
this._http.getMovies().subscribe(data => {
this.movies = data;
// for (let x of data['results']){
// if(sessionStorage.getItem('#'+x.id) != null){
// console.log("#" + x.id);
// this.ids = sessionStorage.getItem("#" + x.id);
// console.log(this.ids);
// }

// }
// console.log(this.ids);
});
this.storage = sessionStorage
console.log(this.storage)
}
增加喜欢的指令
import { Directive, HostListener } from "@angular/core";

@Directive({ selector: "a[counting]" })


export class CountClicks {
numberOfClicks = 1;
number:any
store:any;
getValue:any;


@HostListener("click", ["$event.target"])
onClick(a): void {

a.innerHTML = `Likes ${this.numberOfClicks++}`;
this.number = this.numberOfClicks+1
// console.log(localStorage.getItem(a.id));
if(sessionStorage.getItem(a.id)){
this.number = sessionStorage.getItem(a.id);
// sessionStorage.clear()
}
this.store = sessionStorage.setItem(a.id, a.innerHTML);
this.getValue = sessionStorage.getItem(a.id)
a.innerHTML = this.getValue;
// console.log("button", btn.id, "number of clicks:", this.numberOfClicks++);
}
}
我希望能够访问 DOM 并在每个按钮上更新 session 存储
<section class="jumbotron">
<h2>
Welcome to {{title}}
</h2>

</section>

<div *ngIf="movies" class="container-fluid d-flex p-2 bd-highlight mb-3 flex-wrap justify-content-between">

<div *ngFor = "let movie of movies['results']" class="card d-block mb-3 " style="width: 18rem;">
<img src='https://image.tmdb.org/t/p/w500{{movie.poster_path}}' class="card-img-top" alt="Movie image">
<div class="card-body">
<h5 class="card-title">Title: {{movie.title}}</h5>
<p class="card-text">Year: {{movie.release_date.slice(0, 4)}}</p>
<a href="#/" class="btn btn-color" id={{movie.id}}>More details</a>
<a href="#/" class="btn btn-color" #{{likeCounter}} id=#{{movie.id}} counting>Likes</a>
</div>
</div>
</div>

最佳答案

要在刷新页面时保存值,您可以使用 localStoragesessionStorage为此。不需要外部库或导入。它应该在大多数浏览器中开箱即用。

为了节省:

// clicks is the variable containing your value to save
localStorage.setItem('clickCounter', clicks);
// If you want to use the sessionStorage
// sessionStorage.setItem('clickCounter', clicks);

装载:
const clicks = localStorage.getItem('clickCounter');
// If you want to use the sessionStorage
// const clicks = sessionStorage.getItem('clickCounter');

您可以使用开发工具在 Chrome 中进行检查。

enter image description here

关于angular - 在 Angular 8 应用程序中实现 session 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58500879/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com