我想知道如何制作一个只显示一次然后再也不显示的页面,或者在初次启动时像Visual Studio代码一样选中“不再显示我”复选框。
答案 0 :(得分:2)
您可以使用localStorage
:
//Place this code in your electron app's initialisation
if (localStorage.getItem("showStartUpWindow") == "No") {
//Show main window
} else {
//Show startup window and wait until the "OK" button is clicked
if (doNotShowStartUpWindowCheckBox.checked) {
localStorage.setItem("showStartUpWindow", "No");
}
}
答案 1 :(得分:0)
使用localStorage的问题
问题在于,删除应用程序后,使用localStorage存储的数据可以继续保存
C:\Users\%USERNAME%\AppData\Roaming\Atom
包含localDB,indexedDB和其他一些缓存内容
使用其他保留数据的方式
我认为您可以将数据保留在JSON文件或任何配置文件文本中,在这种情况下,请创建json文件(在任何路径下)
javascript
const electron = require('electron');
const path = require('path');
const fs = require('fs');
class Store {
constructor(opts) {
// Renderer process has to get `app` module via `remote`, whereas the main process can get it directly
// app.getPath('userData') will return a string of the user's app data directory path.
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
// We'll use the `configName` property to set the file name and path.join to bring it all together as a string
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = parseDataFile(this.path, opts.defaults);
}
// This will just return the property on the `data` object
get(key) {
return this.data[key];
}
// ...and this will set it
set(key, val) {
this.data[key] = val;
// Wait, I thought using the node.js' synchronous APIs was bad form?
// We're not writing a server so there's not nearly the same IO demand on the process
// Also if we used an async API and our app was quit before the asynchronous write had a chance to complete,
// we might lose that data. Note that in a real app, we would try/catch this.
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}
function parseDataFile(filePath, defaults) {
// We'll try/catch it in case the file doesn't exist yet, which will be the case on the first application run.
// `fs.readFileSync` will return a JSON string which we then parse into a Javascript object
try {
return JSON.parse(fs.readFileSync(filePath));
} catch(error) {
// if there was some kind of error, return the passed in defaults instead.
return defaults;
}
}
// expose the class
module.exports = Store;
该文件是首次创建的,如果配置文件中存在此值,是否可以对其进行验证
javascript
const { app, BrowserWindow } = require('electron');
const path = require('path');
const Store = require('./store.js');
let mainWindow; //do this so that the window object doesn't get GC'd
// First instantiate the class
const store = new Store({
configName: 'user-preferences',
defaults: {
initialLaunch: true
}
});
获取该值并对其进行处理
let { initialLaunch } = store.get('initialLaunch');
if(initialLaunch){
//show initial config window
}else{
//show other window
}