localStorage 无法在 google chrome 上工作

localStorage is not working on google chrome

我正在使用浏览器 localStorage 来存储一个值,但是在使用 google chrome 时,当我们使用 window.location.reload()localStorage.value 刷​​新页面时脸红了。例如

localStorage.value1=true

重新加载后,我没有在 localStorage 中获取此 value1 对象。

相同的代码适用于 mozila firefox,但不适用于 chrome。 使用 firefox 时,localstorage 值是持久的。

试一试

window.localStorage.setItem("value1", true);

var yourvar = window.localStorage.getItem("value1");

LocalStorage 仅支持字符串值,不支持布尔值或其他值。

存储和检索值的方法:

存入值

localStorage.setItem('value1', 'true');

从存储中检索值

var val1 = localStorage.getItem('value1');

Read more on MDN..

您需要使用正确的 API for Storage:

window.localStorage.setItem('value1', 'true');

您正在做的是在一个变量上设置一个 属性,该变量不会在页面加载之间持续存在。 Firefox 可能太聪明了,它意识到您实际上想将值保存在浏览器的本地存储中。

// main.go
package main

import (
    "encoding/json"
    "fmt"
    "log"
    "io/ioutil"
    "net/http"
    "github.com/gorilla/mux"
)

// Article - Our struct for all articles
type Article struct {
    Id      string    `json:"Id"`
    Title   string `json:"Title"`
    Desc    string `json:"desc"`
    Content string `json:"content"`
}

var Articles []Article

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome to the HomePage!")
    fmt.Println("Endpoint Hit: homePage")
}

func returnAllArticles(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Endpoint Hit: returnAllArticles")
    json.NewEncoder(w).Encode(Articles)
}

func returnSingleArticle(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    key := vars["id"]

    for _, article := range Articles {
        if article.Id == key {
            json.NewEncoder(w).Encode(article)
        }
    }
}


func createNewArticle(w http.ResponseWriter, r *http.Request) {
    // get the body of our POST request
    // unmarshal this into a new Article struct
    // append this to our Articles array.    
    reqBody, _ := ioutil.ReadAll(r)

    var article Article 
    json.Unmarshal(reqBody, &article)
    // update our global Articles array to include
    // our new Article
    Articles = append(Articles, article)

    json.NewEncoder(w).Encode(article)
}

func deleteArticle(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]

    for index, article := range Articles {
        if article.Id == id {
            Articles = append(Articles[:index], Articles[index+1:]...)
        }
    }

}

func handleRequests() {
    myRouter := mux.NewRouter().StrictSlash(true)
    myRouter.HandleFunc("/", homePage)
    myRouter.HandleFunc("/articles", returnAllArticles)
    myRouter.HandleFunc("/article", createNewArticle).Methods("POST")
    myRouter.HandleFunc("/article/{id}", deleteArticle).Methods("DELETE")
    myRouter.HandleFunc("/article/{id}", returnSingleArticle)
    log.Fatal(http.ListenAndServe(":10000", myRouter))
}

func main() {
    Articles = []Article{
        Article{Id: "1", Title: "Hello", Desc: "Article Description", Content: "Article Content"},
        Article{Id: "2", Title: "Hello 2", Desc: "Article Description", Content: "Article Content"},
    }
    handleRequests()
}

我刚刚解决了在页面重新加载时没有读取本地存储的问题,我有一个这样写的选项:

    if ('darkMode') === 'true') {
  enableDarkMode();
}

它没有工作,重新加载时它是真的,但 enableDarkMode() 没有调用自己。所以我期待一个答案,发现改变 if 选项中的一个语句实际上解决了我的问题,所以答案是: if (localStorage.getItem('darkMode') === 'true') { enableDarkMode() };

希望对一些人有所帮助。